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 * @param {String} namespace
* @return {Object} the processed frame * @return {Object} the processed frame
*/ */
ns.AsyncCachedFrameProcessor.prototype.get = function (frame, callback, namespace) { ns.AsyncCachedFrameProcessor.prototype.get = function (frame, namespace) {
var processedFrame = null; var processedFrame = null;
namespace = namespace || this.defaultNamespace; namespace = namespace || this.defaultNamespace;
@ -35,18 +35,21 @@
processedFrame = this.outputCloner(cache[secondCacheKey], frame); processedFrame = this.outputCloner(cache[secondCacheKey], frame);
cache[firstCacheKey] = processedFrame; cache[firstCacheKey] = processedFrame;
} else { } 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) { 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[secondCacheKey] = processedFrame;
cache[firstCacheKey] = processedFrame; cache[firstCacheKey] = processedFrame;
callback(processedFrame); console.log('RESOLVING');
deferred.resolve(processedFrame);
} }
})(); })();

View file

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

View file

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