refactor: hook behaviorContextGlobals

This commit is contained in:
cha0s 2019-12-25 00:59:51 -06:00
parent 7deaa58fdb
commit 1c8e8d025a
8 changed files with 115 additions and 66 deletions

View File

@ -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,
};
}

View File

@ -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);

View File

@ -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() {

View File

@ -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,
};
}

View File

@ -1,13 +0,0 @@
import {TickingPromise} from '@avocado/core';
export const wait = (duration) => {
return new TickingPromise(
() => {},
(elapsed, resolve) => {
duration -= elapsed;
if (duration <= 0) {
resolve();
}
},
);
};

View File

@ -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,
};
}

View File

@ -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);
}

View File

@ -0,0 +1,7 @@
import * as MathExt from '.';
export function behaviorContextGlobals() {
return {
Math: MathExt,
};
}