From 72eace044af612f1dab5924812275a9a97912c23 Mon Sep 17 00:00:00 2001 From: cha0s Date: Wed, 30 Mar 2022 02:23:00 -0500 Subject: [PATCH] refactor: StatePropertea ;) --- TODO.md | 1 - packages/code/src/propertea.js | 29 +++++++------- packages/traits/package.json | 2 +- packages/traits/src/state-property.js | 54 +++++++++++++-------------- packages/traits/src/trait.js | 4 -- 5 files changed, 39 insertions(+), 51 deletions(-) diff --git a/TODO.md b/TODO.md index ff39ffa..169c265 100644 --- a/TODO.md +++ b/TODO.md @@ -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~~ diff --git a/packages/code/src/propertea.js b/packages/code/src/propertea.js index 09106d1..223adad 100644 --- a/packages/code/src/propertea.js +++ b/packages/code/src/propertea.js @@ -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, diff --git a/packages/traits/package.json b/packages/traits/package.json index 67bb9e5..4f04b5c 100644 --- a/packages/traits/package.json +++ b/packages/traits/package.json @@ -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" }, diff --git a/packages/traits/src/state-property.js b/packages/traits/src/state-property.js index 8c10189..edf650b 100644 --- a/packages/traits/src/state-property.js +++ b/packages/traits/src/state-property.js @@ -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, + }); } diff --git a/packages/traits/src/trait.js b/packages/traits/src/trait.js index 99087d5..281cd45 100644 --- a/packages/traits/src/trait.js +++ b/packages/traits/src/trait.js @@ -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)); }