Flow: Track asynchronous application flow and access flow context variables
Sneak Peek
This library allows you to create custom asynchronous flow contexts and access its variables in every step of the flow execution.
Let's illustrate Flow functionality on a simple http server:
const Flow = ; // load Flow library let requestID = 0; const server = ; { Flow; // Set a new variable in our Flow let data = await Model; return data elapsed: process // get process.hrtime() from the current Flow (server request handler) requestID: Flow // get requestID incremented in the current Flow (server request handler) ;}
Table of Contents
Installing
npm install --save liqd-flow
Class: Flow
static start( callback[, scope = {}[, freeze = true]] )
Starts a new Flow and sets its scope object.
callback
{Function} Callback executed in a newly created Flowscope
{Object} Scope assigned to the Flow- defaults to {Object} empty object
{}
- defaults to {Object} empty object
freeze
{Boolean} If set to true scope variables are frozen to prevent changes in the Flow- defaults to {Boolean}
true
- defaults to {Boolean}
Flowstart { // Callback is executed inside Flow scope let foo = Flow; // returns 'bar';} foo: 'bar' ; // Sets the Flow scope
static set( key, value[, freeze = true] )
Sets value for the key in the current Flow if key is not frozen.
key
{Any} Keyvalue
{Any} Valuefreeze
{Boolean} If set to true value for key will be frozen to prevent changes in the Flow- defaults to {Boolean}
true
- defaults to {Boolean}
Returns {Boolean}
true
value has been set for the key which was not frozenfalse
key was frozen and variable have not been changed
Flow; // Sets the value inside Flow, returns trueFlow; // Rewrites the value, returns trueFlow; // Does not rewrite the value, returns false
static get( key[, default_value = undefined] )
Returns value for the key in the current Flow, default_value
if the key is not set.
key
{Any} Keydefault_value
{Any} Value returned if the key is not set- defaults to
undefined
- defaults to
Returns {Any}
let foo = Flow;
static getPath( path[, default_value = undefined[, path_delimiter = '.']] )
Returns value for the path in the current Flow, default_value
if the path is not set.
path
{Array of Strings | String} pathdefault_value
{Any} Value returned if the key is not set- defaults to
undefined
- defaults to
path_delimiter
{String} Path delimiter if path is {String}- defaults to
'.'
- defaults to
Returns {Any}
let foo = Flow;let bar = Flow;
static scope()
Returns the current scope of the Flow, object containing every key set in the Flow with its value and frozen state.
Returns {Object}
let scope = Flowscope; /* scope ={ [key] : { value, frozen }}*/
static save()
Returns the current Flow handle for later restoration.
Returns {FlowHandle}
let flow_handle = Flow;
static restore( FlowHandle, callback )
Restores the stored Flow and dispatches the callback. Flow can be restored by calling restore method on FlowHandle as well. Each FlowHandle can be restored only once - multiple restorations throws an Exception.
Flow;
flow_handle;
static bind( callback )
Binds the current Flow to callback function, callback can be executed outside of current flow yet the original Flow is automaticaly restored.
let callback; Flowstart { callback = Flow;} foo: 'bar' ; ; // Executes callback outside of its Flow