perf: initialize
This commit is contained in:
parent
e99ae1136b
commit
030bf436c4
|
@ -37,9 +37,31 @@ export default class Component {
|
|||
return [];
|
||||
}
|
||||
const allocated = this.allocateMany(entries.length);
|
||||
const {properties} = this.constructor.schema.specification.concrete;
|
||||
const Schema = this.constructor.schema.constructor;
|
||||
const keys = new Set(Object.keys(properties));
|
||||
const {
|
||||
constructor: Schema,
|
||||
specification: {concrete: {properties}},
|
||||
} = this.constructor.schema;
|
||||
const defaults = {};
|
||||
for (const key in properties) {
|
||||
defaults[key] = ((key) => () => Schema.defaultValue(properties[key]))(key);
|
||||
switch (properties[key].type) {
|
||||
case 'float32':
|
||||
case 'float64':
|
||||
case 'int8':
|
||||
case 'int16':
|
||||
case 'int32':
|
||||
case 'int64':
|
||||
case 'string':
|
||||
case 'uint8':
|
||||
case 'uint16':
|
||||
case 'uint32':
|
||||
case 'uint64': {
|
||||
defaults[key] = defaults[key]();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
const keys = new Set(Object.keys(defaults));
|
||||
const promises = [];
|
||||
for (let i = 0; i < entries.length; ++i) {
|
||||
const [entityId, values] = entries[i];
|
||||
|
@ -48,15 +70,14 @@ export default class Component {
|
|||
this.instances[entityId] = instance;
|
||||
for (const key in values) {
|
||||
keys.delete(key);
|
||||
instance[key] = values[key];
|
||||
}
|
||||
const defaultValues = {};
|
||||
for (const key of keys) {
|
||||
const defaultValue = Schema.defaultValue(properties[key]);
|
||||
if ('undefined' !== typeof defaultValue) {
|
||||
instance[`$$${key}`] = defaultValue;
|
||||
instance[key] = defaultValue;
|
||||
}
|
||||
defaultValues[key] = 'function' === typeof defaults[key]
|
||||
? defaults[key]()
|
||||
: defaults[key];
|
||||
}
|
||||
instance.initialize(values, defaultValues);
|
||||
promises.push(this.load(instance));
|
||||
}
|
||||
await Promise.all(promises);
|
||||
|
@ -114,6 +135,14 @@ export default class Component {
|
|||
const Instance = class {
|
||||
$$entity = 0;
|
||||
destroy() {}
|
||||
initialize(values, defaults) {
|
||||
for (const key in values) {
|
||||
this[`$$${key}`] = values[key];
|
||||
}
|
||||
for (const key in defaults) {
|
||||
this[`$$${key}`] = defaults[key];
|
||||
}
|
||||
}
|
||||
toNet(recipient, data) {
|
||||
return data || Component.constructor.filterDefaults(this);
|
||||
}
|
||||
|
|
|
@ -70,6 +70,19 @@ export default class Sprite extends Component {
|
|||
const [, s, l] = hexToHsl(this.$$tint);
|
||||
this.tint = hslToHex(hue, s, l);
|
||||
}
|
||||
initialize(values, defaults) {
|
||||
let {
|
||||
anchorX = defaults.anchorX,
|
||||
anchorY = defaults.anchorY,
|
||||
frame = defaults.frame,
|
||||
scaleX = defaults.scaleX,
|
||||
scaleY = defaults.scaleY,
|
||||
} = values;
|
||||
this.$$anchor = {x: anchorX, y: anchorY};
|
||||
this.$$scale = {x: scaleX, y: scaleY};
|
||||
super.initialize(values, defaults);
|
||||
this.frame = frame;
|
||||
}
|
||||
get lightness() {
|
||||
return this.$$lightness;
|
||||
}
|
||||
|
|
|
@ -35,6 +35,7 @@ export default class Schema {
|
|||
return {
|
||||
$: this.$$types[type],
|
||||
concrete: $$type.normalize ? $$type.normalize(rest) : rest,
|
||||
type,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user