refactor: StateProperty optimizations

This commit is contained in:
cha0s 2019-04-12 17:21:43 -05:00
parent 9d618d1998
commit e16e969170

View File

@ -89,16 +89,27 @@ export class Trait extends Synchronized {
} }
export function StateProperty(key, meta = {}) { export function StateProperty(key, meta = {}) {
let transformedProperty;
if (meta.transformProperty) {
transformedProperty = meta.transformProperty(key);
}
else {
transformedProperty = `$$avocado_state_property_${key}`;
}
return (Superclass) => { return (Superclass) => {
meta.emit = meta.emit || function(...args) { meta.emit = meta.emit || function(...args) {
this.entity.emit(...args); this.entity.emit(...args);
}; };
meta.get = meta.get || function(value) { meta.initialize = meta.initialize || function() {
return this.state.get(key); this[transformedProperty] = this.state.get(key);
}; }
meta.set = meta.set || function(value) { meta.get = meta.get || new Function(`
this.state = this.state.set(key, value); return this.${transformedProperty};
}; `);
meta.set = meta.set || new Function('value', `
this.${transformedProperty} = value;
this.state = this.state.set('${key}', value);
`);
return Property(key, meta)(Superclass); return Property(key, meta)(Superclass);
} }
} }