refactor: ensure key design
This commit is contained in:
parent
ada3dfd997
commit
e560d89b0c
|
@ -29,14 +29,7 @@ export function PropertyMixin(key, meta = {}) {
|
|||
else {
|
||||
metaDefault = JSON.parse(JSON.stringify(meta.default));
|
||||
}
|
||||
// Transform key. TODO: dynamic? Comes at a price...
|
||||
let transformedProperty;
|
||||
if (meta.transformProperty) {
|
||||
transformedProperty = meta.transformProperty(key);
|
||||
}
|
||||
else {
|
||||
transformedProperty = `$$avocado_property_${key}`;
|
||||
}
|
||||
const transformedKey = `$$avocado_property_${key}`;
|
||||
class Property extends Superclass {
|
||||
|
||||
constructor(...args) {
|
||||
|
@ -48,7 +41,7 @@ export function PropertyMixin(key, meta = {}) {
|
|||
else {
|
||||
// Set default.
|
||||
if (undefined !== metaDefault) {
|
||||
this[transformedProperty] = metaDefault;
|
||||
this[transformedKey] = metaDefault;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -56,13 +49,13 @@ export function PropertyMixin(key, meta = {}) {
|
|||
}
|
||||
// Getter.
|
||||
const getter = meta.get ? meta.get : new Function(`
|
||||
return this.${transformedProperty};
|
||||
return this.${transformedKey};
|
||||
`);
|
||||
// Setter.
|
||||
let setter;
|
||||
// Helper to define assigner.
|
||||
function defineAssigner() {
|
||||
const assignMethod = `${transformedProperty}$assign`;
|
||||
const assignMethod = `${transformedKey}$assign`;
|
||||
Object.defineProperty(Property.prototype, assignMethod, {
|
||||
value: meta.set,
|
||||
});
|
||||
|
@ -76,7 +69,7 @@ export function PropertyMixin(key, meta = {}) {
|
|||
this.emit(...args);
|
||||
}
|
||||
};
|
||||
const emitMethod = `${transformedProperty}$emit`;
|
||||
const emitMethod = `${transformedKey}$emit`;
|
||||
Object.defineProperty(Property.prototype, emitMethod, {
|
||||
value: emitter,
|
||||
});
|
||||
|
@ -86,7 +79,7 @@ export function PropertyMixin(key, meta = {}) {
|
|||
const comparator = meta.eq ? meta.eq : function(l, r) {
|
||||
return l === r;
|
||||
};
|
||||
const compareMethod = `${transformedProperty}$compare`;
|
||||
const compareMethod = `${transformedKey}$compare`;
|
||||
Object.defineProperty(Property.prototype, compareMethod, {
|
||||
value: comparator,
|
||||
});
|
||||
|
@ -107,7 +100,7 @@ export function PropertyMixin(key, meta = {}) {
|
|||
// Set with comparator and emitter.
|
||||
setter = new Function('value', `
|
||||
const old = this.${key};
|
||||
this.${transformedProperty} = value;
|
||||
this.${transformedKey} = value;
|
||||
if (!this.${compareMethod}(old, value)) {
|
||||
this.${emitMethod}('${key}Changed', old, value);
|
||||
}
|
||||
|
@ -133,7 +126,7 @@ export function PropertyMixin(key, meta = {}) {
|
|||
// Set with emitter.
|
||||
setter = new Function('value', `
|
||||
const old = this.${key};
|
||||
this.${transformedProperty} = value;
|
||||
this.${transformedKey} = value;
|
||||
if (old !== value) {
|
||||
this.${emitMethod}('${key}Changed', old, value);
|
||||
}
|
||||
|
@ -155,7 +148,7 @@ export function PropertyMixin(key, meta = {}) {
|
|||
else {
|
||||
// Set raw.
|
||||
setter = new Function('value', `
|
||||
this.${transformedProperty} = value;
|
||||
this.${transformedKey} = value;
|
||||
`);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -140,28 +140,22 @@ export class Trait extends decorate(class {}) {
|
|||
}
|
||||
|
||||
export function StateProperty(key, meta = {}) {
|
||||
let transformedProperty;
|
||||
if (meta.transformProperty) {
|
||||
transformedProperty = meta.transformProperty(key);
|
||||
}
|
||||
else {
|
||||
transformedProperty = `$$avocado_state_property_${key}`;
|
||||
}
|
||||
const transformedKey = `$$avocado_state_property_${key}`;
|
||||
return (Superclass) => {
|
||||
meta.emit = meta.emit || function(...args) {
|
||||
this.entity.emit(...args);
|
||||
};
|
||||
meta.initialize = meta.initialize || function() {
|
||||
this[transformedProperty] = this.state[key];
|
||||
this[transformedKey] = this.state[key];
|
||||
}
|
||||
meta.get = meta.get || new Function(`
|
||||
return this.${transformedProperty};
|
||||
return this.${transformedKey};
|
||||
`);
|
||||
meta.set = meta.set || new Function('value', `
|
||||
if (value !== this.${transformedProperty}) {
|
||||
if (value !== this.${transformedKey}) {
|
||||
this.setDirty();
|
||||
}
|
||||
this.${transformedProperty} = value;
|
||||
this.${transformedKey} = value;
|
||||
this.state['${key}'] = value;
|
||||
`);
|
||||
return Property(key, meta)(Superclass);
|
||||
|
|
|
@ -15,25 +15,19 @@ export function VectorMixin(
|
|||
},
|
||||
...meta,
|
||||
};
|
||||
let transformedProperty;
|
||||
if (meta.transformProperty) {
|
||||
transformedProperty = meta.transformProperty(vectorKey);
|
||||
}
|
||||
else {
|
||||
transformedProperty = `$$avocado_property_${vectorKey}`;
|
||||
}
|
||||
const transformedKey = `$$avocado_property_${vectorKey}`;
|
||||
meta.initialize = function() {
|
||||
this[transformedProperty] = Vector.copy(meta.default);
|
||||
this[transformedKey] = Vector.copy(meta.default);
|
||||
};
|
||||
const bypass = `${transformedProperty}$bypassChangedEvent`;
|
||||
const bypass = `${transformedKey}$bypassChangedEvent`;
|
||||
meta.set = meta.set || new Function('vector', `
|
||||
this.${transformedProperty} = [
|
||||
this.${transformedProperty}[0],
|
||||
this.${transformedProperty}[1],
|
||||
this.${transformedKey} = [
|
||||
this.${transformedKey}[0],
|
||||
this.${transformedKey}[1],
|
||||
];
|
||||
if (this.${bypass}) {
|
||||
this.${transformedProperty}[0] = vector[0];
|
||||
this.${transformedProperty}[1] = vector[1];
|
||||
this.${transformedKey}[0] = vector[0];
|
||||
this.${transformedKey}[1] = vector[1];
|
||||
}
|
||||
else {
|
||||
this.${x} = vector[0];
|
||||
|
@ -43,7 +37,7 @@ export function VectorMixin(
|
|||
function createPropertyPair(axe) {
|
||||
return {
|
||||
get: new Function(`
|
||||
return this.${transformedProperty}[${'x' === axe ? 0 : 1}];
|
||||
return this.${transformedKey}[${'x' === axe ? 0 : 1}];
|
||||
`),
|
||||
set: new Function('value', `
|
||||
this.${bypass} = true;
|
||||
|
|
Loading…
Reference in New Issue
Block a user