function-tree
Advanced tools
Comparing version 4.0.0-1514099265786 to 4.0.0-1519463520217
@@ -14,2 +14,5 @@ var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
// Used by cerebral/devtools | ||
export { default as DevtoolsBase } from './base'; | ||
export var Devtools = function (_DevtoolsBase) { | ||
@@ -25,3 +28,3 @@ _inherits(Devtools, _DevtoolsBase); | ||
_this.latestExecutionId = null; | ||
_this.version = "4.0.0-1514099265786"; | ||
_this.version = "4.0.0-1519463520217"; | ||
_this.init(); | ||
@@ -28,0 +31,0 @@ return _this; |
@@ -11,73 +11,75 @@ import { FunctionTreeExecutionError } from './errors'; | ||
*/ | ||
export default function executeTree(execution, initialPayload, branchStart, branchEnd, parallelStart, parallelProgress, parallelEnd, end) { | ||
export default function executeTree(execution, initialPayload, executeBranchWrapper, branchStart, branchEnd, parallelStart, parallelProgress, parallelEnd, end) { | ||
function runBranch(branch, index, payload, prevPayload, nextBranch) { | ||
function runNextItem(result) { | ||
runBranch(branch, index + 1, result, payload, nextBranch); | ||
} | ||
executeBranchWrapper(function () { | ||
function runNextItem(result) { | ||
runBranch(branch, index + 1, result, payload, nextBranch); | ||
} | ||
function processFunctionOutput(funcDetails, outputResult) { | ||
return function (result) { | ||
var newPayload = Object.assign({}, payload, result ? result.payload : {}); | ||
function processFunctionOutput(funcDetails, outputResult) { | ||
return function (result) { | ||
var newPayload = Object.assign({}, payload, result ? result.payload : {}); | ||
if (result && funcDetails.outputs) { | ||
var outputs = Object.keys(funcDetails.outputs); | ||
if (result && funcDetails.outputs) { | ||
var outputs = Object.keys(funcDetails.outputs); | ||
if (~outputs.indexOf(result.path)) { | ||
branchStart(funcDetails, result.path, newPayload); | ||
runBranch(funcDetails.outputs[result.path].items, 0, newPayload, payload, outputResult); | ||
if (~outputs.indexOf(result.path)) { | ||
branchStart(funcDetails, result.path, newPayload); | ||
runBranch(funcDetails.outputs[result.path].items, 0, newPayload, payload, outputResult); | ||
} else { | ||
throw new FunctionTreeExecutionError(execution, funcDetails, payload, 'function ' + funcDetails.name + ' must use one of its possible outputs: ' + outputs.join(', ') + '.'); | ||
} | ||
} else { | ||
throw new FunctionTreeExecutionError(execution, funcDetails, payload, 'function ' + funcDetails.name + ' must use one of its possible outputs: ' + outputs.join(', ') + '.'); | ||
outputResult(newPayload); | ||
} | ||
} else { | ||
outputResult(newPayload); | ||
}; | ||
} | ||
var currentItem = branch[index]; | ||
if (!currentItem) { | ||
if (branch !== execution.staticTree) { | ||
branchEnd(payload); | ||
} | ||
}; | ||
} | ||
nextBranch(payload); | ||
} else if (isPrimitive(currentItem, 'sequence')) { | ||
runBranch(currentItem.items, 0, payload, prevPayload, runNextItem); | ||
} else if (isPrimitive(currentItem, 'parallel')) { | ||
var itemLength = currentItem.items.length; | ||
var payloads = []; | ||
var currentItem = branch[index]; | ||
parallelStart(payload, itemLength); | ||
currentItem.items.forEach(function (func, index) { | ||
if (func.function) { | ||
execution.runFunction(func, payload, prevPayload, processFunctionOutput(func, function (payload) { | ||
payloads.push(payload); | ||
if (payloads.length === itemLength) { | ||
parallelEnd(payload, itemLength); | ||
runNextItem(Object.assign.apply(Object, [{}].concat(payloads))); | ||
} else { | ||
parallelProgress(payload, itemLength - payloads.length); | ||
} | ||
})); | ||
} else { | ||
runBranch(func.items, 0, payload, prevPayload, function (payload) { | ||
payloads.push(payload); | ||
if (payloads.length === itemLength) { | ||
parallelEnd(payload, itemLength); | ||
runNextItem(Object.assign.apply(Object, [{}].concat(payloads))); | ||
} else { | ||
parallelProgress(payload, itemLength - payloads.length); | ||
} | ||
}); | ||
} | ||
if (!currentItem) { | ||
if (branch !== execution.staticTree) { | ||
branchEnd(payload); | ||
return payloads; | ||
}); | ||
} else { | ||
execution.runFunction(currentItem, payload, prevPayload, processFunctionOutput(currentItem, runNextItem)); | ||
} | ||
nextBranch(payload); | ||
} else if (isPrimitive(currentItem, 'sequence')) { | ||
runBranch(currentItem.items, 0, payload, prevPayload, runNextItem); | ||
} else if (isPrimitive(currentItem, 'parallel')) { | ||
var itemLength = currentItem.items.length; | ||
var payloads = []; | ||
parallelStart(payload, itemLength); | ||
currentItem.items.forEach(function (func, index) { | ||
if (func.function) { | ||
execution.runFunction(func, payload, prevPayload, processFunctionOutput(func, function (payload) { | ||
payloads.push(payload); | ||
if (payloads.length === itemLength) { | ||
parallelEnd(payload, itemLength); | ||
runNextItem(Object.assign.apply(Object, [{}].concat(payloads))); | ||
} else { | ||
parallelProgress(payload, itemLength - payloads.length); | ||
} | ||
})); | ||
} else { | ||
runBranch(func.items, 0, payload, prevPayload, function (payload) { | ||
payloads.push(payload); | ||
if (payloads.length === itemLength) { | ||
parallelEnd(payload, itemLength); | ||
runNextItem(Object.assign.apply(Object, [{}].concat(payloads))); | ||
} else { | ||
parallelProgress(payload, itemLength - payloads.length); | ||
} | ||
}); | ||
} | ||
return payloads; | ||
}); | ||
} else { | ||
execution.runFunction(currentItem, payload, prevPayload, processFunctionOutput(currentItem, runNextItem)); | ||
} | ||
}); | ||
} | ||
return runBranch([execution.staticTree], 0, initialPayload, null, end); | ||
runBranch([execution.staticTree], 0, initialPayload, null, end); | ||
} | ||
//# sourceMappingURL=executeTree.js.map |
export { default as debounce } from './debounce'; | ||
export { default as wait } from './wait'; | ||
//# sourceMappingURL=index.js.map |
@@ -19,2 +19,3 @@ var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
import { FunctionTreeExecutionError } from './errors'; | ||
import { isPromise } from './utils'; | ||
@@ -60,9 +61,2 @@ /* | ||
/* | ||
If it walks like a duck and quacks like a duck... | ||
*/ | ||
function isPromise(result) { | ||
return result && typeof result.then === 'function' && typeof result.catch === 'function'; | ||
} | ||
var FunctionTreeExecution = function () { | ||
@@ -246,2 +240,3 @@ function FunctionTreeExecution(name, staticTree, functionTree, errorCallback) { | ||
var contextProviders = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
@@ -254,2 +249,5 @@ _classCallCheck(this, FunctionTree); | ||
_this.cachedStaticTrees = []; | ||
_this.executeBranchWrapper = options.executeBranchWrapper || function (cb) { | ||
cb(); | ||
}; | ||
@@ -324,3 +322,3 @@ if ((typeof contextProviders === 'undefined' ? 'undefined' : _typeof(contextProviders)) !== 'object' || contextProviders === null || Array.isArray(contextProviders)) { | ||
_this2.emit('start', execution, payload); | ||
executeTree(execution, payload, function (funcDetails, path, currentPayload) { | ||
executeTree(execution, payload, _this2.executeBranchWrapper, function (funcDetails, path, currentPayload) { | ||
_this2.emit('pathStart', path, execution, funcDetails, currentPayload); | ||
@@ -327,0 +325,0 @@ }, function (currentPayload) { |
import { Sequence, Parallel } from './primitives'; | ||
export { FunctionTreeExecutionError, FunctionTreeError } from './errors'; | ||
export { Primitive } from './primitives'; | ||
export { default as createStaticTree } from './staticTree'; | ||
export { default as Path } from './Path'; | ||
@@ -5,0 +8,0 @@ export function sequence() { |
@@ -18,3 +18,4 @@ var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
path: this.path, | ||
payload: this.payload | ||
payload: this.payload, | ||
_functionTreePath: true | ||
}; | ||
@@ -21,0 +22,0 @@ } |
@@ -7,2 +7,4 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
import { isPromise } from './utils'; | ||
var Provider = function () { | ||
@@ -12,7 +14,12 @@ function Provider(definition) { | ||
_ref$wrap = _ref.wrap, | ||
wrap = _ref$wrap === undefined ? true : _ref$wrap; | ||
wrap = _ref$wrap === undefined ? true : _ref$wrap, | ||
_ref$ignoreDefinition = _ref.ignoreDefinition, | ||
ignoreDefinition = _ref$ignoreDefinition === undefined ? false : _ref$ignoreDefinition; | ||
_classCallCheck(this, Provider); | ||
this.verifyDefinition(definition); | ||
if (!ignoreDefinition) { | ||
this.verifyDefinition(definition); | ||
} | ||
this.wrap = wrap; | ||
@@ -29,6 +36,8 @@ | ||
}; | ||
this.WrappedProviderConstructor.prototype = Object.keys(definition).reduce(function (wrappedProvider, key) { | ||
this.WrappedProviderConstructor.prototype = Object.keys(ignoreDefinition ? {} : definition).reduce(function (wrappedProvider, key) { | ||
var originalFunc = definition[key]; | ||
wrappedProvider[key] = function () { | ||
var _this = this; | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
@@ -38,8 +47,34 @@ args[_key] = arguments[_key]; | ||
var providerResult = originalFunc.apply(this, args); | ||
if (isPromise(providerResult)) { | ||
return providerResult.then(function (result) { | ||
_this.context.debugger.send({ | ||
method: _this.providerName + '.' + key, | ||
args: args, | ||
isPromise: true, | ||
isRejected: false, | ||
returnValue: result | ||
}); | ||
return result; | ||
}).catch(function (error) { | ||
_this.context.debugger.send({ | ||
method: _this.providerName + '.' + key, | ||
args: args, | ||
isPromise: true, | ||
isRejected: true | ||
}); | ||
throw error; | ||
}); | ||
} | ||
this.context.debugger.send({ | ||
method: this.providerName + '.' + key, | ||
args: args | ||
args: args, | ||
returnValue: providerResult | ||
}); | ||
return originalFunc.apply(this, args); | ||
return providerResult; | ||
}; | ||
@@ -54,2 +89,6 @@ | ||
value: function verifyDefinition(definition) { | ||
if (this.ignoreDefinition) { | ||
return; | ||
} | ||
if ((typeof definition === 'undefined' ? 'undefined' : _typeof(definition)) !== 'object' || definition === null) { | ||
@@ -56,0 +95,0 @@ throw new Error('The definition passed as Provider is not valid'); |
@@ -29,3 +29,3 @@ var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
if (index > 0 && currentValue === undefined) { | ||
throw new Error('A tag is extracting with path "' + path + '", but it is not valid.'); | ||
throw new Error('Cannot extract value at path "' + path + '" ("' + key + '" is not defined).'); | ||
} | ||
@@ -32,0 +32,0 @@ |
@@ -1,4 +0,4 @@ | ||
import { FunctionTreeExecutable } from '../' | ||
import { FunctionTree } from '../' | ||
export interface DebounceFunction extends FunctionTreeExecutable { | ||
export interface DebounceFunction extends FunctionTree { | ||
displayName: string | ||
@@ -13,2 +13,8 @@ (context: { path: string }): Promise<any> | ||
export const debounce: Debounce | ||
export const debounce: Debounce | ||
export interface Wait { | ||
(time: number): FunctionTree | ||
} | ||
export const wait: Wait |
188
index.d.ts
import { EventEmitter } from 'eventemitter3' | ||
interface Payload { | ||
[key: string]: any | ||
[key: number]: any | ||
} | ||
export interface IPath { | ||
path: string | ||
payload: Payload | ||
} | ||
export class Path implements IPath { | ||
path: string | ||
payload: Payload | ||
constructor(path: string, payload: Payload) | ||
toJS(): IPath | ||
} | ||
export class Abort { | ||
constructor(payload: Payload) | ||
} | ||
export function createStaticTree(tree: Sequence | Parallel | FunctionTreePrimitive | Array<Function | Sequence | Parallel>): Array<FunctionTreePrimitive> | ||
type RunFunctionResolve = (funcDetails: FunctionTreePrimitive, payload: Payload, prevPayload: Payload, next: any) => void | ||
type BranchStartCallback = (funcDetails: FunctionTreePrimitive, path: string, payload: Payload) => void | ||
type BranchEndCallback = (payload: Payload) => void | ||
type ParallelStartEndCallback = (payload: Payload, itemLength: number) => void | ||
type ParallelProgressCallback = (payload: Payload, remainingLength: number) => void | ||
export function executeTree( | ||
tree: Array<FunctionTreePrimitive>, | ||
resolveFunctionResult: RunFunctionResolve, | ||
initialPayload: Payload, | ||
branchStart: BranchStartCallback, | ||
branchEnd: BranchEndCallback, | ||
parallelStart: ParallelStartEndCallback, | ||
parallelProgress: ParallelProgressCallback, | ||
parallelEnd: ParallelStartEndCallback, | ||
end: BranchEndCallback | ||
): void | ||
export interface FunctionTreePrimitive { | ||
export class Primitive { | ||
name?: string | ||
"function": Function | ||
function: Function | ||
functionIndex: number | ||
items: Array<FunctionTreePrimitive> | ||
items: Array<Primitive> | ||
type: "parallel" | "sequence" | ||
_functionTreePrimitive: boolean | ||
outputs?: { [name: string]: FunctionTreePrimitive } | ||
outputs?: { [name: string]: Primitive } | ||
} | ||
export interface FunctionTreeExecutable { | ||
type TPath = { | ||
[key: string]: TFunctionTreeExecutable | ||
} | ||
export class Sequence implements FunctionTreeExecutable { | ||
constructor(items: Array<FunctionTreePrimitive>) | ||
constructor(name: string, items: Array<FunctionTreePrimitive>) | ||
toJSON(): FunctionTreePrimitive | ||
} | ||
type TSequence = Array<Primitive | Function | TPath> | ||
export class Parallel implements FunctionTreeExecutable { | ||
constructor(items: Array<FunctionTreePrimitive>) | ||
constructor(name: string, items: Array<FunctionTreePrimitive>) | ||
export type TFunctionTreeExecutable = Primitive | Function | TPath | TSequence | ||
toJSON(): FunctionTreePrimitive | ||
interface IPayload { | ||
[key: string]: any | ||
[key: number]: any | ||
} | ||
export function sequence(items: Array<FunctionTreeExecutable>): Sequence | ||
export function sequence(name: string, items: Array<FunctionTreeExecutable>): Sequence | ||
export function parallel(items: Array<FunctionTreeExecutable>): Parallel | ||
export function parallel(name: string, items: Array<FunctionTreeExecutable>): Parallel | ||
export interface RunTreeFunction { | ||
(): void | ||
on(event: string | symbol, listener: Function): this | ||
once(event: string | symbol, listener: Function): this | ||
off(event: string | symbol, listener: Function): this | ||
export interface IPath { | ||
path: string | ||
payload: IPayload | ||
} | ||
export interface DevtoolsOptions { | ||
host: string | ||
type TContextProviders = { | ||
[key: string]: Provider | ||
} | ||
export class Devtools { | ||
constructor(options: DevtoolsOptions) | ||
addListeners(): void | ||
init(): void | ||
safeStringify(object: any): string | ||
reInit(): void | ||
sendMessage(message: string): void | ||
watchExecution(tree: FunctionTree): void | ||
sendInitial(): void | ||
createExecutionMessage(debuggingData: any, context: any, functionDetails: FunctionTreePrimitive, payload: Payload): string | ||
sendExecutionData(debuggingData: any, context: any, functionDetails: FunctionTreePrimitive, payload: Payload): void | ||
Provider(): (context: any, functionDetails: FunctionTreePrimitive, payload: Payload) => any | ||
export class FunctionTree extends EventEmitter { | ||
constructor(contextProviders?: TContextProviders) | ||
run(sequence: TFunctionTreeExecutable, payload?: IPayload): Promise<any> | ||
} | ||
export class FunctionTreeError extends Error { | ||
constructor(error: any) | ||
toJSON(): any | ||
export interface IBaseContext { | ||
[providerName: string]: any | ||
resolve: IResolve | ||
} | ||
export class FunctionTreeExecutionError extends FunctionTreeError { | ||
constructor(execution: any, funcDetails: any, payload: any, error: any) | ||
toJSON(): any | ||
export interface IContext<T={}> extends IBaseContext { | ||
props: T | ||
} | ||
export type Provider = (context: any, funcDetails: FunctionTreePrimitive, payload: Payload, next: Payload) => any | ||
type TTagFactory<T> = (path: TemplateStringsArray | string[], ...values: any[]) => Tag<T> | ||
export function ReduxProvider(store: any): Provider | ||
export function PropsProvider(): Provider | ||
export function PathProvider(): Provider | ||
export function ExecutionProvider(execution: any, abort: Abort): Provider | ||
export function ContextProvider(extendedContext: any): Provider | ||
export function createTemplateTag<T=any>(tagName: string, getValue: (path: string, context: IBaseContext) => T): TTagFactory<T> | ||
export class FunctionTree extends EventEmitter { | ||
constructor(contextProviders: Array<Provider>) | ||
cachedTrees: Array<FunctionTreePrimitive> | ||
contextProviders: Array<Provider> | ||
runTree: RunTreeFunction | ||
export function extractValueWithPath<T=any>(obj: any, path: string): T | ||
createContext(funcDetails: FunctionTreePrimitive, payload: Payload, prevPayload: Payload): Array<Provider> | ||
run(...args: any[]): Promise<any> | ||
export function resolveObject<T=any>(obj: any): ResolveValue<T> | ||
export class Provider<IContext = {}> { | ||
constructor (definition: { [key: string]: (this: { context: IContext }, ...args : any[]) => void }) | ||
} | ||
// ========= Contextual value | ||
export interface BaseContext { | ||
[providerName: string]: any | ||
// Keys set to 'any' type are not defined so that they | ||
// can be overriden in generic type. | ||
// execution: any | ||
// path: any | ||
// props: any | ||
resolve: Resolve | ||
export class Path implements IPath { | ||
path: string | ||
payload: IPayload | ||
constructor(path: string, payload: IPayload) | ||
toJS(): IPath | ||
} | ||
export type Context<T={}> = BaseContext & T | ||
export function sequence(items: Array<TFunctionTreeExecutable>): Primitive | ||
export function sequence(name: string, items: Array<TFunctionTreeExecutable>): Primitive | ||
export function parallel(items: Array<TFunctionTreeExecutable>): Primitive | ||
export function parallel(name: string, items: Array<TFunctionTreeExecutable>): Primitive | ||
export abstract class ResolveValue<T=any, C={}> { | ||
abstract getValue(context: Context<C>): T | ||
} | ||
export class ResolveValue<T=any> {} | ||
interface TagOptions { | ||
hasValue?: boolean | ||
isStateDependency?: boolean | ||
} | ||
export class Tag<T=any> extends ResolveValue<T> { | ||
constructor( | ||
tag: string, | ||
options: TagOptions, | ||
strings: string[], | ||
values: any[] | ||
) | ||
options: TagOptions | ||
strings: string[] | ||
type: string | ||
values: any[] | ||
constructor() | ||
getPath(getters: any): string | ||
@@ -172,6 +81,5 @@ getValue(getters: any): T | ||
export interface Resolve { | ||
export interface IResolve { | ||
isTag(arg: any, ...types: string[]): arg is Tag | ||
isResolveValue(arg: any): arg is ResolveValue | ||
path(tag: Tag): string | ||
@@ -182,10 +90,2 @@ value<T>(value: Tag<T>, overrideContext?: any): T | ||
value<T=any>(value: ResolveValue<T> | T, overrideContext?: any): T | ||
} | ||
// ========= Helpers | ||
export function extractValueWithPath<T=any>(obj: any, path: string): T | ||
type TagFactory<T> = (path: TemplateStringsArray | string[], ...values: any[]) => Tag<T> | ||
export function createTemplateTag<T=any>(tagName: string, getValue: (path: string, context: BaseContext) => T): TagFactory<T> | ||
export function resolveObject<T=any>(obj: any): ResolveValue<T> | ||
} |
@@ -6,6 +6,15 @@ 'use strict'; | ||
}); | ||
exports.Devtools = undefined; | ||
exports.Devtools = exports.DevtoolsBase = undefined; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _base = require('./base'); | ||
Object.defineProperty(exports, 'DevtoolsBase', { | ||
enumerable: true, | ||
get: function get() { | ||
return _interopRequireDefault(_base).default; | ||
} | ||
}); | ||
var _Provider2 = require('../Provider'); | ||
@@ -15,4 +24,2 @@ | ||
var _base = require('./base'); | ||
var _base2 = _interopRequireDefault(_base); | ||
@@ -43,3 +50,3 @@ | ||
_this.latestExecutionId = null; | ||
_this.version = "4.0.0-1514099265786"; | ||
_this.version = "4.0.0-1519463520217"; | ||
_this.init(); | ||
@@ -46,0 +53,0 @@ return _this; |
@@ -18,73 +18,75 @@ 'use strict'; | ||
*/ | ||
function executeTree(execution, initialPayload, branchStart, branchEnd, parallelStart, parallelProgress, parallelEnd, end) { | ||
function executeTree(execution, initialPayload, executeBranchWrapper, branchStart, branchEnd, parallelStart, parallelProgress, parallelEnd, end) { | ||
function runBranch(branch, index, payload, prevPayload, nextBranch) { | ||
function runNextItem(result) { | ||
runBranch(branch, index + 1, result, payload, nextBranch); | ||
} | ||
executeBranchWrapper(function () { | ||
function runNextItem(result) { | ||
runBranch(branch, index + 1, result, payload, nextBranch); | ||
} | ||
function processFunctionOutput(funcDetails, outputResult) { | ||
return function (result) { | ||
var newPayload = Object.assign({}, payload, result ? result.payload : {}); | ||
function processFunctionOutput(funcDetails, outputResult) { | ||
return function (result) { | ||
var newPayload = Object.assign({}, payload, result ? result.payload : {}); | ||
if (result && funcDetails.outputs) { | ||
var outputs = Object.keys(funcDetails.outputs); | ||
if (result && funcDetails.outputs) { | ||
var outputs = Object.keys(funcDetails.outputs); | ||
if (~outputs.indexOf(result.path)) { | ||
branchStart(funcDetails, result.path, newPayload); | ||
runBranch(funcDetails.outputs[result.path].items, 0, newPayload, payload, outputResult); | ||
if (~outputs.indexOf(result.path)) { | ||
branchStart(funcDetails, result.path, newPayload); | ||
runBranch(funcDetails.outputs[result.path].items, 0, newPayload, payload, outputResult); | ||
} else { | ||
throw new _errors.FunctionTreeExecutionError(execution, funcDetails, payload, 'function ' + funcDetails.name + ' must use one of its possible outputs: ' + outputs.join(', ') + '.'); | ||
} | ||
} else { | ||
throw new _errors.FunctionTreeExecutionError(execution, funcDetails, payload, 'function ' + funcDetails.name + ' must use one of its possible outputs: ' + outputs.join(', ') + '.'); | ||
outputResult(newPayload); | ||
} | ||
} else { | ||
outputResult(newPayload); | ||
}; | ||
} | ||
var currentItem = branch[index]; | ||
if (!currentItem) { | ||
if (branch !== execution.staticTree) { | ||
branchEnd(payload); | ||
} | ||
}; | ||
} | ||
nextBranch(payload); | ||
} else if (isPrimitive(currentItem, 'sequence')) { | ||
runBranch(currentItem.items, 0, payload, prevPayload, runNextItem); | ||
} else if (isPrimitive(currentItem, 'parallel')) { | ||
var itemLength = currentItem.items.length; | ||
var payloads = []; | ||
var currentItem = branch[index]; | ||
parallelStart(payload, itemLength); | ||
currentItem.items.forEach(function (func, index) { | ||
if (func.function) { | ||
execution.runFunction(func, payload, prevPayload, processFunctionOutput(func, function (payload) { | ||
payloads.push(payload); | ||
if (payloads.length === itemLength) { | ||
parallelEnd(payload, itemLength); | ||
runNextItem(Object.assign.apply(Object, [{}].concat(payloads))); | ||
} else { | ||
parallelProgress(payload, itemLength - payloads.length); | ||
} | ||
})); | ||
} else { | ||
runBranch(func.items, 0, payload, prevPayload, function (payload) { | ||
payloads.push(payload); | ||
if (payloads.length === itemLength) { | ||
parallelEnd(payload, itemLength); | ||
runNextItem(Object.assign.apply(Object, [{}].concat(payloads))); | ||
} else { | ||
parallelProgress(payload, itemLength - payloads.length); | ||
} | ||
}); | ||
} | ||
if (!currentItem) { | ||
if (branch !== execution.staticTree) { | ||
branchEnd(payload); | ||
return payloads; | ||
}); | ||
} else { | ||
execution.runFunction(currentItem, payload, prevPayload, processFunctionOutput(currentItem, runNextItem)); | ||
} | ||
nextBranch(payload); | ||
} else if (isPrimitive(currentItem, 'sequence')) { | ||
runBranch(currentItem.items, 0, payload, prevPayload, runNextItem); | ||
} else if (isPrimitive(currentItem, 'parallel')) { | ||
var itemLength = currentItem.items.length; | ||
var payloads = []; | ||
parallelStart(payload, itemLength); | ||
currentItem.items.forEach(function (func, index) { | ||
if (func.function) { | ||
execution.runFunction(func, payload, prevPayload, processFunctionOutput(func, function (payload) { | ||
payloads.push(payload); | ||
if (payloads.length === itemLength) { | ||
parallelEnd(payload, itemLength); | ||
runNextItem(Object.assign.apply(Object, [{}].concat(payloads))); | ||
} else { | ||
parallelProgress(payload, itemLength - payloads.length); | ||
} | ||
})); | ||
} else { | ||
runBranch(func.items, 0, payload, prevPayload, function (payload) { | ||
payloads.push(payload); | ||
if (payloads.length === itemLength) { | ||
parallelEnd(payload, itemLength); | ||
runNextItem(Object.assign.apply(Object, [{}].concat(payloads))); | ||
} else { | ||
parallelProgress(payload, itemLength - payloads.length); | ||
} | ||
}); | ||
} | ||
return payloads; | ||
}); | ||
} else { | ||
execution.runFunction(currentItem, payload, prevPayload, processFunctionOutput(currentItem, runNextItem)); | ||
} | ||
}); | ||
} | ||
return runBranch([execution.staticTree], 0, initialPayload, null, end); | ||
runBranch([execution.staticTree], 0, initialPayload, null, end); | ||
} | ||
//# sourceMappingURL=executeTree.js.map |
@@ -16,3 +16,12 @@ 'use strict'; | ||
var _wait = require('./wait'); | ||
Object.defineProperty(exports, 'wait', { | ||
enumerable: true, | ||
get: function get() { | ||
return _interopRequireDefault(_wait).default; | ||
} | ||
}); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
//# sourceMappingURL=index.js.map |
@@ -40,2 +40,4 @@ 'use strict'; | ||
var _utils = require('./utils'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -88,9 +90,2 @@ | ||
/* | ||
If it walks like a duck and quacks like a duck... | ||
*/ | ||
function isPromise(result) { | ||
return result && typeof result.then === 'function' && typeof result.catch === 'function'; | ||
} | ||
var FunctionTreeExecution = function () { | ||
@@ -145,3 +140,3 @@ function FunctionTreeExecution(name, staticTree, functionTree, errorCallback) { | ||
*/ | ||
if (isPromise(result)) { | ||
if ((0, _utils.isPromise)(result)) { | ||
functionTree.emit('asyncFunction', execution, funcDetails, payload, result); | ||
@@ -275,2 +270,3 @@ this.isAsync = true; | ||
var contextProviders = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
@@ -283,2 +279,5 @@ _classCallCheck(this, FunctionTree); | ||
_this.cachedStaticTrees = []; | ||
_this.executeBranchWrapper = options.executeBranchWrapper || function (cb) { | ||
cb(); | ||
}; | ||
@@ -353,3 +352,3 @@ if ((typeof contextProviders === 'undefined' ? 'undefined' : _typeof(contextProviders)) !== 'object' || contextProviders === null || Array.isArray(contextProviders)) { | ||
_this2.emit('start', execution, payload); | ||
(0, _executeTree2.default)(execution, payload, function (funcDetails, path, currentPayload) { | ||
(0, _executeTree2.default)(execution, payload, _this2.executeBranchWrapper, function (funcDetails, path, currentPayload) { | ||
_this2.emit('pathStart', path, execution, funcDetails, currentPayload); | ||
@@ -356,0 +355,0 @@ }, function (currentPayload) { |
@@ -6,3 +6,3 @@ 'use strict'; | ||
}); | ||
exports.Tag = exports.createTemplateTag = exports.ResolveValue = exports.resolveObject = exports.extractValueWithPath = exports.Provider = exports.default = exports.FunctionTree = exports.FunctionTreeError = exports.FunctionTreeExecutionError = undefined; | ||
exports.Tag = exports.createTemplateTag = exports.ResolveValue = exports.resolveObject = exports.extractValueWithPath = exports.Provider = exports.default = exports.FunctionTree = exports.Path = exports.createStaticTree = exports.Primitive = exports.FunctionTreeError = exports.FunctionTreeExecutionError = undefined; | ||
@@ -23,2 +23,29 @@ var _errors = require('./errors'); | ||
}); | ||
var _primitives = require('./primitives'); | ||
Object.defineProperty(exports, 'Primitive', { | ||
enumerable: true, | ||
get: function get() { | ||
return _primitives.Primitive; | ||
} | ||
}); | ||
var _staticTree = require('./staticTree'); | ||
Object.defineProperty(exports, 'createStaticTree', { | ||
enumerable: true, | ||
get: function get() { | ||
return _interopRequireDefault(_staticTree).default; | ||
} | ||
}); | ||
var _Path = require('./Path'); | ||
Object.defineProperty(exports, 'Path', { | ||
enumerable: true, | ||
get: function get() { | ||
return _interopRequireDefault(_Path).default; | ||
} | ||
}); | ||
exports.sequence = sequence; | ||
@@ -87,4 +114,2 @@ exports.parallel = parallel; | ||
var _primitives = require('./primitives'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -91,0 +116,0 @@ |
@@ -24,3 +24,4 @@ "use strict"; | ||
path: this.path, | ||
payload: this.payload | ||
payload: this.payload, | ||
_functionTreePath: true | ||
}; | ||
@@ -27,0 +28,0 @@ } |
@@ -11,2 +11,4 @@ 'use strict'; | ||
var _utils = require('./utils'); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -18,7 +20,12 @@ | ||
_ref$wrap = _ref.wrap, | ||
wrap = _ref$wrap === undefined ? true : _ref$wrap; | ||
wrap = _ref$wrap === undefined ? true : _ref$wrap, | ||
_ref$ignoreDefinition = _ref.ignoreDefinition, | ||
ignoreDefinition = _ref$ignoreDefinition === undefined ? false : _ref$ignoreDefinition; | ||
_classCallCheck(this, Provider); | ||
this.verifyDefinition(definition); | ||
if (!ignoreDefinition) { | ||
this.verifyDefinition(definition); | ||
} | ||
this.wrap = wrap; | ||
@@ -35,6 +42,8 @@ | ||
}; | ||
this.WrappedProviderConstructor.prototype = Object.keys(definition).reduce(function (wrappedProvider, key) { | ||
this.WrappedProviderConstructor.prototype = Object.keys(ignoreDefinition ? {} : definition).reduce(function (wrappedProvider, key) { | ||
var originalFunc = definition[key]; | ||
wrappedProvider[key] = function () { | ||
var _this = this; | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
@@ -44,8 +53,34 @@ args[_key] = arguments[_key]; | ||
var providerResult = originalFunc.apply(this, args); | ||
if ((0, _utils.isPromise)(providerResult)) { | ||
return providerResult.then(function (result) { | ||
_this.context.debugger.send({ | ||
method: _this.providerName + '.' + key, | ||
args: args, | ||
isPromise: true, | ||
isRejected: false, | ||
returnValue: result | ||
}); | ||
return result; | ||
}).catch(function (error) { | ||
_this.context.debugger.send({ | ||
method: _this.providerName + '.' + key, | ||
args: args, | ||
isPromise: true, | ||
isRejected: true | ||
}); | ||
throw error; | ||
}); | ||
} | ||
this.context.debugger.send({ | ||
method: this.providerName + '.' + key, | ||
args: args | ||
args: args, | ||
returnValue: providerResult | ||
}); | ||
return originalFunc.apply(this, args); | ||
return providerResult; | ||
}; | ||
@@ -60,2 +95,6 @@ | ||
value: function verifyDefinition(definition) { | ||
if (this.ignoreDefinition) { | ||
return; | ||
} | ||
if ((typeof definition === 'undefined' ? 'undefined' : _typeof(definition)) !== 'object' || definition === null) { | ||
@@ -62,0 +101,0 @@ throw new Error('The definition passed as Provider is not valid'); |
@@ -37,3 +37,3 @@ 'use strict'; | ||
if (index > 0 && currentValue === undefined) { | ||
throw new Error('A tag is extracting with path "' + path + '", but it is not valid.'); | ||
throw new Error('Cannot extract value at path "' + path + '" ("' + key + '" is not defined).'); | ||
} | ||
@@ -40,0 +40,0 @@ |
{ | ||
"name": "function-tree", | ||
"version": "4.0.0-1514099265786", | ||
"version": "4.0.0-1519463520217", | ||
"description": "When a function is not enough", | ||
@@ -11,6 +11,7 @@ "main": "lib/index.js", | ||
"test": "cross-env BABEL_ENV=test mocha --compilers js:babel-register \"src/**/*.test.js\" \"__test__/**/*.test.js\"", | ||
"test:ts": "cross-env mocha --require ts-runtime.js \"src/**/*.test.ts\" \"__test__/**/*.test.ts\"", | ||
"test:watch": "npm run test -- --watch", | ||
"build:cjs": "cross-env BABEL_ENV=cjs babel src/ --out-dir=lib/ -s", | ||
"build:es": "cross-env BABEL_ENV=es babel src/ --out-dir=es/ -s", | ||
"build": "run-p build:*", | ||
"build": "tsc && run-p build:*", | ||
"coverage": "nyc --reporter=lcov --reporter=json npm run test", | ||
@@ -26,2 +27,4 @@ "prepublish": "npm run build" | ||
"devDependencies": { | ||
"@types/assert": "0.0.31", | ||
"@types/mocha": "^2.2.47", | ||
"mock-socket": "6.0.4" | ||
@@ -28,0 +31,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
350515
96
3450
3