Skip to content

Instantly share code, notes, and snippets.

@fmarcia
Last active October 26, 2017 18:06
Show Gist options
  • Save fmarcia/c994dc66beccf018850b to your computer and use it in GitHub Desktop.
Save fmarcia/c994dc66beccf018850b to your computer and use it in GitHub Desktop.
// Seq
// Author: Franck Marcia
module.exports = (function () {
"use strict";
function Seq() {
this._error = [];
this._cont = [];
}
Seq.prototype.step = function (step) {
if (step && (step.constructor === Function || step instanceof Seq)) {
[].push.call(this, step);
}
return this;
};
Seq.prototype.error = function (step) {
if (step && (step.constructor === Function || step instanceof Seq)) {
this._error.push(step);
}
return this;
};
Seq.prototype.next = function () {
var args = arguments.length ? arguments : void 0;
if (this.length) {
var step = [].shift.call(this);
if (step.constructor === Function) {
step.apply(this, args);
} else {
var self = this;
step._cont.push(function () { self.next(); });
step.next.apply(step, args);
}
} else if (this._cont.length) {
this._cont.shift().apply(this, args);
}
};
Seq.prototype.halt = function () {
if (this._error.length) {
var args = arguments.length ? arguments : void 0;
this.length = 0;
var error = this._error.shift();
if (error.constructor === Function) {
error.apply(this, args);
if (this._cont.length) {
this._cont.shift().apply(this, args);
}
} else {
error.next.apply(error, args);
}
}
};
return function () {
return new Seq();
};
}());
seq = require('./seq');
var mySeq = seq();
mySeq
// 1st step: use 1 arg; define a seq property; call next step without args
.step(function (a) {
console.log('TEST #1');
var mySeq = this;
setTimeout(function () {
console.log(a);
mySeq.value = 'step #2';
mySeq.next();
}, 100);
})
// 2nd step: use a seq property; call the next step with 2 args
.step(function () {
setTimeout(function () {
console.log(mySeq.value);
mySeq.next('step #3a', 'step #3b');
}, 100);
})
// 3rd step: use 2 args
.step(function (a, b) {
setTimeout(function () {
console.log([ a, b ].join(', '));
mySeq.halt('halt #1', 'halt #2');
}, 100);
})
// 4th step: never reached
.step(function () {
console.log('step #4: never reached');
this.next();
})
// 5th step: never reached
.step(function () {
console.log('step #5: never reached');
})
// define handler to be called if the seq is halted
.error(function (a, b) {
console.log('halted with ' + a + ' and ' + b + ', ' + this.length + ' left');
});
var seqSub1 = seq();
seqSub1
.step(function () {
setTimeout(function () {
console.log('sub #1 - step #1');
seqSub1.next();
//seqSub1.halt();
//seqMain.halt('-1-', '-2-');
}, 100);
})
.step(function () {
setTimeout(function () {
console.log('sub #1 - step #2');
seqSub1.next();
}, 100);
})
.error(function() {
console.log('sub #1 - halted');
});
var seqSub2 = seq();
seqSub2
.step(function () {
setTimeout(function () {
console.log('sub #2 - step #1');
seqSub2.next();
}, 100);
})
.step(function () {
var seqSub2 = this;
setTimeout(function () {
console.log('sub #2 - step #2');
seqSub2.next();
}, 100);
})
.error(function() {
console.log('sub #2 - halted');
});
var seqError = seq()
.step(function (a, b) {
var seqError = this;
setTimeout(function () {
console.log('error - step #1 with (' + a + ', ' + b + ')');
seqError.next();
}, 100);
})
.step(function () {
setTimeout(function () {
console.log('error - step #2');
seqError.next();
}, 100);
})
.error(function() {
console.log('error - halted');
});
var seqMain = seq();
seqMain
.step(mySeq)
.step(function () { console.log('\nTEST #2'); this.next(); })
.step(seqSub1)
.step(seqSub2)
.error(seqError)
// .error(function () { console.log('main - halted') })
.next('step #1');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment