function-tree
Advanced tools
Comparing version 0.3.3 to 0.3.4
{ | ||
"name": "function-tree", | ||
"version": "0.3.3", | ||
"version": "0.3.4", | ||
"description": "When a function is not enough", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -71,13 +71,27 @@ var chalk = require('chalk') | ||
var id = context.execution.id + '_' + context.execution.executionId | ||
var prevFunction = ( | ||
registeredFunctionTrees[id] && | ||
registeredFunctionTrees[id].functions[registeredFunctionTrees[id].functions.length - 1] | ||
) | ||
var isExistingFunction = Boolean(prevFunction && prevFunction.functionIndex === functionDetails.functionIndex) | ||
if (registeredFunctionTrees[id] && registeredFunctionTrees[id].functions[functionDetails.functionIndex]) { | ||
registeredFunctionTrees[id].functions[functionDetails.functionIndex].data.push(debuggingData) | ||
} else if (isExistingFunction) { | ||
prevFunction.data = prevFunction.data.concat(functionDetails.data) | ||
} else if (registeredFunctionTrees[id]) { | ||
registeredFunctionTrees[id].functions[functionDetails.functionIndex] = { | ||
registeredFunctionTrees[id].functions.push({ | ||
functionIndex: functionDetails.functionIndex, | ||
outputs: functionDetails.outputs, | ||
payload: payload, | ||
data: [] | ||
} | ||
}) | ||
} else { | ||
registeredFunctionTrees[id] = { | ||
logLevel: 0, | ||
staticTree: context.execution.staticTree, | ||
functions: [{ | ||
functionIndex: functionDetails.functionIndex, | ||
outputs: functionDetails.outputs, | ||
payload: payload, | ||
@@ -89,10 +103,40 @@ data: [] | ||
if (!registeredFunctionTrees[id].isBatching) { | ||
registeredFunctionTrees[id].isBatching = true | ||
setTimeout(function () { | ||
console.log([ | ||
chalk.bgWhite.black.bold(padded(context.execution.name || context.execution.id)), | ||
].concat(traverseFunctionTree(registeredFunctionTrees[id])).join('\n')); | ||
registeredFunctionTrees[id].isBatching = false | ||
}) | ||
if (isExistingFunction) { | ||
var data = prevFunction.data[prevFunction.data.length - 1] | ||
var args = data.args || [] | ||
console.log.apply(console, | ||
[padded(chalk[data.color || 'white'](data.method), registeredFunctionTrees[id].logLevel)].concat( | ||
args.map(function (arg) { | ||
return padded(chalk.white(JSON.stringify(arg)), registeredFunctionTrees[id].logLevel) | ||
}) | ||
) | ||
) | ||
} else { | ||
if (registeredFunctionTrees[id].functions.length === 1) { | ||
console.log(chalk.bgWhite.black.bold(padded(context.execution.name || context.execution.id))) | ||
} | ||
if (prevFunction && prevFunction.outputs) { | ||
var chosenOutput = Object.keys(prevFunction.outputs).filter(function (outputKey) { | ||
if (prevFunction.outputs[outputKey].length && prevFunction.outputs[outputKey][0].functionIndex === functionDetails.functionIndex) { | ||
return true | ||
} | ||
return false | ||
})[0] | ||
console.log(padded(chalk.dim.underline.white(chosenOutput), registeredFunctionTrees[id].logLevel)) | ||
registeredFunctionTrees[id].logLevel++ | ||
} | ||
console.log(padded(chalk.underline.white(functionDetails.name), registeredFunctionTrees[id].logLevel)) | ||
console.log(padded( | ||
chalk.dim(JSON.stringify(payload || {})), | ||
registeredFunctionTrees[id].logLevel | ||
)) | ||
} | ||
@@ -99,0 +143,0 @@ } |
@@ -43,2 +43,3 @@ 'use strict' | ||
const result = funcDetails.function(context) | ||
if (result && result.then && result.catch && typeof result.then === 'function' && typeof result.catch === 'function') { | ||
@@ -50,2 +51,5 @@ result | ||
next(result.toJS()) | ||
} else if (funcDetails.outputs) { | ||
functionTree.emit('functionEnd', funcDetails, payload) | ||
throw new Error('The result ' + JSON.stringify(result) + ' from function ' + funcDetails.name + ' needs to be a path') | ||
} else if (isValidResult(result)) { | ||
@@ -70,2 +74,5 @@ functionTree.emit('functionEnd', funcDetails, payload) | ||
next(result.toJS()) | ||
} else if (funcDetails.outputs) { | ||
functionTree.emit('functionEnd', funcDetails, payload) | ||
throw new Error('The result ' + JSON.stringify(result) + ' from function ' + funcDetails.name + ' needs to be a path') | ||
} else if (isValidResult(result)) { | ||
@@ -86,2 +93,5 @@ functionTree.emit('functionEnd', funcDetails, payload) | ||
next(result.toJS()) | ||
} else if (funcDetails.outputs) { | ||
functionTree.emit('functionEnd', funcDetails, payload) | ||
throw new Error('The result ' + JSON.stringify(result) + ' from function ' + funcDetails.name + ' needs to be a path') | ||
} else if (isValidResult(result)) { | ||
@@ -88,0 +98,0 @@ functionTree.emit('functionEnd', funcDetails, payload) |
@@ -118,15 +118,11 @@ 'use strict' | ||
module.exports['should be able to reuse existing chain to define signals'] = (test) => { | ||
function actionA() { | ||
module.exports['should be able to reuse existing tree'] = (test) => { | ||
function actionA(context) { | ||
test.ok(true) | ||
return { | ||
path: 'success' | ||
} | ||
return context.path.success() | ||
} | ||
function actionB() { | ||
function actionB(context) { | ||
test.ok(true) | ||
return { | ||
path: 'success' | ||
} | ||
return context.path.success() | ||
} | ||
@@ -157,1 +153,21 @@ | ||
} | ||
module.exports['should throw when path and no path returned'] = (test) => { | ||
function actionA() { | ||
return { | ||
foo: 'bar' | ||
} | ||
} | ||
const execute = FunctionTree([]) | ||
const tree = [ | ||
actionA, { | ||
success: [] | ||
} | ||
] | ||
test.expect(1) | ||
test.throws(() => { | ||
execute(tree) | ||
}) | ||
test.done() | ||
} |
@@ -13,2 +13,3 @@ 'use strict' | ||
test.ok(context.path.error) | ||
return context.path.success() | ||
}, { | ||
@@ -42,2 +43,3 @@ success: [], | ||
test.ok(context.path.bar) | ||
return context.path.foo() | ||
}, { | ||
@@ -44,0 +46,0 @@ foo: [], |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
82628
2184