function-tree
Advanced tools
Comparing version 0.1.0 to 0.2.0
{ | ||
"name": "function-tree", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "When a function is not enough", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -22,8 +22,8 @@ module.exports = function (options) { | ||
var data = { | ||
name: context._instance.name, | ||
executionId: context._instance.id, | ||
name: context.execution.name, | ||
executionId: context.execution.id, | ||
functionIndex: functionDetails.functionIndex, | ||
staticTree: context._instance.staticTree, | ||
staticTree: context.execution.staticTree, | ||
payload: payload, | ||
datetime: context._instance.datetime, | ||
datetime: context.execution.datetime, | ||
data: debuggingData | ||
@@ -30,0 +30,0 @@ }; |
@@ -70,13 +70,3 @@ var chalk = require('chalk') | ||
function send(debuggingData, context, functionDetails, payload) { | ||
/* | ||
name: context._instance.name, | ||
functionTreeId: context._instance.id, | ||
executionId: context._instance.executionId, | ||
functionIndex: functionDetails.functionIndex, | ||
staticTree: context._instance.staticTree, | ||
payload: payload, | ||
datetime: context._instance.datetime, | ||
data: debuggingData | ||
*/ | ||
var id = context._instance.id + '_' + context._instance.executionId | ||
var id = context.execution.id + '_' + context.execution.executionId | ||
if (registeredFunctionTrees[id] && registeredFunctionTrees[id].functions[functionDetails.functionIndex]) { | ||
@@ -91,3 +81,3 @@ registeredFunctionTrees[id].functions[functionDetails.functionIndex].data.push(debuggingData) | ||
registeredFunctionTrees[id] = { | ||
staticTree: context._instance.staticTree, | ||
staticTree: context.execution.staticTree, | ||
functions: [{ | ||
@@ -104,3 +94,3 @@ payload: payload, | ||
console.log([ | ||
chalk.bgWhite.black.bold(padded(context._instance.name || context._instance.id)), | ||
chalk.bgWhite.black.bold(padded(context.execution.name || context.execution.id)), | ||
].concat(traverseFunctionTree(registeredFunctionTrees[id])).join('\n')); | ||
@@ -107,0 +97,0 @@ registeredFunctionTrees[id].isBatching = false |
@@ -8,3 +8,3 @@ # function-tree | ||
Callback hell is a common term which says something about how asynchronous code execution affects readability and maintainability of our code bases. Even with promises we get into trouble when we have conditional execution flow. Callbacks and promises aside, the testability and reusability of code is also an important factor which is difficult to achieve in general. | ||
Callback hell is a common term which says something about how asynchronous code execution affects readability and maintainability of our code bases. Even with promises we can get readability issues with conditional execution flow. Callbacks and promises aside, the testability and reusability of code is also an important factor which is difficult to achieve in general. | ||
@@ -15,2 +15,9 @@ A function tree will help you execute synchronous and asynchronous functions in a declarative, composable and testable way. **Declarative** means that you can describe an execution without writing any implementation, increasing readability of the code. **Composable** means that some part of one execution can be reused in an other execution. And **testable** means that you will write your code in a way where the whole chain of execution and its individual parts can be tested. | ||
### Demo | ||
Install the [chrome extension debugger](https://chrome.google.com/webstore/detail/function-tree-debugger/ppfbmcnapdgakfiocieggdgbhmlalgjp). Clone this repo, install deps and run: | ||
`npm run demo:redux` | ||
More demos coming soon... | ||
### A small example | ||
@@ -92,4 +99,4 @@ A typical function with side effects. | ||
import FunctionTree from 'function-tree' | ||
import DebuggerProvider from 'function-tree/providers/DebuggerProvider' | ||
import ContextProvider from 'function-tree/providers/ContextProvider' | ||
import DebuggerProvider from 'function-tree/providers/Debugger' | ||
import ContextProvider from 'function-tree/providers/Context' | ||
import request from 'request' | ||
@@ -232,5 +239,6 @@ | ||
}) | ||
``` | ||
The really good thing about asynchronous testing with a `function-tree` is that any async side effect returns a promise, meaning that we do not care about the side effect itself. Any async side effect can be mocked with a simple promise, like you see on the **request.get** above. If you do care about the side effect though you can still insert it as normal on the context. | ||
### API | ||
@@ -301,2 +309,33 @@ | ||
#### Retry (recursive) | ||
You can also retry execution of the tree at any point. Even with async functions running. For example: | ||
```js | ||
import execute from './execute' | ||
function funcA(context) { | ||
return new Promise(resolve => { | ||
setTimeout(resolve, 500) | ||
}) | ||
} | ||
function funcB(context) { | ||
if (context.input.retryCount < 3) { | ||
return context.execution.retry({ | ||
retryCount: context.input.retryCount + 1 | ||
}) | ||
} | ||
} | ||
const tree = [ | ||
funcA, | ||
funcB | ||
] | ||
execute(tree, { | ||
retryCount: 0 | ||
}) | ||
``` | ||
#### Providers | ||
@@ -320,6 +359,7 @@ A provider gives you access to the current context and other information about the execution. It is required that you return the context or a mutated version of it. | ||
context._instance.name // Function tree id | ||
context._instance.executionId // Current execution id | ||
context._instance.staticTree // The static representation of the tree | ||
context._instance.datetime // Time of execution | ||
context.execution.name // Function tree id | ||
context.execution.id // Current execution id | ||
context.execution.staticTree // The static representation of the tree | ||
context.execution.datetime // Time of execution | ||
context.execution.functionTree // The function tree instance | ||
@@ -437,2 +477,10 @@ return context // Always return the changed context | ||
When you execute trees you can name the execution: | ||
```js | ||
execute('thisHappened', someTree) | ||
``` | ||
And that will be used in the debugger instead of execution id. | ||
#### NodeDebugger (optional provider) | ||
@@ -439,0 +487,0 @@ |
@@ -6,3 +6,3 @@ 'use strict' | ||
const createStaticTree = require('./staticTree') | ||
const InstanceProvider = require('../providers/Instance') | ||
const ExecutionProvider = require('../providers/Execution') | ||
const InputProvider = require('../providers/Input') | ||
@@ -85,3 +85,3 @@ const ResultProvider = require('../providers/Result') | ||
return [ | ||
InstanceProvider(this), | ||
ExecutionProvider(this), | ||
InputProvider(), | ||
@@ -160,6 +160,2 @@ ResultProvider() | ||
FunctionTree.prototype.nextExecutionId = function() { | ||
return this.currentExecutionId++ | ||
} | ||
module.exports = FunctionTree |
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
63914
1575
524