wip : replace Job by promises

This commit is contained in:
Julian Descottes 2015-04-09 16:37:55 +02:00 committed by juliandescottes
parent 522006f67a
commit e6ed0c28a3
3 changed files with 0 additions and 103 deletions

View File

@ -1,29 +0,0 @@
(function () {
var ns = $.namespace('pskl.utils');
ns.Job = function (cfg) {
this.args = cfg.args;
this.items = cfg.items;
this.process = cfg.process;
this.onProcessEnd = cfg.onProcessEnd;
this.onComplete = cfg.onComplete;
this.completed_ = 0;
};
ns.Job.prototype.start = function () {
this.items.forEach(function (item, index) {
this.process(item, this.processCallback.bind(this, index));
}.bind(this))
};
ns.Job.prototype.processCallback = function (index, args) {
this.completed_++;
this.onProcessEnd(args, index);
if (this.completed_ === this.items.length) {
this.onComplete(this.args);
}
}
})();

View File

@ -24,7 +24,6 @@
"js/utils/FileUtilsDesktop.js",
"js/utils/FrameTransform.js",
"js/utils/FrameUtils.js",
"js/utils/Job.js",
"js/utils/LayerUtils.js",
"js/utils/ImageResizer.js",
"js/utils/PixelUtils.js",

View File

@ -1,73 +0,0 @@
describe("Job for // async", function() {
beforeEach(function() {});
afterEach(function() {});
it("completes synchronous job", function() {
// when
var isComplete = false;
var result = null;
// then
var job = new pskl.utils.Job({
items : [0,1,2,3,4],
args : {
store : []
},
process : function (item, callback) {
callback(item+5)
},
onProcessEnd : function (value, index) {
this.args.store[index] = value;
},
onComplete : function (args) {
isComplete = true;
result = args.store;
}
});
job.start();
// verify
expect(isComplete).toBe(true);
expect(result).toEqual([5,6,7,8,9]);
});
describe("async", function () {
// when
var isComplete = false;
var result = null;
beforeEach(function(done) {
// then
var job = new pskl.utils.Job({
items : [0,1,2,3,4],
args : {
store : []
},
process : function (item, callback) {
setTimeout(function (item, callback) {
callback(item+5);
}.bind(this, item, callback), 100 - (item * 20));
},
onProcessEnd : function (value, index) {
console.log('Processed ', index);
this.args.store[index] = value;
},
onComplete : function (args) {
isComplete = true;
result = args.store;
done();
}
});
job.start();
});
it("completes asynchronous job", function() {
// verify
expect(isComplete).toBe(true);
expect(result).toEqual([5,6,7,8,9]);
});
})
});