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 = {}) {
let transformedProperty;
if (meta.transformProperty) {
transformedProperty = meta.transformProperty(key);
}
else {
transformedProperty = `$$avocado_state_property_${key}`;
}
return (Superclass) => {
meta.emit = meta.emit || function(...args) {
this.entity.emit(...args);
};
meta.get = meta.get || function(value) {
return this.state.get(key);
};
meta.set = meta.set || function(value) {
this.state = this.state.set(key, value);
};
meta.initialize = meta.initialize || function() {
this[transformedProperty] = this.state.get(key);
}
meta.get = meta.get || new Function(`
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);
}
}