mirror of
https://github.com/flynx/pWiki.git
synced 2025-10-29 18:10:09 +00:00
added 'status' and 'chached' actions to index...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
d15d516676
commit
cd9f6b53e7
@ -41,10 +41,12 @@ var pwpath = require('../path')
|
|||||||
// NOTE: when a getter is pending (promise), all consecutive calls
|
// NOTE: when a getter is pending (promise), all consecutive calls
|
||||||
// will resolve the original getter return value...
|
// will resolve the original getter return value...
|
||||||
//
|
//
|
||||||
// Get cached result and do "lazy" background update... (XXX EXPERIMENTAL)
|
// Get sync or cached result and do "lazy" background update...
|
||||||
// <index-handler>('lazy')
|
// <index-handler>('lazy')
|
||||||
// -> <data>
|
// -> <data>
|
||||||
// -> <promise>
|
// -> <promise>
|
||||||
|
// NOTE: if <index-handler>(..) is synchronous, this will wait till
|
||||||
|
// it returns and will return the result.
|
||||||
// NOTE: 'lazy' mode is generally faster as it does all the checks and
|
// NOTE: 'lazy' mode is generally faster as it does all the checks and
|
||||||
// updating (if needed) in a background promise, but can return
|
// updating (if needed) in a background promise, but can return
|
||||||
// outdated cached results.
|
// outdated cached results.
|
||||||
@ -52,6 +54,16 @@ var pwpath = require('../path')
|
|||||||
// value is available. i.e. a promise is returned only when
|
// value is available. i.e. a promise is returned only when
|
||||||
// getting/generating a value for the first time.
|
// getting/generating a value for the first time.
|
||||||
//
|
//
|
||||||
|
// Get cached result and trigger a background update...
|
||||||
|
// <index-handler>('cached')
|
||||||
|
// -> <data>
|
||||||
|
// -> <promise>
|
||||||
|
// -> undefined
|
||||||
|
// NOTE: this is like 'lazy' but will not wait for <index-handler>)(..)
|
||||||
|
// to return, making it even faster but as a trade off it will
|
||||||
|
// return the cached and possibly outdated result even if
|
||||||
|
// <index-handler>(..) is synchronous.
|
||||||
|
//
|
||||||
// Get local data (uncached)...
|
// Get local data (uncached)...
|
||||||
// <index-handler>('local')
|
// <index-handler>('local')
|
||||||
// -> <data>
|
// -> <data>
|
||||||
@ -65,13 +77,19 @@ var pwpath = require('../path')
|
|||||||
// -> <data>
|
// -> <data>
|
||||||
// -> <promise>
|
// -> <promise>
|
||||||
//
|
//
|
||||||
|
// Get index status...
|
||||||
|
// <index-handler>('status')
|
||||||
|
// -> 'empty'
|
||||||
|
// -> 'pending'
|
||||||
|
// -> 'cached'
|
||||||
|
// -> 'outdated'
|
||||||
|
//
|
||||||
// Run custom action...
|
// Run custom action...
|
||||||
// <index-handler>(<action-name>), ...)
|
// <index-handler>(<action-name>), ...)
|
||||||
// -> <data>
|
// -> <data>
|
||||||
// -> <promise>
|
// -> <promise>
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
//
|
|
||||||
// Special methods:
|
// Special methods:
|
||||||
//
|
//
|
||||||
// Special method to generate local <data>...
|
// Special method to generate local <data>...
|
||||||
@ -147,11 +165,11 @@ function(name, generate, options={}){
|
|||||||
: !!options.attr ?
|
: !!options.attr ?
|
||||||
name
|
name
|
||||||
: `__${name}_cache`
|
: `__${name}_cache`
|
||||||
|
var modified = `__${name}_modified`
|
||||||
|
var promise = `__${name}_promise`
|
||||||
var test = `__${name}_isvalid__`
|
var test = `__${name}_isvalid__`
|
||||||
var merge = `__${name}_merge__`
|
var merge = `__${name}_merge__`
|
||||||
var special = `__${name}__`
|
var special = `__${name}__`
|
||||||
var modified = `__${name}_modified`
|
|
||||||
var promise = `__${name}_promise`
|
|
||||||
|
|
||||||
// set modified time...
|
// set modified time...
|
||||||
var _stamp = function(that, res){
|
var _stamp = function(that, res){
|
||||||
@ -184,23 +202,50 @@ function(name, generate, options={}){
|
|||||||
delete obj[promise] }) }
|
delete obj[promise] }) }
|
||||||
val = obj[promise] }
|
val = obj[promise] }
|
||||||
return val }
|
return val }
|
||||||
|
var _deferred = async function(obj, ...args){
|
||||||
|
return meth.call(obj, ...args) }
|
||||||
|
|
||||||
// build the method...
|
// build the method...
|
||||||
var meth
|
var meth
|
||||||
return (meth = Object.assign(
|
return (meth = Object.assign(
|
||||||
function(action='get', ...args){
|
function(action='get', ...args){
|
||||||
var that = this
|
var that = this
|
||||||
// XXX EXPERIMENTAL...
|
|
||||||
|
// action: status...
|
||||||
|
if(action == 'status'){
|
||||||
|
if(this[cache] instanceof Promise){
|
||||||
|
return 'pending' }
|
||||||
|
if(cache in this){
|
||||||
|
var cur = this[modified]
|
||||||
|
// user test...
|
||||||
|
if(test in this
|
||||||
|
&& !this[test](cur)){
|
||||||
|
return 'outdated'
|
||||||
|
// check dependencies...
|
||||||
|
} else if(meth.options.depends){
|
||||||
|
for(var dep of meth.options.depends){
|
||||||
|
if(this[`__${this[dep].index}_modified`] > cur){
|
||||||
|
return 'outdated' } } }
|
||||||
|
return 'cached' }
|
||||||
|
return 'empty' }
|
||||||
|
|
||||||
// action: lazy...
|
// action: lazy...
|
||||||
if(action == 'lazy'){
|
if(action == 'lazy'){
|
||||||
if(this[cache] instanceof Promise){
|
if(this[cache] instanceof Promise){
|
||||||
return this[cache] }
|
return this[cache] }
|
||||||
var res = meth('get')
|
var res = meth.call(this, 'get')
|
||||||
return (this[cache]
|
return (this[cache]
|
||||||
&& res instanceof Promise) ?
|
&& res instanceof Promise) ?
|
||||||
this[cache]
|
this[cache]
|
||||||
: res }
|
: res }
|
||||||
|
|
||||||
|
// action: cached...
|
||||||
|
if(action == 'cached'){
|
||||||
|
_deferred(this, 'get')
|
||||||
|
return this[cache] }
|
||||||
|
|
||||||
// action: local...
|
// action: local...
|
||||||
|
// NOTE: this "cascade" of actions is interdependent...
|
||||||
// NOTE: this is intentionally not cached...
|
// NOTE: this is intentionally not cached...
|
||||||
if(action == 'local'){
|
if(action == 'local'){
|
||||||
return _make(this) }
|
return _make(this) }
|
||||||
@ -211,19 +256,12 @@ function(name, generate, options={}){
|
|||||||
// action: clear...
|
// action: clear...
|
||||||
if(action == 'clear'){
|
if(action == 'clear'){
|
||||||
return }
|
return }
|
||||||
|
|
||||||
// validate cache...
|
// validate cache...
|
||||||
if(cache in this){
|
if(cache in this
|
||||||
var cur = this[modified]
|
&& meth.call(this, 'status') == 'outdated'){
|
||||||
// user test...
|
delete this[cache] }
|
||||||
if(test in this
|
|
||||||
&& !this[test](cur)){
|
|
||||||
delete this[cache]
|
|
||||||
// check dependencies...
|
|
||||||
} else if(meth.options.depends){
|
|
||||||
for(var dep of meth.options.depends){
|
|
||||||
if(this[`__${this[dep].index}_modified`] > cur){
|
|
||||||
delete this[cache]
|
|
||||||
break } } } }
|
|
||||||
// action: other...
|
// action: other...
|
||||||
if(action != 'get'
|
if(action != 'get'
|
||||||
&& action != 'reset'){
|
&& action != 'reset'){
|
||||||
@ -243,6 +281,7 @@ function(name, generate, options={}){
|
|||||||
res !== cur
|
res !== cur
|
||||||
&& _stamp(this, res)
|
&& _stamp(this, res)
|
||||||
return res }
|
return res }
|
||||||
|
|
||||||
// action: get...
|
// action: get...
|
||||||
return _await(this,
|
return _await(this,
|
||||||
this[cache] =
|
this[cache] =
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user