refactor: behavior item hook
This commit is contained in:
parent
2fdee43950
commit
e49ef9b54c
|
@ -1,7 +1,6 @@
|
|||
export {Context} from './context';
|
||||
export {
|
||||
fromJSON as behaviorItemFromJSON,
|
||||
register as registerBehaviorItem
|
||||
} from './item/registry';
|
||||
export {
|
||||
buildCondition,
|
||||
|
|
|
@ -2,10 +2,6 @@ import {Traversal} from './traversal';
|
|||
|
||||
export class Action extends Traversal {
|
||||
|
||||
static type() {
|
||||
return 'action';
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
...super.toJSON(),
|
||||
|
|
|
@ -9,10 +9,6 @@ const decorate = compose(
|
|||
|
||||
export class Actions extends decorate(Traversals) {
|
||||
|
||||
static type() {
|
||||
return 'actions';
|
||||
}
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this._index = 0;
|
||||
|
|
|
@ -4,10 +4,6 @@ export function Collection(type) {
|
|||
const plural = `${type}s`;
|
||||
return class Collection {
|
||||
|
||||
static type() {
|
||||
return plural;
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this[plural] = [];
|
||||
}
|
||||
|
|
|
@ -2,10 +2,6 @@ import {fromJSON as behaviorItemFromJSON} from './registry';
|
|||
|
||||
export class Condition {
|
||||
|
||||
static type() {
|
||||
return 'condition';
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this.operator = '';
|
||||
this.operands = [];
|
||||
|
|
|
@ -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,
|
||||
};
|
||||
}
|
|
@ -1,9 +1,5 @@
|
|||
export class Literal {
|
||||
|
||||
static type() {
|
||||
return 'literal';
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this.value = null;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -2,10 +2,6 @@ import {Actions} from './actions';
|
|||
|
||||
export class Routine {
|
||||
|
||||
static type() {
|
||||
return 'routine';
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this.actions = new Actions();
|
||||
}
|
||||
|
|
|
@ -2,10 +2,6 @@ import {Routine} from './routine';
|
|||
|
||||
export class Routines {
|
||||
|
||||
static type() {
|
||||
return 'routines';
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this.routines = {};
|
||||
}
|
||||
|
|
|
@ -2,10 +2,6 @@ import {fromJSON as behaviorItemFromJSON} from './registry';
|
|||
|
||||
export class Traversal {
|
||||
|
||||
static type() {
|
||||
return 'traversal';
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this.steps = [];
|
||||
this.value = undefined;
|
||||
|
|
Loading…
Reference in New Issue
Block a user