experimenting with journalling...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-11-07 16:01:53 +03:00
parent 2dbf814bf4
commit 6606373142
3 changed files with 87 additions and 6 deletions

View File

@ -16,8 +16,17 @@ var object = require('ig-object')
// makeIndex(<name>, <generate>[, <options>])
// -> <index-handler>
//
// Get merged data (cached)
// Call/get
// <index-handler>()
// -> <data>
// -> <promise>
//
// Call the index handler method...
// <index-handler>('__call__', ..)
// -> ...
// -> <promise>
//
// Get merged data (cached)
// <index-handler>('get')
// -> <data>
// -> <promise>
@ -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,

View File

@ -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(<path>)
// -> <normalized-path>

View File

@ -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...