From 72e60e41252f53de8259d3027ef974ffb575b7f0 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sat, 22 Oct 2022 03:10:41 +0300 Subject: [PATCH] tweaking and bug-fixes... Signed-off-by: Alex A. Naanou --- pwiki/store/base.js | 71 +++++++++++++++++++++++---------------------- pwiki2.js | 10 ------- 2 files changed, 36 insertions(+), 45 deletions(-) diff --git a/pwiki/store/base.js b/pwiki/store/base.js index 8c588c8..5cd306d 100755 --- a/pwiki/store/base.js +++ b/pwiki/store/base.js @@ -67,7 +67,7 @@ var pwpath = require('../path') // -> // // Test if cache is valid... -// .___test__() +// .___isvalid__() // -> // // Handle custom action... @@ -110,7 +110,7 @@ var pwpath = require('../path') // // // XXX do we separate internal methods and actions??? -// i.e. ___merge__(..) / ___test__(..) and the rest... +// i.e. ___merge__(..) / ___isvalid__(..) and the rest... var makeIndex = module.makeIndex = function(name, generate, options={}){ @@ -128,20 +128,25 @@ function(name, generate, options={}){ : !!options.attr ? name : `__${name}_cache` + var test = `__${name}_isvalid__` var merge = `__${name}_merge__` - var test = `__${name}_test__` var special = `__${name}__` var modified = `__${name}_modified` - // make local cache... - var _make = function(){ - var res = - this[special] != null ? - this[special]() - : (generate - && generate.call(this)) - this[modified] = Date.now() + // set modified time... + var _stamp = function(that, res){ + res instanceof Promise ? + res.then(function(){ + that[modified] = Date.now() }) + : (that[modified] = Date.now()) return res } + // make local cache... + var _make = function(that){ + return _stamp(that, + that[special] != null ? + that[special]() + : (generate + && generate.call(that))) } // unwrap a promised value into cache... var _await = function(obj, val){ if(val instanceof Promise){ @@ -191,13 +196,13 @@ function(name, generate, options={}){ options[action].call(this, cur, ...args) : cur) res !== cur - && (this[modified] = Date.now()) + && _stamp(this, res) return res } // action: get/local... return _await(this, // NOTE: this is intentionally not cached... action == 'local' ? - _make.call(this) + _make(this) // get... : (this[cache] = // cached... @@ -205,9 +210,11 @@ function(name, generate, options={}){ this[cache] // generate + merge... : this[merge] != null ? - this[merge](_make.call(this)) + // NOTE: need to set the timestamp after the merge... + _stamp(this, + this[merge](_make(this))) // generate... - : _make.call(this)) ) }, + : _make(this)) ) }, { index: name, indexed: true, @@ -472,7 +479,7 @@ module.BaseStore = { && 'xpaths' in this.next) ? await this.next.xpaths : []) }, - __xpaths_test__: function(t){ + __xpaths_isvalid__: function(t){ var changed = !!this.__xpaths_next_exists != !!this.next || (!!this.next @@ -1069,25 +1076,19 @@ module.MetaStore = { return object.parentCall(MetaStore.__xpaths_merge__, this, ...arguments) .iter() .concat(stores) }, - // XXX BUG: - // (reload) - // pwiki.store.xnames - // pwiki.store.substores['Stores/memory'].index('update', 'a/b/c/xxx', {}) - // pwiki.store.xnames // does not change... - __xpaths_test__: function(t){ - if(!this.substores){ - return true } - // match substore list... - var cur = Object.keys(this.substores ?? {}) - var prev = this.__xpaths_substores ?? cur ?? [] - if(prev.length != cur.length - || (new Set([...cur, ...prev])).length != cur.length){ - return false } - // check timestamps... - for(var store of Object.values(this.substores ?? {})){ - if(store.__xpaths_modified > t){ - return false } } - return object.parentCall(MetaStore.__xpaths_test__, this, ...arguments) }, + __xpaths_isvalid__: function(t){ + if(this.substores){ + // match substore list... + var cur = Object.keys(this.substores ?? {}) + var prev = this.__xpaths_substores ?? cur ?? [] + if(prev.length != cur.length + || (new Set([...cur, ...prev])).size != cur.length){ + return false } + // check timestamps... + for(var {__xpaths_modified} of Object.values(this.substores ?? {})){ + if(__xpaths_modified > t){ + return false } } } + return object.parentCall(MetaStore.__xpaths_isvalid__, this, ...arguments) }, paths: async function(){ var that = this diff --git a/pwiki2.js b/pwiki2.js index 59a2ff5..c807dc3 100755 --- a/pwiki2.js +++ b/pwiki2.js @@ -17,7 +17,6 @@ * - CLI * * -* XXX odd bug: in node .xpaths seems to get invalidated on each call... * XXX INDEX / CACHE: * - centralized * - nestable @@ -27,15 +26,6 @@ * name, generate, get (extensible), update, delete * - event-based??? * - sync??? -* API???: -* // create -* .index(, {generate, update, ... }) -* delete .index. -* . - index data (prop) -* .____() - index getter -* .index.(..) - update -* .index.clear() -* .index.refresh() * XXX BUG: when editing the root page of a substore the page's .cache is * not reset for some reason... * ...the problem is in that .names() cache is not reset when a new