From 8f17e9ed0a1529c6bae380b691181a0b22e73e03 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Thu, 18 Feb 2016 17:49:48 -0800 Subject: [PATCH] GUAC-1378: Cache previous calls to $templateRequest() to avoid duplicating patch processing work. --- .../index/config/templateRequestDecorator.js | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/guacamole/src/main/webapp/app/index/config/templateRequestDecorator.js b/guacamole/src/main/webapp/app/index/config/templateRequestDecorator.js index 68eb6a1f5..43655bc62 100644 --- a/guacamole/src/main/webapp/app/index/config/templateRequestDecorator.js +++ b/guacamole/src/main/webapp/app/index/config/templateRequestDecorator.js @@ -31,6 +31,16 @@ angular.module('index').config(['$provide', function($provide) { // Required services var $q = $injector.get('$q'); + /** + * A map of previously-returned promises from past calls to + * $templateRequest(). Future calls to $templateRequest() will return + * new promises chained to the first promise returned for a given URL, + * rather than redo patch processing for every request. + * + * @type Object.> + */ + var promiseCache = {}; + /** * Array of the raw HTML of all patches which should be applied to the * HTML of retrieved templates. @@ -224,14 +234,24 @@ angular.module('index').config(['$provide', function($provide) { * applying all HTML patches from any installed Guacamole extensions * to the HTML of the requested template. * + * @param {String} url + * The URL of the template being requested. + * * @returns {Promise.} * A Promise which resolves with the patched HTML contents of the * requested template if retrieval of the template is successful. */ - var decoratedTemplateRequest = function decoratedTemplateRequest() { + var decoratedTemplateRequest = function decoratedTemplateRequest(url) { var deferred = $q.defer(); + // Chain to cached promise if it already exists + var cachedPromise = promiseCache[url]; + if (cachedPromise) { + cachedPromise.then(deferred.resolve, deferred.reject); + return deferred.promise; + } + // Resolve promise with patched template HTML $delegate.apply(this, arguments).then(function patchTemplate(data) { @@ -281,6 +301,8 @@ angular.module('index').config(['$provide', function($provide) { }, deferred.reject); + // Cache this promise for future results + promiseCache[url] = deferred.promise; return deferred.promise; };