From 1c8e8d025aa56d4cda3df618fce9c51845c3451f Mon Sep 17 00:00:00 2001 From: cha0s Date: Wed, 25 Dec 2019 00:59:51 -0600 Subject: [PATCH] refactor: hook behaviorContextGlobals --- packages/behavior/context/flow.hooks.js | 23 +++++++++++++ packages/behavior/context/flow.js | 9 ----- packages/behavior/context/index.js | 38 ++++++++++++--------- packages/behavior/context/timing.hooks.js | 23 +++++++++++++ packages/behavior/context/timing.js | 13 -------- packages/behavior/context/utility.hooks.js | 39 ++++++++++++++++++++++ packages/behavior/context/utility.js | 29 ---------------- packages/math/globals.hooks.js | 7 ++++ 8 files changed, 115 insertions(+), 66 deletions(-) create mode 100644 packages/behavior/context/flow.hooks.js delete mode 100644 packages/behavior/context/flow.js create mode 100644 packages/behavior/context/timing.hooks.js delete mode 100644 packages/behavior/context/timing.js create mode 100644 packages/behavior/context/utility.hooks.js delete mode 100644 packages/behavior/context/utility.js create mode 100644 packages/math/globals.hooks.js diff --git a/packages/behavior/context/flow.hooks.js b/packages/behavior/context/flow.hooks.js new file mode 100644 index 0000000..2f5c4e6 --- /dev/null +++ b/packages/behavior/context/flow.hooks.js @@ -0,0 +1,23 @@ +class Flow { + + static conditional(condition, actions, context) { + if (condition.get(context)) { + return actions.serial(context); + } + }; + + static parallel(actions, context) { + return actions.parallel(context); + } + + static serial(actions, context) { + return actions.serial(context); + } + +} + +export function behaviorContextGlobals() { + return { + Flow, + }; +} diff --git a/packages/behavior/context/flow.js b/packages/behavior/context/flow.js deleted file mode 100644 index 5b0a790..0000000 --- a/packages/behavior/context/flow.js +++ /dev/null @@ -1,9 +0,0 @@ -export const conditional = (condition, actions, context) => { - if (condition.get(context)) { - return actions.serial(context); - } -}; - -export const parallel = (actions, context) => actions.parallel(context); - -export const serial = (actions, context) => actions.serial(context); diff --git a/packages/behavior/context/index.js b/packages/behavior/context/index.js index b38bb54..0ab4a6a 100644 --- a/packages/behavior/context/index.js +++ b/packages/behavior/context/index.js @@ -1,39 +1,47 @@ import D from 'debug'; -import {fastApply} from '@avocado/core'; -import * as MathExt from '@avocado/math'; - -import * as Flow from './flow'; -import * as Timing from './timing'; -import * as Utility from './utility'; +import {fastApply, invokeHookFlat} from '@avocado/core'; const debug = D('@avocado:behavior:context'); export class Context { constructor(defaults = {}) { - this.map = new Map(); - this.addGlobalDefaults(); + this.initialize(); for (const key in defaults) { this.add(key, defaults[key]); } } + static globals() { + if (!Context._globals) { + Context._globals = {}; + const globalss = invokeHookFlat('behaviorContextGlobals'); + for (let i = 0; i < globalss.length; i++) { + const globals = globalss[i]; + for (const key in globals) { + Context._globals[key] = globals[key]; + } + } + } + return Context._globals; + } + add(key, value) { this.map.set(key, value); } - addGlobalDefaults() { + initialize() { + this.map = new Map(); this.add('context', this); - this.add('Flow', Flow); - this.add('Math', MathExt); - this.add('Timing', Timing); - this.add('Utility', Utility); + const globals = this.constructor.globals(); + for (const key in globals) { + this.add(key, globals[key]); + } } clear() { - this.map.clear(); - this.addGlobalDefaults(); + this.initialize(); } destroy() { diff --git a/packages/behavior/context/timing.hooks.js b/packages/behavior/context/timing.hooks.js new file mode 100644 index 0000000..1ef60c4 --- /dev/null +++ b/packages/behavior/context/timing.hooks.js @@ -0,0 +1,23 @@ +import {TickingPromise} from '@avocado/core'; + +class Timing { + + static wait (duration) { + return new TickingPromise( + () => {}, + (elapsed, resolve) => { + duration -= elapsed; + if (duration <= 0) { + resolve(); + } + }, + ); + } + +} + +export function behaviorContextGlobals() { + return { + Timing, + }; +} diff --git a/packages/behavior/context/timing.js b/packages/behavior/context/timing.js deleted file mode 100644 index 9afd0c1..0000000 --- a/packages/behavior/context/timing.js +++ /dev/null @@ -1,13 +0,0 @@ -import {TickingPromise} from '@avocado/core'; - -export const wait = (duration) => { - return new TickingPromise( - () => {}, - (elapsed, resolve) => { - duration -= elapsed; - if (duration <= 0) { - resolve(); - } - }, - ); -}; diff --git a/packages/behavior/context/utility.hooks.js b/packages/behavior/context/utility.hooks.js new file mode 100644 index 0000000..c9ae51b --- /dev/null +++ b/packages/behavior/context/utility.hooks.js @@ -0,0 +1,39 @@ +import {merge as mergeObject} from '@avocado/core'; + +class Utility { + + static makeArray(...args) { + // No context! + args.pop(); + return args; + }; + + static makeObject(...args) { + // No context! + args.pop(); + const object = {}; + while (args.length > 0) { + const key = args.pop(); + const value = args.pop(); + object[key] = value; + } + return object; + }; + + static log(...args) { + return console.log(...args); + } + + static merge(...args) { + // No context! + args.pop(); + return mergeObject(...args); + } + +} + +export function behaviorContextGlobals() { + return { + Utility, + }; +} diff --git a/packages/behavior/context/utility.js b/packages/behavior/context/utility.js deleted file mode 100644 index 0e2c9d2..0000000 --- a/packages/behavior/context/utility.js +++ /dev/null @@ -1,29 +0,0 @@ -import {merge as mergeObject} from '@avocado/core'; - -export function makeArray(...args) { - // No context! - args.pop(); - return args; -}; - -export function makeObject(...args) { - // No context! - args.pop(); - const object = {}; - while (args.length > 0) { - const key = args.pop(); - const value = args.pop(); - object[key] = value; - } - return object; -}; - -export function log(...args) { - return console.log(...args); -} - -export function merge(...args) { - // No context! - args.pop(); - return mergeObject(...args); -} diff --git a/packages/math/globals.hooks.js b/packages/math/globals.hooks.js new file mode 100644 index 0000000..4e893fc --- /dev/null +++ b/packages/math/globals.hooks.js @@ -0,0 +1,7 @@ +import * as MathExt from '.'; + +export function behaviorContextGlobals() { + return { + Math: MathExt, + }; +}