From 9c9553bb16073e955c2cb22f3fab703930fb635f Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Thu, 18 Jan 2018 09:16:46 +0300 Subject: [PATCH] added caching api to core... Signed-off-by: Alex A. Naanou --- ui (gen4)/features/core.js | 72 +++++++++++++++++++++++++++++++- ui (gen4)/features/peer.js | 4 +- ui (gen4)/features/ui-widgets.js | 15 +++++-- 3 files changed, 85 insertions(+), 6 deletions(-) diff --git a/ui (gen4)/features/core.js b/ui (gen4)/features/core.js index c2476ab8..95a2b4e4 100755 --- a/ui (gen4)/features/core.js +++ b/ui (gen4)/features/core.js @@ -270,7 +270,9 @@ function(func){ var IntrospectionActions = actions.Actions({ // user-callable actions... get useractions(){ - return this.actions.filter(this.isUserCallable.bind(this)) }, + return this.cache('useractions', function(){ + return this.actions.filter(this.isUserCallable.bind(this)) }) }, + //return this.actions.filter(this.isUserCallable.bind(this)) }, // check if action is callable by user... isUserCallable: ['- System/', @@ -294,6 +296,9 @@ module.Introspection = ImageGridFeatures.Feature({ title: '', tag: 'introspection', + depends: [ + 'cache' + ], actions: IntrospectionActions, }) @@ -551,6 +556,71 @@ module.Serialization = ImageGridFeatures.Feature({ }) +//--------------------------------------------------------------------- + +// XXX should this be in actions.js??? +// XXX should we invalidate the cache automatically??? +var CacheActions = actions.Actions({ + config: { + // Enable/disable caching... + 'cache': true, + + // Control pre-caching... + // + // This can be: + // true - pre-caching enabled + // 0 - same as true + // number - delay pre-caching by number milliseconds + // false - pre-caching disabled + 'pre-cache': 200, + }, + + // XXX should these be actions??? + cache: function(title, lister){ + if(!this.config.cache){ + return lister.call(this) + } + var cache = this.__cache = this.__cache || {} + var l = cache[title] = cache[title] || lister.call(this) + return l + }, + // XXX is this too broad?? + preCache: function(){ + if(this.config.cache){ + for(k in this){ this[k] } } }, + clearCache: function(title){ + if(title){ + delete (this.__cache|| {})[title] + + } else { + delete this.__cache + } + }, +}) + +var Cache = +module.Cache = ImageGridFeatures.Feature({ + title: '', + doc: '', + + tag: 'cache', + + actions: CacheActions, + + handlers: [ + ['start', + function(){ + var t = this.config['pre-cache'] + t == 0 || t === true ? + this.preCache() + : t > 0 ? + setTimeout((function(){ this.preCache() }).bind(this), t) + : false + }], + ], +}) + + //--------------------------------------------------------------------- var UtilActions = actions.Actions({ diff --git a/ui (gen4)/features/peer.js b/ui (gen4)/features/peer.js index ca729296..7b4746f4 100755 --- a/ui (gen4)/features/peer.js +++ b/ui (gen4)/features/peer.js @@ -181,7 +181,9 @@ var PeerActions = actions.Actions({ // XXX need more control... // - get proxies to specific peer... get peeractions(){ - return this.getPeerActions() }, + this.cache('peeractions', function(){ + return this.getPeerActions() }) }, + //return this.getPeerActions() }, getPeerActions: ['- System/Peer/', function(id){ diff --git a/ui (gen4)/features/ui-widgets.js b/ui (gen4)/features/ui-widgets.js index 4783c023..08d017ae 100755 --- a/ui (gen4)/features/ui-widgets.js +++ b/ui (gen4)/features/ui-widgets.js @@ -588,11 +588,17 @@ var DialogsActions = actions.Actions({ // introspection... get uiContainers(){ - return this.actions.filter(this.isUIContainer.bind(this)) }, + return this.cache('uiContainers', function(){ + return this.actions.filter(this.isUIContainer.bind(this)) }) }, + //return this.actions.filter(this.isUIContainer.bind(this)) }, get uiDialogs(){ - return this.actions.filter(this.isUIDialog.bind(this)) }, + return this.cache('uiDialogs', function(){ + return this.actions.filter(this.isUIDialog.bind(this)) }) }, + //return this.actions.filter(this.isUIDialog.bind(this)) }, get uiElements(){ - return this.actions.filter(this.isUIElement.bind(this)) }, + return this.cache('uiElements', function(){ + return this.actions.filter(this.isUIElement.bind(this)) }) }, + //return this.actions.filter(this.isUIElement.bind(this)) }, // XXX this knows about action priority and shortcut marker... // XXX should these be more like .getDoc(..) and support lists of actions??? @@ -875,6 +881,7 @@ module.Dialogs = core.ImageGridFeatures.Feature({ tag: 'ui-dialogs', depends: [ + 'cache', 'ui', ], @@ -1978,7 +1985,7 @@ module.ContextActionMenu = core.ImageGridFeatures.Feature({ //--------------------------------------------------------------------- -// XXX mac seems not to have the utf gear icon... +// mac seems not to have the utf gear icon... var SETTINGS_ICON = typeof(navigator) == 'undefined' ? 'settings' : navigator.platform == 'Win32' ? '⛭'