From fa4e96e7e500fe0c3b4695ced34cc9d6b75bb86c Mon Sep 17 00:00:00 2001 From: jdescottes Date: Thu, 3 Jul 2014 23:23:31 +0200 Subject: [PATCH] Added documentation in CachedFrameProcessor --- src/js/controller/PreviewFilmController.js | 1 - src/js/model/frame/CachedFrameProcessor.js | 25 +++++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/js/controller/PreviewFilmController.js b/src/js/controller/PreviewFilmController.js index fc0bf192..d61a80c6 100644 --- a/src/js/controller/PreviewFilmController.js +++ b/src/js/controller/PreviewFilmController.js @@ -208,7 +208,6 @@ ns.PreviewFilmController.prototype.getCanvasForFrame = function (frame) { var canvas = this.cachedFrameProcessor.get(frame, this.zoom); - canvas.classList.add('tile-view', 'canvas'); return canvas; }; diff --git a/src/js/model/frame/CachedFrameProcessor.js b/src/js/model/frame/CachedFrameProcessor.js index cb44e8af..3c0c07aa 100644 --- a/src/js/model/frame/CachedFrameProcessor.js +++ b/src/js/model/frame/CachedFrameProcessor.js @@ -1,7 +1,8 @@ (function () { var ns = $.namespace('pskl.model.frame'); - var DEFAULT_RESET_INTERVAL = 10 * 60 *1000; + + var DEFAULT_CLEAR_INTERVAL = 10 * 60 *1000; var DEFAULT_FRAME_PROCESSOR = function (frame) { return pskl.utils.FrameUtils.toImage(frame); @@ -13,7 +14,7 @@ ns.CachedFrameProcessor = function (cacheResetInterval) { this.cache_ = {}; - this.cacheResetInterval = cacheResetInterval || DEFAULT_RESET_INTERVAL; + this.cacheResetInterval = cacheResetInterval || DEFAULT_CLEAR_INTERVAL; this.frameProcessor = DEFAULT_FRAME_PROCESSOR; this.outputCloner = DEFAULT_OUTPUT_CLONER; @@ -24,14 +25,32 @@ this.cache_ = {}; }; + /** + * Set the processor function that will be called when there is a cache miss + * Function with 1 argument : pskl.model.Frame + * @param {Function} frameProcessor + */ ns.CachedFrameProcessor.prototype.setFrameProcessor = function (frameProcessor) { this.frameProcessor = frameProcessor; }; + /** + * Set the cloner that will be called when there is a miss on the 1st level cache + * but a hit on the 2nd level cache. + * Function with 2 arguments : cached value, frame + * @param {Function} outputCloner + */ ns.CachedFrameProcessor.prototype.setOutputCloner = function (outputCloner) { this.outputCloner = outputCloner; }; + /** + * Retrieve the processed frame from the cache, in the (optional) namespace + * If the first level cache is empty, attempt to clone it from 2nd level cache. If second level cache is empty process the frame. + * @param {pskl.model.Frame} frame + * @param {String} namespace + * @return {Object} the processed frame + */ ns.CachedFrameProcessor.prototype.get = function (frame, namespace) { var processedFrame = null; namespace = namespace || DEFAULT_NAMESPACE; @@ -48,7 +67,7 @@ } else { var frameAsString = JSON.stringify(frame.getPixels()); if (this.cache_[frameAsString]) { - processedFrame = this.outputCloner(this.cache_[frameAsString]); + processedFrame = this.outputCloner(this.cache_[frameAsString], frame); } else { processedFrame = this.frameProcessor(frame); this.cache_[frameAsString] = processedFrame;