sequence-stepper
Advanced tools
Comparing version 0.3.1 to 0.4.0
@@ -13,60 +13,78 @@ 'use strict'; | ||
var StepDescriptor = function () { | ||
/** | ||
* @param {Function} step - action, which will be carried out in the executing of this step | ||
* @param {Stepper} stepper - instance of Stepper, which contains this StepDescriptor | ||
* */ | ||
function StepDescriptor(step, stepper) { | ||
var _this = this; | ||
var StepDescriptor = | ||
/** | ||
* @param {Function} step - action, which will be carried out in the executing of this step | ||
* @param {Stepper} stepper - instance of Stepper, which contains this StepDescriptor | ||
* */ | ||
function StepDescriptor(step, stepper) { | ||
var _this = this; | ||
_classCallCheck(this, StepDescriptor); | ||
_classCallCheck(this, StepDescriptor); | ||
this.id = 0; | ||
_initialiseProps.call(this); | ||
this.id = StepDescriptor.ID_COUNTER; | ||
StepDescriptor.ID_COUNTER++; | ||
this.id = StepDescriptor.ID_COUNTER; | ||
StepDescriptor.ID_COUNTER++; | ||
this.stepper = stepper; | ||
this.action = step; | ||
this.execute = function (data, done) { | ||
return step(_this, data, done); | ||
}; | ||
} | ||
this.stepper = stepper; | ||
this.action = step; | ||
this.execute = function (data, done) { | ||
return step(_this, data, done); | ||
}; | ||
} | ||
_createClass(StepDescriptor, [{ | ||
key: 'next', | ||
/** | ||
* @param {*} [data] | ||
* */ | ||
/** | ||
* @param {*} data | ||
* */ | ||
value: function next(data) { | ||
return this.stepper.next(data, this); | ||
} | ||
}, { | ||
key: 'remove', | ||
value: function remove() { | ||
this.stepper.remove(this); | ||
} | ||
/** | ||
* @param {*} data | ||
* */ | ||
/** | ||
* @param {*} data | ||
* */ | ||
}, { | ||
key: 'reject', | ||
value: function reject(data) { | ||
this.stepper.reject(data); | ||
} | ||
}]); | ||
/** | ||
* @param {Function} step | ||
* @return {StepDescriptor} | ||
* */ | ||
return StepDescriptor; | ||
}(); | ||
/** | ||
* @param {Function} step | ||
* @return {StepDescriptor} | ||
* */ | ||
; | ||
StepDescriptor.ID_COUNTER = 0; | ||
var _initialiseProps = function _initialiseProps() { | ||
var _this3 = this; | ||
this.id = 0; | ||
this.next = function (data) { | ||
_this3.stepper.next(data, _this3); | ||
}; | ||
this.remove = function () { | ||
_this3.stepper.remove(_this3); | ||
}; | ||
this.reject = function (data) { | ||
_this3.stepper.reject(data); | ||
}; | ||
this.insertAfter = function (step) { | ||
return _this3.stepper.insertAfter(_this3, step); | ||
}; | ||
this.insertBefore = function (step) { | ||
return _this3.stepper.insertBefore(_this3, step); | ||
}; | ||
}; | ||
var Stepper = exports.Stepper = function () { | ||
/** | ||
* @param {Function[]} steps - array of steps, which will be treated | ||
* @param {Function} onReject - callback, which will be executing on some step | ||
* @param {Function} [onReject] - callback, which will be executing on some step | ||
* */ | ||
@@ -132,2 +150,14 @@ function Stepper(steps) { | ||
/** | ||
* Start execution a queue from start | ||
* @param {*} data | ||
* */ | ||
}, { | ||
key: 'start', | ||
value: function start(data) { | ||
this.currentStep = -1; | ||
this.next(data); | ||
} | ||
/** | ||
* @param {StepDescriptor} stepDescriptor | ||
@@ -186,2 +216,13 @@ * */ | ||
/** | ||
* @param {Number} index - position in steps array | ||
* @return {StepDescriptor} | ||
* */ | ||
}, { | ||
key: 'getStep', | ||
value: function getStep(index) { | ||
return this.steps[index]; | ||
} | ||
/** | ||
* @param {StepDescriptor} stepDescriptor - descriptor of the step before which will be inserted a new step | ||
@@ -228,3 +269,13 @@ * @param {Function} step - callback for the new step descriptor | ||
function _sequence(steps, reject) { | ||
/** | ||
* @param {Function[]} steps | ||
* @param {Function} [reject] | ||
* */ | ||
function _sequence(steps) { | ||
var reject = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () { | ||
return null; | ||
}; | ||
var _steps$slice$reverse = steps.slice().reverse(), | ||
@@ -231,0 +282,0 @@ _steps$slice$reverse2 = _toArray(_steps$slice$reverse), |
{ | ||
"name": "sequence-stepper", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "The small lib for the asynchronous control of queue of functions", | ||
@@ -9,4 +9,3 @@ "main": "build/stepper.js", | ||
"transpile": "babel ./src --out-dir ./build", | ||
"prepublish": "npm run transpile", | ||
"preversion": "npm run transpile" | ||
"prepublish": "npm run transpile" | ||
}, | ||
@@ -13,0 +12,0 @@ "keywords": [ |
@@ -22,3 +22,3 @@ #sequence-stepper | ||
(step, data, done) => done ? console.log(data) : null; | ||
]); | ||
], (message) => console.log(message)); | ||
``` | ||
@@ -33,3 +33,3 @@ | ||
```js | ||
stepper.next(data); | ||
stepper.start(data); | ||
``` | ||
@@ -44,3 +44,3 @@ | ||
```js | ||
stepper.next(data, stepper[2]); | ||
stepper.next(data, stepper.steps[2]); | ||
``` | ||
@@ -53,4 +53,4 @@ | ||
let stepper = new Stepper([ | ||
(step, data, done) => {...}, | ||
(step, data, done) => { | ||
(step) => {...}, | ||
(step) => { | ||
//some behavior | ||
@@ -61,6 +61,6 @@ ... | ||
}, | ||
(step, data, done) => {...} | ||
(step) => {...} | ||
]); | ||
stepper.next()//execute queue till the end | ||
stepper.start()//execute queue till the end | ||
@@ -70,2 +70,46 @@ savedStepDescriptor.next()//execute queue from saved step till the end; | ||
insertBefore and insertAfter usage | ||
```js | ||
let stepper = new Stepper([ | ||
... | ||
(step) => { | ||
step.insertAfter((step) => step.next()); | ||
step.insertBefore((step) => step.next()); | ||
step.next(); | ||
}, | ||
... | ||
]); | ||
``` | ||
or | ||
```js | ||
let stepper = new Stepper([...]); | ||
stepper.insertAfter(stepper.getStep(2), ({next}) => next()); | ||
``` | ||
or | ||
```js | ||
let savedStepDescriptor; | ||
let stepper = new Stepper([ | ||
... | ||
(step) => { | ||
savedStepDescriptor = step; | ||
step.next(); | ||
}, | ||
... | ||
]); | ||
savedStepDescriptor.insertAfter(({next}) => next()); | ||
``` | ||
Brief usage of Stepper | ||
```js | ||
let stepper = new Stepper([ | ||
({next}) => next(), | ||
({next}) => setTimeout(next, 100), | ||
({next}) => console.log('complete') | ||
]); | ||
stepper.start(); | ||
``` | ||
###function sequence | ||
@@ -94,1 +138,6 @@ Its help you to make a function thats launches a queue to the end. You can make it with this simple functional conveyors. | ||
``` | ||
###Notice | ||
In outline Stepper and sequence has a similar behavior. | ||
If you don`t want to use insertAfter and insertBefore, you can restrict a sequence. |
@@ -22,11 +22,11 @@ class StepDescriptor { | ||
/** | ||
* @param {*} data | ||
* @param {*} [data] | ||
* */ | ||
next(data) { | ||
return this.stepper.next(data, this); | ||
} | ||
next = (data) => { | ||
this.stepper.next(data, this); | ||
}; | ||
remove() { | ||
remove = () => { | ||
this.stepper.remove(this); | ||
} | ||
}; | ||
@@ -36,5 +36,21 @@ /** | ||
* */ | ||
reject(data) { | ||
reject = (data) => { | ||
this.stepper.reject(data); | ||
} | ||
}; | ||
/** | ||
* @param {Function} step | ||
* @return {StepDescriptor} | ||
* */ | ||
insertAfter = (step) => { | ||
return this.stepper.insertAfter(this, step); | ||
}; | ||
/** | ||
* @param {Function} step | ||
* @return {StepDescriptor} | ||
* */ | ||
insertBefore = (step) => { | ||
return this.stepper.insertBefore(this, step); | ||
}; | ||
} | ||
@@ -45,3 +61,3 @@ | ||
* @param {Function[]} steps - array of steps, which will be treated | ||
* @param {Function} onReject - callback, which will be executing on some step | ||
* @param {Function} [onReject] - callback, which will be executing on some step | ||
* */ | ||
@@ -85,2 +101,11 @@ constructor(steps, onReject = () => null) { | ||
/** | ||
* Start execution a queue from start | ||
* @param {*} data | ||
* */ | ||
start(data) { | ||
this.currentStep = -1; | ||
this.next(data); | ||
} | ||
/** | ||
* @param {StepDescriptor} stepDescriptor | ||
@@ -124,2 +149,10 @@ * */ | ||
/** | ||
* @param {Number} index - position in steps array | ||
* @return {StepDescriptor} | ||
* */ | ||
getStep(index) { | ||
return this.steps[index]; | ||
} | ||
/** | ||
* @param {StepDescriptor} stepDescriptor - descriptor of the step before which will be inserted a new step | ||
@@ -152,3 +185,7 @@ * @param {Function} step - callback for the new step descriptor | ||
export function sequence(steps, reject) { | ||
/** | ||
* @param {Function[]} steps | ||
* @param {Function} [reject] | ||
* */ | ||
export function sequence(steps, reject = () => null) { | ||
let [last, ...firsts] = steps.slice().reverse(); | ||
@@ -155,0 +192,0 @@ let seq = firsts.reduce((nextStep, step, index) => |
97
tests.js
@@ -23,3 +23,3 @@ import test from 'ava'; | ||
return (new Array(count)).fill(null).map((itm, index) => (step, data, done) => { | ||
if (index - 1 === data ? 1 : 0) { | ||
if (index === data) { | ||
passedStepsCounter.add(); | ||
@@ -30,3 +30,3 @@ } | ||
step[done ? 'reject' : 'next'](index); | ||
step[done ? 'reject' : 'next'](index + 1); | ||
}); | ||
@@ -44,3 +44,3 @@ } | ||
return {startValue: -1, reject, descriptors, passedSteps, steps, count}; | ||
return {startValue: 0, reject, descriptors, passedSteps, steps, count}; | ||
} | ||
@@ -61,3 +61,3 @@ | ||
stepper.next(testObj.startValue); | ||
stepper.start(testObj.startValue); | ||
@@ -67,9 +67,60 @@ return testObj; | ||
function checkValidEnding(factory, validator) { | ||
let stepsCount = 10, testObj = factory(stepsCount); | ||
function makeSequenceChecker(desiredStepsSeq, onFail) { | ||
let stepsLeft = desiredStepsSeq.slice(); | ||
if (testObj.reject.state && testObj.passedSteps.get() === stepsCount) { | ||
validator.pass(); | ||
} else { | ||
validator.fail(` | ||
return (index) => { | ||
const [current, ...rest] = stepsLeft; | ||
if (current !== index) { | ||
onFail(`fail on step ${current}`); | ||
} | ||
stepsLeft = rest; | ||
} | ||
} | ||
function checkAsyncHoleyStepper() { | ||
test.cb('checking async holey Stepper with inserting steps', (t) => { | ||
let checker = makeSequenceChecker([0, 1, 2, 3, 4], (message) => t.fail(message)); | ||
let stepper = new Stepper([ | ||
(step) => { | ||
checker(0); | ||
step.next(); | ||
}, | ||
(step) => { | ||
checker(1); | ||
setTimeout(() => { | ||
step.insertAfter((step) => { | ||
checker(2); | ||
setTimeout(() => { | ||
step.insertAfter((step) => { | ||
checker(3); | ||
step.next(); | ||
}); | ||
step.next(); | ||
}, 1); | ||
}); | ||
step.next(); | ||
}, 1) | ||
}, | ||
() => { | ||
checker(4); | ||
t.pass(); | ||
t.end(); | ||
} | ||
]); | ||
stepper.next(); | ||
}); | ||
} | ||
function checkValidEnding(factory, title) { | ||
test.cb(`create ${title} and full series executing`, (t) => { | ||
let stepsCount = 10, testObj = factory(stepsCount); | ||
if (testObj.reject.state && testObj.passedSteps.get() === stepsCount) { | ||
t.pass(); | ||
} else { | ||
t.fail(` | ||
reject on last step (it must be a true): ${testObj.reject.state} | ||
@@ -79,5 +130,6 @@ steps count: ${stepsCount} | ||
`); | ||
} | ||
} | ||
validator.end(); | ||
t.end(); | ||
}); | ||
} | ||
@@ -94,5 +146,5 @@ | ||
if (shift < stepsCount - 1) { | ||
testObj.descriptors[shift].next(shift); | ||
testObj.descriptors[shift].next(shift + 1); | ||
} else { | ||
testObj.descriptors[shift].reject(shift); | ||
testObj.descriptors[shift].reject(shift + 1); | ||
} | ||
@@ -104,7 +156,7 @@ | ||
t.fail(` | ||
reject on last step (it must be a true): ${testObj.reject.state} | ||
steps count: ${stepsCount} | ||
shift: ${shift} | ||
steps passed: ${testObj.passedSteps.get()} | ||
`); | ||
reject on last step (it must be a true): ${testObj.reject.state} | ||
steps count: ${stepsCount} | ||
shift: ${shift} | ||
steps passed: ${testObj.passedSteps.get()} | ||
`); | ||
} | ||
@@ -117,8 +169,7 @@ | ||
test.cb('create sequence and full series executing', | ||
(t) => checkValidEnding(createSequenceAndRun, t)); | ||
test.cb('create Stepper instance and full series executing', | ||
(t) => checkValidEnding(createStepperAndRun, t)); | ||
checkValidEnding(createSequenceAndRun, 'sequence'); | ||
checkValidEnding(createStepperAndRun, 'Stepper instance'); | ||
testShiftedValidEnding(createSequenceAndRun, 'sequence'); | ||
testShiftedValidEnding(createStepperAndRun, 'Stepper instance'); | ||
checkAsyncHoleyStepper(); |
Sorry, the diff of this file is not supported yet
21002
530
137