element-strategy v1.1.0
Element Strategy
This package helps to build strategies which your program should follow. It is not an finite state machine library.
Strategies are a part of machine state, and should be treated like step by step alghoritm.
For example if program faild due to exception it may change it's state to "failure" with specific strategy. It could've been some repair steps or cleanups before shutting down.
Generic Strategy
Main part of this package contain basic logic which is needed to manage strategy. Any implementation with unified API fits this construct. It may be Linear, Graph or any other Strategy.
It is highly recomended not to use generic constructs directly. You should always extends from it and make your own specific strategy.
Strategy Controller
Controller([ Element[] ]): controller
- @param {Element} Optional array of strategy elements
- @return {controller} new Controller object
Example:
/* create empty controller wihout any elements */
new Controller();
/*
* create controller with two empty elements
*/
new Controller([new Element({}), new Element({})]);
Controller API
index: number
- @property
- @public
Points to current active element in strategy.
push(element: Element[]): number
- @method
- @public
- @param {Element} strategy Element
- @return {number} Index of new added element
Adds new Element at the end of strategy, and returns its index.
currentElement(): Element
- @method
- @public
- @return {Element} Current active element
move(step: number): boolean
- @method
- @public
- @return {bool} is move action successful
Move method contains logic which prevents to go out of range. Returns bool which indicates if strategy successfully move to next Element.
step
param must be zero or positive and negative Integer.
Move calls hooks which may cause wanted side effects. These hooks are:
- onNext()
- onPrev()
- onAgain()
- onFinsih()
Hooks are part of Strategy Element API and it's required by implementing IElement Interface.
prev(): boolean
- @method
- @public
- @return {bool} is prev action successful
Move strategy by one step backward, and return true if ends with success.
next(): boolean
- @method
- @public
- @return {bool} is next action successful
Move strategy by one step forward, and return true if ends with success.
again(): boolean
- @method
- @public
- @return {bool} is next action successful
Repeats the same step, and return true if ends with success.
Strategy Element
Element(obj: Object): element
- @param {Object} Object which represents your strategy step
- @return {element} new Element object
Example:
/* create simple element which some method */
new Element({ init: function () {},
run: function () {},
report: function () {} });
Element API
Element implements IElement interface which define hooks unified API. Every hook takes one argument which is index of element.
It shouldn't return anything because all returned values will not be utilise. Purpose of these hooks is to create space for side effects like logging, event emmitting and so on.
onPush(index: number): void
- @method
- @public
onPush is invoke every time when new element is added to strategy and returns it's index.
onPrev(index: number): void
- @method
- @public
onPrev is invoke every time when strategy successfully goes backward.
onNext(index: number): void
- @method
- @public
onNext is invoke every time when strategy successfully goes forward.
onAgain(index: number): void
- @method
- @public
onAgain is invoke every time when strategy successfully repeat the step.
onFinish(index: number): void
- @method
- @public
onFinish is invoke every time when strategy passes last element.
Linear Strategy
Lienar strategy leverage Generic Strategy to make linear strategy, which should be processed one by one without jumping through all strategy Elements.
Linear strategy additionally implements rewind to first and fast forward to last element.
Controller API
first(): void
- @method
- @public
Rewind to first element in strategy.
last(): void
- @method
- @public
Fast forward to last element in strategy.