refactor: context

This commit is contained in:
cha0s 2021-02-01 23:28:16 -06:00
parent caa7c1f047
commit 381fb7cb4b
7 changed files with 18 additions and 31 deletions

View File

@ -78,6 +78,6 @@ export default (latus) => (expression) => {
// eslint-disable-next-line no-param-reassign
current = next;
return current;
}), context.getValue(rawOps[0].key));
}), context.get(rawOps[0].key));
};
};

View File

@ -7,27 +7,22 @@ export default class Context {
this.addObjectMap(defaults);
}
add(key, value, type = 'undefined') {
add(key, value) {
if (key) {
this.map.set(key, [value, type]);
this.map.set(key, value);
}
}
addObjectMap(map) {
Object.entries(map)
.forEach(([key, [variable, type]]) => (
this.add(key, variable, type)
.forEach(([key, variable]) => (
this.add(key, variable)
));
}
all() {
return Array.from(this.map.entries())
.map(([key, [value]]) => [key, value]);
}
clear() {
this.destroy();
this.add('context', this, 'context');
this.add('context', this);
this.addObjectMap(this.latus.get('%behaviorGlobals'));
}
@ -55,7 +50,7 @@ export default class Context {
}
describeChildren() {
return this.all()
return Array.from(this.map.entries())
.reduce(
(r, [key, value]) => ({
...r,
@ -84,15 +79,7 @@ export default class Context {
}
get(key) {
return this.has(key) ? this.map.get(key) : [undefined, 'undefined'];
}
getValue(key) {
return this.get(key)[0];
}
getType(key) {
return this.get(key)[1];
return this.map.get(key);
}
has(key) {

View File

@ -3,8 +3,8 @@ import Timing from './timing';
import Utility from './utility';
export default (latus) => ({
latus: [latus, 'latus'],
Flow: [Flow, 'Flow'],
Timing: [Timing, 'Timing'],
Utility: [Utility, 'Utility'],
latus,
Flow,
Timing,
Utility,
});

View File

@ -90,7 +90,7 @@ export default (latus) => class Behaved extends decorate(Trait) {
await super.load(json);
this.#context = new Context(
{
entity: [this.entity, 'entity'],
entity: this.entity,
},
latus,
);

View File

@ -156,7 +156,7 @@ export default (latus) => class Alive extends decorate(Trait) {
await super.load(json);
this.#context = new Context(
{
entity: [this.entity, 'entity'],
entity: this.entity,
},
latus,
);

View File

@ -12,8 +12,8 @@ export * as Vertice from './vertice';
export default {
hooks: {
'@avocado/behavior/globals': () => ({
Math: [Math, 'Math'],
Vector: [Vector, 'Vector'],
Math,
Vector,
}),
},
};

View File

@ -210,8 +210,8 @@ export default (latus) => class Collider extends decorate(Trait) {
pushCollisionTickingPromise(actions, other) {
const context = new Context(
{
entity: [this.entity, 'entity'],
other: [other, 'entity'],
entity: this.entity,
other,
},
latus,
);