diff --git a/pwiki/index.js b/pwiki/index.js index ab3add5..c49f097 100755 --- a/pwiki/index.js +++ b/pwiki/index.js @@ -16,8 +16,17 @@ var object = require('ig-object') // makeIndex(, [, ]) // -> // -// Get merged data (cached) +// Call/get // () +// -> +// -> +// +// Call the index handler method... +// ('__call__', ..) +// -> ... +// -> +// +// Get merged data (cached) // ('get') // -> // -> @@ -202,9 +211,15 @@ function(name, generate, options={}){ // build the method... var meth return (meth = Object.assign( - function(action='get', ...args){ + function(action, ...args){ var that = this + action = action === undefined ? + ('__call__' in options ? + '__call__' + : 'get') + : action + // action: status... if(action == 'status'){ if(this[cache] instanceof Promise){ @@ -257,6 +272,7 @@ function(name, generate, options={}){ // action: other... if(action != 'get' + && action != '__call__' && action != 'reset'){ var action_meth = `__${name}_${action}__` // generate cache if not available... @@ -277,8 +293,8 @@ function(name, generate, options={}){ && _stamp(this, res) return res } - // action: get... - return _await(this, + // get/generate the data... + var res = _await(this, this[cache] = // cached... this[cache] != null ? @@ -289,7 +305,16 @@ function(name, generate, options={}){ _stamp(this, this[merge](_make(this))) // generate... - : _smake(this)) }, + : _smake(this)) + + // action: call... + // NOTE: this directly returns the result to user but will + // not automatically influence the stored value... + if(action == '__call__'){ + return options.__call__.call(this, res, name, ...args) } + + // action: get... + return res }, { index: name, indexed: true, diff --git a/pwiki/store/base.js b/pwiki/store/base.js index cac07a2..f5b412d 100755 --- a/pwiki/store/base.js +++ b/pwiki/store/base.js @@ -430,6 +430,62 @@ module.BaseStore = { : s.search(...args) }, + // XXX EXPERIMENTAL... + // XXX Q: can we store journal data in a page??? + __journal: index.makeIndex('journal', + function(){ + // XXX stub... + var data = [] + return data }, { + + '__call__': function(data, name, from, to){ + if(typeof(from) == 'object'){ + var {from, to} = from } + var _get = function(data){ + return data + .filter(function(elem){ + return (!from + || elem[0] > from) + && (!to + || elem[0] <= to)}) } + return data instanceof Promise ? + data.then(_get) + : _get(data) }, + + // XXX do we need this??? + 'journal-clear': function(data, name){ + this.__journal('clear') + return data }, + + update: function(data, name, path, update){ + data.push([Date.now(), 'update', path, update]) + return data }, + remove: function(data, name, path){ + data.push([Date.now(), 'remove', path]) + return data }, + //reset: function(){ + //}, + save: function(data, name){ + // XXX move this out... + //var idb = require('idb-keyval') + // XXX + data.push([Date.now(), 'save']) + return data}, + load: function(data, name){ + // XXX move this out... + //var idb = require('idb-keyval') + // XXX should we clear the journal here??? + this.__journal('clear') + // load... + // XXX + + // XXX + data.push([Date.now(), 'load']) + return data}, }), + journal: function(){ + return this.__journal('__call__', ...arguments)}, + + // // .exists() // -> diff --git a/pwiki2.js b/pwiki2.js index 558c7f1..dbef26c 100755 --- a/pwiki2.js +++ b/pwiki2.js @@ -31,7 +31,7 @@ * XXX FEATURE: to avoid reinventing the wheel, serialize stuff as pages... * .paths - ??? * .names - ??? -* .tags - +* .tags - DONE * .search() - DONE * XXX index: pass name to all the handlers... * ...this will simplify generic handlers...