refactor: behavior item hook

This commit is contained in:
cha0s 2019-11-20 01:01:26 -06:00
parent 2fdee43950
commit e49ef9b54c
11 changed files with 30 additions and 52 deletions

View File

@ -1,7 +1,6 @@
export {Context} from './context';
export {
fromJSON as behaviorItemFromJSON,
register as registerBehaviorItem
} from './item/registry';
export {
buildCondition,

View File

@ -2,10 +2,6 @@ import {Traversal} from './traversal';
export class Action extends Traversal {
static type() {
return 'action';
}
toJSON() {
return {
...super.toJSON(),

View File

@ -9,10 +9,6 @@ const decorate = compose(
export class Actions extends decorate(Traversals) {
static type() {
return 'actions';
}
constructor() {
super();
this._index = 0;

View File

@ -4,10 +4,6 @@ export function Collection(type) {
const plural = `${type}s`;
return class Collection {
static type() {
return plural;
}
constructor() {
this[plural] = [];
}

View File

@ -2,10 +2,6 @@ import {fromJSON as behaviorItemFromJSON} from './registry';
export class Condition {
static type() {
return 'condition';
}
constructor() {
this.operator = '';
this.operands = [];

View File

@ -1,5 +1,3 @@
import {register} from './registry';
import {Action} from './action';
import {Actions} from './actions';
import {Condition} from './condition';
@ -10,12 +8,16 @@ import {Routines} from './routines';
import {Traversal} from './traversal';
import {Traversals} from './traversals';
register(Action);
register(Actions);
register(Condition);
register(Conditions);
register(Literal);
register(Routine);
register(Routines);
register(Traversal);
register(Traversals);
export function avocadoBehaviorItems() {
return {
action: Action,
actions: Actions,
condition: Condition,
conditions: Conditions,
literal: Literal,
routine: Routine,
routines: Routines,
traversal: Traversal,
traversals: Traversals,
};
}

View File

@ -1,9 +1,5 @@
export class Literal {
static type() {
return 'literal';
}
constructor() {
this.value = null;
}

View File

@ -1,17 +1,26 @@
const behaviorItemRegistry = new Map();
import {invokeHookFlat} from '@avocado/core';
let _behaviorItems;
export function fromJSON({type, ...json}) {
const Class = behaviorItemRegistry.get(type);
const items = behaviorItems();
const Class = items[type];
if (!Class) {
throw new TypeError(`There is no class for the behavior item "${type}"`);
}
return (new Class()).fromJSON(json);
}
export function deregister(BehaviorItem) {
behaviorItemRegistry.delete(BehaviorItem.type());
}
export function register(BehaviorItem) {
behaviorItemRegistry.set(BehaviorItem.type(), BehaviorItem);
function behaviorItems() {
if (!_behaviorItems) {
_behaviorItems = {};
const itemsLists = invokeHookFlat('avocadoBehaviorItems');
for (let i = 0; i < itemsLists.length; i++) {
_behaviorItems = {
..._behaviorItems,
...itemsLists[i],
};
}
}
return _behaviorItems;
}

View File

@ -2,10 +2,6 @@ import {Actions} from './actions';
export class Routine {
static type() {
return 'routine';
}
constructor() {
this.actions = new Actions();
}

View File

@ -2,10 +2,6 @@ import {Routine} from './routine';
export class Routines {
static type() {
return 'routines';
}
constructor() {
this.routines = {};
}

View File

@ -2,10 +2,6 @@ import {fromJSON as behaviorItemFromJSON} from './registry';
export class Traversal {
static type() {
return 'traversal';
}
constructor() {
this.steps = [];
this.value = undefined;