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
- don't tick entities without any ticking traits
- ~~production build~~
- Trait::initializeStateProperties should be rolled into ::load()
- ~~Split Persea packages~~
- ~~Remove @avocado/persea~~

View File

@ -214,29 +214,26 @@ export default function Propertea(name, options = {}) {
}
return (Superclass) => {
let Target;
if (initialize) {
if (!Superclass[Initializers]) {
Target = class Class extends Superclass {
if (!Superclass[Initializers]) {
Target = class Class extends Superclass {
constructor(...args) {
super(...args);
for (let i = 0; i < this.constructor[Initializers].length; ++i) {
const [key, initialize] = this.constructor[Initializers][i];
this[key] = initialize.call(this);
}
constructor(...args) {
super(...args);
for (let i = 0; i < this.constructor[Initializers].length; ++i) {
const [key, initialize] = this.constructor[Initializers][i];
this[key] = initialize.call(this);
}
}
};
Target[Initializers] = [];
}
else {
Target = Superclass;
}
Target[Initializers].push([key, initialize]);
};
Target[Initializers] = [];
}
else {
Target = Superclass;
}
if (initialize) {
Target[Initializers].push([key, initialize]);
}
Object.defineProperty(
Target.prototype,
name,

View File

@ -26,7 +26,7 @@
"test.js.map"
],
"dependencies": {
"@avocado/core": "^3.0.0",
"@avocado/code": "^3.0.0",
"@avocado/resource": "^3.0.0",
"@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 = {}) {
const transformedKey = `$$avocado_state_property_${key}`;
return (Superclass) => {
/* eslint-disable func-names, no-new-func, no-param-reassign */
meta.emit = meta.emit || function (...args) {
this.entity.emit(...args);
};
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}) {
export default function StateProperty(name, options = {}) {
const {
// eslint-disable-next-line no-new-func
get = new Function(`return this.state.${name};`),
// eslint-disable-next-line no-new-func
set = new Function('value, old', `
if (value !== old) {
this.markAsDirty();
}
this.${transformedKey} = value;
this.state['${key}'] = value;
`);
/* eslint-enable func-names, no-new-func, no-param-reassign */
class Initializable extends Superclass {
initializeStateProperties() {
super.initializeStateProperties();
meta.initialize.call(this);
}
}
return Property(key, meta)(Initializable);
this.state.${name} = value;
`),
} = options;
let {
track = {},
} = options;
if (true === track) {
track = {};
}
track.emit ||= function emit(e, o, n) {
this.entity.emit(e, o, n);
};
return Propertea(name, {
...options,
get,
set,
track,
});
}

View File

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