updated the cache mechanics a bit more + don't show progress if things go fast enough...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2018-01-20 01:13:41 +03:00
parent 8b2a135b9d
commit 9f1fe87509

View File

@ -271,9 +271,10 @@ function(func){
var IntrospectionActions = actions.Actions({ var IntrospectionActions = actions.Actions({
// user-callable actions... // user-callable actions...
get useractions(){ get useractions(){
return this.cache('useractions', function(){ return this.cache('useractions', function(d){
return this.actions.filter(this.isUserCallable.bind(this)) }) }, return d instanceof Array ?
//return this.actions.filter(this.isUserCallable.bind(this)) }, d.slice()
: this.actions.filter(this.isUserCallable.bind(this)) }) },
// check if action is callable by user... // check if action is callable by user...
isUserCallable: ['- System/', isUserCallable: ['- System/',
@ -571,9 +572,9 @@ var CacheActions = actions.Actions({
// Control pre-caching... // Control pre-caching...
// //
// This can be: // This can be:
// true - sync pre-cache // true - sync pre-cache (recursion)
// 0 - semi-sync pre-cache // 0 - semi-sync pre-cache
// number - delay pre-caching by number milliseconds // number - delay in milliseconds between pre-cache chunks
// false - pre-caching disabled // false - pre-caching disabled
'pre-cache': 0, 'pre-cache': 0,
@ -582,25 +583,67 @@ var CacheActions = actions.Actions({
// Caching is done in a series of chunks set by this separated by // Caching is done in a series of chunks set by this separated by
// timeouts set by .config['pre-cache'] to let other stuff run... // timeouts set by .config['pre-cache'] to let other stuff run...
'pre-cache-chunk': 8, 'pre-cache-chunk': 8,
// Control pre-cache progress display...
//
// This can be:
// false - never show progress
// true - always show progress
// number - show progress if number of milliseconds has
// passed and we are not done yet...
//
// NOTE: progress will only be displayed if .showProgress(..)
// action is available...
'pre-cache-progress': 3000,
}, },
// XXX should these be actions??? // Cache utility method...
//
// Example use:
// someAction: [
// function(){
// return this.cache('someAction',
// function(data){
// if(data){
// // clone/update the data...
// // NOTE: this should be faster than the construction
// // branch below or this will defeat the purpose
// // of caching...
// ...
//
// } else {
// // get the data...
// ...
// }
// return data
// }) }],
//
cache: function(title, lister){ cache: function(title, lister){
if(!this.config.cache){ if(!this.config.cache){
return lister.call(this) return lister.call(this)
} }
var cache = this.__cache = this.__cache || {} var cache = this.__cache = this.__cache || {}
var l = cache[title] = cache[title] || lister.call(this) return (cache[title] =
return l title in cache ?
// pass the cached data for cloning/update to the lister...
lister.call(this, cache[title])
: lister.call(this))
}, },
// XXX revise naming...
// XXX is this too broad?? preCache: ['System/Run pre-cache',
preCache: function(){ doc`Run pre-cache...
if(this.config.cache){
for(k in this){ this[k] } } Do an async pre-cache...
return this .preCache()
},
_preCache: function(t){ Do a sync pre-cache...
.preCache(true)
NOTE: both "modes" of doing a pre-cache run in the main thread,
the difference is that the "async" version lets JS run frames
between processing sync chunks...
`,
function(t){
if(this.config.cache){ if(this.config.cache){
var t = t || this.config['pre-cache'] || 0 var t = t || this.config['pre-cache'] || 0
var c = this.config['pre-cache-chunk'] || 8 var c = this.config['pre-cache-chunk'] || 8
@ -611,6 +654,9 @@ var CacheActions = actions.Actions({
} }
var l = attrs.length var l = attrs.length
var started = Date.now()
var show = this.config['pre-cache-progress']
var tick = function(){ var tick = function(){
var a = Date.now() var a = Date.now()
var b = a var b = a
@ -621,27 +667,30 @@ var CacheActions = actions.Actions({
while(b - a < c){ while(b - a < c){
this[attrs.pop()] this[attrs.pop()]
b = Date.now() b = Date.now()
done += 1
// XXX is this the right way to go???
this.showProgress this.showProgress
&& this.showProgress('Caching', done++, l) && (show === true || (show && b - started > show))
&& this.showProgress('Caching', done, l)
} }
setTimeout(tick, t) t === true ?
tick()
: setTimeout(tick, t)
}.bind(this) }.bind(this)
tick() tick()
} }
return this }],
}, clearCache: ['System/Clear cache',
clearCache: function(title){ function(title){
if(title){ if(title){
delete (this.__cache|| {})[title] delete (this.__cache|| {})[title]
} else { } else {
delete this.__cache delete this.__cache
} }
}, }],
}) })
var Cache = var Cache =
@ -650,6 +699,9 @@ module.Cache = ImageGridFeatures.Feature({
doc: '', doc: '',
tag: 'cache', tag: 'cache',
// NOTE: we use .showProgress(..) of 'ui-progress' but we do not
// need it to work, thus we do not declare it as a dependency...
//depends: [],
actions: CacheActions, actions: CacheActions,
@ -658,9 +710,9 @@ module.Cache = ImageGridFeatures.Feature({
function(){ function(){
var t = this.config['pre-cache'] var t = this.config['pre-cache']
t === true ? t === true ?
this.preCache() this.preCache('now')
: t >= 0 ? : t >= 0 ?
this._preCache() this.preCache()
: false : false
}], }],
], ],