wip : replace Job by promises

This commit is contained in:
Julian Descottes 2015-04-09 16:37:29 +02:00 committed by juliandescottes
parent e11355193b
commit 522006f67a
4 changed files with 2011 additions and 24 deletions

1986
src/js/lib/q.js Normal file

File diff suppressed because it is too large Load diff

View file

@ -15,7 +15,7 @@
* @param {String} namespace
* @return {Object} the processed frame
*/
ns.AsyncCachedFrameProcessor.prototype.get = function (frame, callback, namespace) {
ns.AsyncCachedFrameProcessor.prototype.get = function (frame, namespace) {
var processedFrame = null;
namespace = namespace || this.defaultNamespace;
@ -35,18 +35,21 @@
processedFrame = this.outputCloner(cache[secondCacheKey], frame);
cache[firstCacheKey] = processedFrame;
} else {
this.frameProcessor(frame, this.onFrameProcessorComplete.bind(this, callback, cache, firstCacheKey, secondCacheKey));
var deferred = Q.defer();
this.frameProcessor(frame, this.onFrameProcessorComplete.bind(this, deferred, cache, firstCacheKey, secondCacheKey));
return deferred.promise;
}
}
if (processedFrame) {
callback(processedFrame);
return Q.fcall(processedFrame);
}
};
ns.AsyncCachedFrameProcessor.prototype.onFrameProcessorComplete = function (callback, cache, firstCacheKey, secondCacheKey, processedFrame) {
ns.AsyncCachedFrameProcessor.prototype.onFrameProcessorComplete = function (deferred, cache, firstCacheKey, secondCacheKey, processedFrame) {
cache[secondCacheKey] = processedFrame;
cache[firstCacheKey] = processedFrame;
callback(processedFrame);
console.log('RESOLVING');
deferred.resolve(processedFrame);
}
})();

View file

@ -53,29 +53,24 @@
var layers = this.piskelController.getLayers();
var frames = layers.map(function (l) {return l.getFrames();}).reduce(function (p, n) {return p.concat(n);});
this.currentJob = new pskl.utils.Job({
items : frames,
args : {
colors : {}
},
process : function (frame, callback) {
return this.cachedFrameProcessor.get(frame, callback);
}.bind(this),
onProcessEnd : function (frameColors) {
var colors = this.args.colors;
Object.keys(frameColors).slice(0, Constants.MAX_CURRENT_COLORS_DISPLAYED).forEach(function (color) {
Q.all(
frames.map(function (frame) {
return this.cachedFrameProcessor.get(frame);
}.bind(this))
).done(function (results) {
console.log('ALL DONE');
var colors = {};
results.forEach(function (result) {
Object.keys(result).forEach(function (color) {
colors[color] = true;
});
},
onComplete : this.updateCurrentColorsReady_.bind(this)
});
this.currentJob.start();
})
this.updateCurrentColorsReady_(colors);
}.bind(this))
};
ns.CurrentColorsService.prototype.updateCurrentColorsReady_ = function (args) {
var colors = args.colors;
ns.CurrentColorsService.prototype.updateCurrentColorsReady_ = function (colors) {
// Remove transparent color from used colors
delete colors[Constants.TRANSPARENT_COLOR];

View file

@ -51,6 +51,9 @@
// Spectrum color-picker library
"js/lib/spectrum/spectrum.js",
// Promises
"js/lib/q.js",
// Application libraries-->
"js/rendering/DrawingLoop.js",