cycle-workflows
Provides Plugin-based workflow functionality for cycle.js applications.
Installation
npm i cycle-workflows --save
Scripts
NOTE: Make sure you've installed all dependencies using npm install
first.
To generate documentation: npm run doc
. This will create documentation in the
build/docs
folder.
To run unit tests: npm test
API
Workflow
Executes any WorkflowStep instances associated with the name of the Workflow instance, in dependency-safe order, with automatic rollback of executed steps if any steps fail. See the examples for more information.
Kind: global class
Emits: StepsChanged
Inherits: Plugin
- Workflow
- new Workflow(props)
- instance
- .asReadOnly() ⇒
Workflow
- .asObservable() ⇒
Rx.Observable
- .delete()
- .dispatch([args]) ⇒
Promise.<WorkflowResult>
- .asReadOnly() ⇒
- static
- .States :
Object
- .Errors :
Object
- .Events :
Object
- "Deleted" (instance)
- "Dispatched" (instance, arguments, promise)
- "StepsChanged" (instance, steps)
- .States :
new Workflow(props)
Param | Type | Description |
---|---|---|
props | Object |
A map of property names and values to associate with the workflow. The only required attribute is name , which will be used to subscribe to any Plugin instances targeting this Workflow instance. |
Example
var stepOne = name: 'step-one' filter: any: 'my-workflow' { return { // represents a long-running operation ; }; }); var stepTwo = name: 'step-two' after: 'step-one' filter: any: 'my-workflow' 'my-other-workflow' { return 'step two results'; }); var workflow = name: 'my-workflow';workflow ;
Workflow
workflow.asReadOnly() ⇒ Converts the Workflow into a version that cannot be deleted. Useful when you need to share a workflow without risking it being deleted.
Kind: instance method of Workflow
Returns: Workflow
- A readonly version of the Workflow instance.
Example
var myWorkflow = name: 'safe-workflow';return { return myWorkflow; // cannot be deleted };
Rx.Observable
workflow.asObservable() ⇒ Provides a shared stream consumers can subscribe to in order to receive subsequent dispatch results.
Kind: instance method of Workflow
Example
someWorkflow // the values returned by executed steps ;
workflow.delete()
Deletes the workflow and any associated streams. Trying to invoke any methods on a deleted workflow will cause an Error to be thrown.
Kind: instance method of Workflow
Emits: Deleted
Example
myWorkflow;myWorkflow; // throws Error
Promise.<WorkflowResult>
workflow.dispatch([args]) ⇒ Executes any enabled steps associated with this
Workflow instance, passing the dispatch arguments
to each step's execute
method and saving each
step's execute
result.
Kind: instance method of Workflow
Returns: Promise.<WorkflowResult>
- A promise that will never be
rejected. If a step's execute
method returns a
promise that never resolves, this promise will
also never resolve. Examine the resulting value's
status
member to determine whether the dispatch
ended successfully, failed, or was cancelled.
Emits: Dispatched
Param | Type | Description |
---|---|---|
[args] | * |
One or more arguments to pass to each step's execute method. |
Example
myWorkflow ;
Object
Workflow.States : Represents the state of the Workflow
dispatch result. Handlers attached
to the dispatch method should examine
the state
property to determine how
to process the results.
Kind: static property of Workflow
Properties
Name | Type | Description |
---|---|---|
SUCCESS | String |
The dispatch succeeded. Check the results property. |
FAILED | String |
The dispatch failed. Check the error property. |
CANCELLED | String |
The dispatch was cancelled. Check the reason property. |
Object
Workflow.Errors : A collection of possible Error messages the Workflow could generate.
Kind: static property of Workflow
Properties
Name | Type | Description |
---|---|---|
READONLY_MODE | String |
A readonly workflow cannot be deleted. |
WORKFLOW_DELETED | String |
A deleted workflow cannot be invoked. |
Object
Workflow.Events : Events specific to Workflows. Also includes events related to all Plugin instances.
Kind: static property of Workflow
Properties
Name | Type | Description |
---|---|---|
WF_DELETED | String |
The workflow was deleted. |
WF_DISPATCHED | String |
The workflow was dispatched. |
WF_STEPS_CHANGED | String |
The steps associated with the workflow were changed. |
"Deleted" (instance)
The workflow was deleted.
Kind: event emitted by Workflow
Param | Type |
---|---|
instance | Workflow |
"Dispatched" (instance, arguments, promise)
The workflow was dispatched.
Kind: event emitted by Workflow
Param | Type | Description |
---|---|---|
instance | Workflow |
|
arguments | Array |
The arguments passed to dispatch. |
promise | Promise |
A promise that will be resolved when the dispatch completes. |
"StepsChanged" (instance, steps)
Kind: event emitted by Workflow
Param | Type | Description |
---|---|---|
instance | Workflow |
|
steps | Array |
The updated array of steps. The array will have a toDAG method to convert it into a structure that respects dependencies between steps. |
WorkflowStep
Represents a single step in a Workflow instance.
Each WorkflowStep has a lifecycle Workflow follows during dispatch that you can hook into to control what happens.
First, the init
method is called. This can be used
to initialize instance values used in the other methods.
The init
method is guaranteed to only be called once
per dispatch.
Next, the execute
method is invoked, and passed any
arguments provided to the Workflow.dispatch
method. If
execute
returns a Promise, any dependent steps will
not run (and the Workflow dispatch promise will not be
resolved) until the execute
promise resolves. Whatever
value the execute
method returns (or a returned Promise
resolves with) will be stored in an internal results
map that is accessible to subsequent steps and provided
to the WorkflowResult that the dispatch promise is resolved
with.
If the execute
method throws an error or returns a
promise that rejects, then retry
will be called and
provided with the error reason. If retry
returns a
rejected promise or throws an error (which is the default
implementation if none is provided), then the dispatch
will be considered failed. Otherwise, returning anything
else (or nothing at all) will cause execute
to be invoked
again -- potentially an unlimited number of times.
If the dispatch enters a failed state, then all of the
executed steps' rollback
methods will be invoked and
provided with the failure reason. Rollbacks are invoked in
the reverse order the execute
methods were invoked.
This allows your executed steps to perform any cleanup
operations that may be necessary.
If the dispatch fails, then ALL steps -- even steps that
never had a chance to be executed -- will have their
failure
method invoked with the reason the dispatch
failed.
Only if all steps succeed will each executed step's
success
method be invoked. This method will be passed
the results object that maps step names to the values
returned by their execute
methods.
All step lifecycle methods have access to the following
properties on their context (i.e., this
):
Param | Type | Description |
---|---|---|
workflow | String | The name of the workflow being dispatched. This is useful when your step might be associated with multiple workflows. |
results | Object | A map of previously executed steps and the results of their execute methods. This is useful when your step is set to run after another step, and you would like to access the results of that step. |
cancel | Function | A method you can invoke to cancel the dispatch immediately. Note that this method is only available from init , execute , and retry . |
Kind: global class
Inherits: Plugin
Properties
Name | Type | Description |
---|---|---|
init | function |
A no-op, unless overridden. |
execute | function |
A no-op, unless overridden. |
retry | function |
Rejects, unless overridden. |
rollback | function |
A no-op, unless overridden. |
success | function |
A no-op, unless overridden. |
failure | function |
A no-op, unless overridden. |
new WorkflowStep(props)
Param | Type | Description |
---|---|---|
props | Object |
The properties to associate with this WorkflowStep instance. The only required property is name . You can provide Plugin-specific values such as after , filter , and targetType to restrict which Workflows your WorkflowStep will be associated with. You can also override any of the built-in WorkflowStep properties, such as execute . |
Example
// create a step that attempts to log in 3 times before// failing the dispatch -- if the execute method succeeds,// the dispatch results will contain a property called// 'login-step' whose value will be whatever value the// `postLogin(...)` promise resolves withPlugins; name: 'login-workflow' ;
WorkflowResult
Represents the result of a dispatched workflow. The dispatch promise will be resolved with an instance of WorkflowResult that can be examined to learn more about how the dispatch concluded.
Kind: global class
Properties
Name | Type | Description |
---|---|---|
status | States |
The status of the dispatch. |
executed | Array.<String> |
The names of the steps that executed during the dispatch. Useful for determining where an error may have occurred. |
error | Error |
The error that caused the dispatch to fail. This property only exists if status is Workflow.States.FAILED . |
reason | String | Error |
The reason the dispatch ended early. This property only exists if status is Workflow.States.CANCELLED . |
results | Object |
A map of step names to the results of that step's execute method. This property only exists if status is Workflow.States.SUCCESS . |
new WorkflowResult(data)
Param | Type | Description |
---|---|---|
data | Object |
Data about the dispatch. This information is provided automatically by the dispatch method. |