wip : replace Job by promises
This commit is contained in:
parent
e11355193b
commit
522006f67a
4 changed files with 2011 additions and 24 deletions
src
1986
src/js/lib/q.js
Normal file
1986
src/js/lib/q.js
Normal file
File diff suppressed because it is too large
Load diff
|
@ -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);
|
||||
}
|
||||
})();
|
|
@ -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];
|
||||
|
||||
|
|
|
@ -51,6 +51,9 @@
|
|||
// Spectrum color-picker library
|
||||
"js/lib/spectrum/spectrum.js",
|
||||
|
||||
// Promises
|
||||
"js/lib/q.js",
|
||||
|
||||
// Application libraries-->
|
||||
"js/rendering/DrawingLoop.js",
|
||||
|
||||
|
|
Loading…
Reference in a new issue