added caching api to core...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2018-01-18 09:16:46 +03:00
parent 66aea36685
commit 9c9553bb16
3 changed files with 85 additions and 6 deletions

View File

@ -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({

View File

@ -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){

View File

@ -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' ? '&#9965;'