refactor: StatePropertea ;)

This commit is contained in:
cha0s 2022-03-30 02:23:00 -05:00
parent efa1f0a031
commit 72eace044a
5 changed files with 39 additions and 51 deletions

View File

@ -3,6 +3,5 @@
- layer compilation - layer compilation
- don't tick entities without any ticking traits - don't tick entities without any ticking traits
- ~~production build~~ - ~~production build~~
- Trait::initializeStateProperties should be rolled into ::load()
- ~~Split Persea packages~~ - ~~Split Persea packages~~
- ~~Remove @avocado/persea~~ - ~~Remove @avocado/persea~~

View File

@ -214,7 +214,6 @@ export default function Propertea(name, options = {}) {
} }
return (Superclass) => { return (Superclass) => {
let Target; let Target;
if (initialize) {
if (!Superclass[Initializers]) { if (!Superclass[Initializers]) {
Target = class Class extends Superclass { Target = class Class extends Superclass {
@ -232,11 +231,9 @@ export default function Propertea(name, options = {}) {
else { else {
Target = Superclass; Target = Superclass;
} }
if (initialize) {
Target[Initializers].push([key, initialize]); Target[Initializers].push([key, initialize]);
} }
else {
Target = Superclass;
}
Object.defineProperty( Object.defineProperty(
Target.prototype, Target.prototype,
name, name,

View File

@ -26,7 +26,7 @@
"test.js.map" "test.js.map"
], ],
"dependencies": { "dependencies": {
"@avocado/core": "^3.0.0", "@avocado/code": "^3.0.0",
"@avocado/resource": "^3.0.0", "@avocado/resource": "^3.0.0",
"@flecks/core": "^1.4.1" "@flecks/core": "^1.4.1"
}, },

View File

@ -1,34 +1,30 @@
import {Property} from '@avocado/core'; import {Propertea} from '@avocado/code';
export default function StateProperty(key, meta = {}) { export default function StateProperty(name, options = {}) {
const transformedKey = `$$avocado_state_property_${key}`; const {
return (Superclass) => { // eslint-disable-next-line no-new-func
/* eslint-disable func-names, no-new-func, no-param-reassign */ get = new Function(`return this.state.${name};`),
meta.emit = meta.emit || function (...args) { // eslint-disable-next-line no-new-func
this.entity.emit(...args); set = new Function('value, old', `
}; if (value !== old) {
meta.initialize = meta.initialize || function () {
this[transformedKey] = this.state[key];
};
meta.get = meta.get || new Function(`
return this.${transformedKey};
`);
meta.set = meta.set || new Function('value', `
if (value !== this.${transformedKey}) {
this.markAsDirty(); this.markAsDirty();
} }
this.${transformedKey} = value; this.state.${name} = value;
this.state['${key}'] = value; `),
`); } = options;
/* eslint-enable func-names, no-new-func, no-param-reassign */ let {
class Initializable extends Superclass { track = {},
} = options;
initializeStateProperties() { if (true === track) {
super.initializeStateProperties(); track = {};
meta.initialize.call(this);
} }
track.emit ||= function emit(e, o, n) {
} this.entity.emit(e, o, n);
return Property(key, meta)(Initializable);
}; };
return Propertea(name, {
...options,
get,
set,
track,
});
} }

View File

@ -68,9 +68,6 @@ export default class Trait extends JsonResource {
return {}; return {};
} }
// eslint-disable-next-line class-methods-use-this
initializeStateProperties() {}
// eslint-disable-next-line class-methods-use-this // eslint-disable-next-line class-methods-use-this
listeners() { listeners() {
return {}; return {};
@ -81,7 +78,6 @@ export default class Trait extends JsonResource {
const {constructor} = this; const {constructor} = this;
this.params = constructor.defaultParamsWith(params); this.params = constructor.defaultParamsWith(params);
this.state = constructor.defaultStateWith(state); this.state = constructor.defaultStateWith(state);
this.initializeStateProperties();
this.#previousState = JSON.parse(JSON.stringify(this.state)); this.#previousState = JSON.parse(JSON.stringify(this.state));
} }