mirror of
https://github.com/flynx/pWiki.git
synced 2025-10-29 18:10:09 +00:00
refactored update/delete...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
480bf534ba
commit
6ab0aa4fd5
@ -91,7 +91,7 @@ var index = require('../index')
|
|||||||
//
|
//
|
||||||
//
|
//
|
||||||
// XXX potential architectural problems:
|
// XXX potential architectural problems:
|
||||||
// - .paths()
|
// - .paths
|
||||||
// external index -- is this good???
|
// external index -- is this good???
|
||||||
// bottleneck??
|
// bottleneck??
|
||||||
// cache/index???
|
// cache/index???
|
||||||
@ -108,8 +108,6 @@ module.BaseStore = {
|
|||||||
// XXX revise naming...
|
// XXX revise naming...
|
||||||
next: undefined,
|
next: undefined,
|
||||||
|
|
||||||
onUpdate: types.event.Event('update'),
|
|
||||||
onDelete: types.event.Event('delete'),
|
|
||||||
|
|
||||||
// NOTE: .data is not part of the spec and can be implementation-specific,
|
// NOTE: .data is not part of the spec and can be implementation-specific,
|
||||||
// only .__<name>__(..) use it internally... (XXX check this)
|
// only .__<name>__(..) use it internally... (XXX check this)
|
||||||
@ -161,6 +159,8 @@ module.BaseStore = {
|
|||||||
get paths(){
|
get paths(){
|
||||||
return this.__paths() },
|
return this.__paths() },
|
||||||
|
|
||||||
|
__names_isvalid__: function(t){
|
||||||
|
return this.__paths_isvalid__(t) },
|
||||||
// NOTE: this is built from .paths so there is no need to define a
|
// NOTE: this is built from .paths so there is no need to define a
|
||||||
// way to merge...
|
// way to merge...
|
||||||
__names: index.makeIndex('names',
|
__names: index.makeIndex('names',
|
||||||
@ -197,7 +197,8 @@ module.BaseStore = {
|
|||||||
return this.__names() },
|
return this.__names() },
|
||||||
|
|
||||||
// XXX tags
|
// XXX tags
|
||||||
// XXX search
|
|
||||||
|
// XXX text search index
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -235,7 +236,6 @@ module.BaseStore = {
|
|||||||
return false }
|
return false }
|
||||||
return pwpath.joinArgs(res, args) },
|
return pwpath.joinArgs(res, args) },
|
||||||
// find the closest existing alternative path...
|
// find the closest existing alternative path...
|
||||||
// XXX CACHED....
|
|
||||||
find: async function(path, strict=false){
|
find: async function(path, strict=false){
|
||||||
var {path, args} = pwpath.splitArgs(path)
|
var {path, args} = pwpath.splitArgs(path)
|
||||||
args = pwpath.joinArgs('', args)
|
args = pwpath.joinArgs('', args)
|
||||||
@ -255,12 +255,6 @@ module.BaseStore = {
|
|||||||
: '/'+p
|
: '/'+p
|
||||||
if(pages.has(p)){
|
if(pages.has(p)){
|
||||||
return p+args } } },
|
return p+args } } },
|
||||||
/*/
|
|
||||||
find: async function(path, strict=false){
|
|
||||||
for(var p of pwpath.paths(path, !!strict)){
|
|
||||||
if(p = await this.exists(p)){
|
|
||||||
return p } } },
|
|
||||||
//*/
|
|
||||||
//
|
//
|
||||||
// Resolve page for path
|
// Resolve page for path
|
||||||
// .match(<path>)
|
// .match(<path>)
|
||||||
@ -303,15 +297,7 @@ module.BaseStore = {
|
|||||||
// dir for hidden tests...
|
// dir for hidden tests...
|
||||||
.replace(/(^|\\\/+)(\\\.|)([^\/]*)\\\*/g, '$1$2($3[^\\/]*)')
|
.replace(/(^|\\\/+)(\\\.|)([^\/]*)\\\*/g, '$1$2($3[^\\/]*)')
|
||||||
+'(?=[\\/]|$)', 'g')
|
+'(?=[\\/]|$)', 'g')
|
||||||
/*/ XXX CACHED....
|
|
||||||
var name = pwpath.basename(path)
|
|
||||||
return [...(name.includes('*') ?
|
|
||||||
await this.paths()
|
|
||||||
: await (this.names())[name])
|
|
||||||
/*/
|
|
||||||
//return [...(await this.paths())
|
|
||||||
return [...(await this.paths)
|
return [...(await this.paths)
|
||||||
//*/
|
|
||||||
// NOTE: we are not using .filter(..) here as wee
|
// NOTE: we are not using .filter(..) here as wee
|
||||||
// need to keep parts of the path only and not
|
// need to keep parts of the path only and not
|
||||||
// return the whole thing...
|
// return the whole thing...
|
||||||
@ -360,7 +346,6 @@ module.BaseStore = {
|
|||||||
// -> ['System/tree', 'Dir/tree', ...]
|
// -> ['System/tree', 'Dir/tree', ...]
|
||||||
//
|
//
|
||||||
// XXX should this be used by .get(..) instead of .match(..)???
|
// XXX should this be used by .get(..) instead of .match(..)???
|
||||||
// XXX EXPERIMENTAL
|
|
||||||
resolve: async function(path, strict){
|
resolve: async function(path, strict){
|
||||||
// pattern match * / **
|
// pattern match * / **
|
||||||
if(path.includes('*')
|
if(path.includes('*')
|
||||||
@ -402,7 +387,7 @@ module.BaseStore = {
|
|||||||
//
|
//
|
||||||
// XXX should this call actions???
|
// XXX should this call actions???
|
||||||
// XXX should this return a map for pattern matches???
|
// XXX should this return a map for pattern matches???
|
||||||
__get__: async function(key){
|
__get__: function(key){
|
||||||
return this.data[key] },
|
return this.data[key] },
|
||||||
get: async function(path, strict=false, energetic=false){
|
get: async function(path, strict=false, energetic=false){
|
||||||
var that = this
|
var that = this
|
||||||
@ -423,10 +408,9 @@ module.BaseStore = {
|
|||||||
// and returning a a/b which can be undefined...
|
// and returning a a/b which can be undefined...
|
||||||
return that.get(p, strict) })
|
return that.get(p, strict) })
|
||||||
: (await this.__get__(path)
|
: (await this.__get__(path)
|
||||||
?? ((this.next || {}).__get__
|
?? ((this.next || {}).get
|
||||||
&& this.next.get(path, strict))) },
|
&& this.next.get(path, strict))) },
|
||||||
|
|
||||||
// XXX EXPERIMENTAL...
|
|
||||||
isEnergetic: async function(path){
|
isEnergetic: async function(path){
|
||||||
var p = await this.find(path)
|
var p = await this.find(path)
|
||||||
return !!(await this.get(p, true) ?? {}).energetic
|
return !!(await this.get(p, true) ?? {}).energetic
|
||||||
@ -470,7 +454,7 @@ module.BaseStore = {
|
|||||||
// XXX do we copy the data here or modify it????
|
// XXX do we copy the data here or modify it????
|
||||||
__update__: async function(key, data, mode='update'){
|
__update__: async function(key, data, mode='update'){
|
||||||
this.data[key] = data },
|
this.data[key] = data },
|
||||||
update: async function(path, data, mode='update'){
|
__update: async function(path, data, mode='update'){
|
||||||
// read-only...
|
// read-only...
|
||||||
if(this.__update__ == null){
|
if(this.__update__ == null){
|
||||||
return this }
|
return this }
|
||||||
@ -495,13 +479,20 @@ module.BaseStore = {
|
|||||||
data,
|
data,
|
||||||
{mtime: Date.now()})
|
{mtime: Date.now()})
|
||||||
await this.__update__(path, data, mode)
|
await this.__update__(path, data, mode)
|
||||||
// XXX INDEX
|
this.index('update', path, data, mode)
|
||||||
this.index('update', path)
|
|
||||||
this.onUpdate(path)
|
|
||||||
return this },
|
return this },
|
||||||
|
// XXX can we do a blanket .index('update', ...) here??
|
||||||
|
// ...currently this will mess up caches between .next/.substores
|
||||||
|
// and the top level store to an inconsistent state...
|
||||||
|
// ...this could be a sign of problems with index -- needs more
|
||||||
|
// tought...
|
||||||
|
update: types.event.Event('update',
|
||||||
|
function(handler, path, data, mode='update'){
|
||||||
|
return this.__update(...[...arguments].slice(1)) }),
|
||||||
|
|
||||||
__delete__: async function(path){
|
__delete__: async function(path){
|
||||||
delete this.data[path] },
|
delete this.data[path] },
|
||||||
delete: async function(path){
|
__delete: async function(path){
|
||||||
// read-only...
|
// read-only...
|
||||||
if(this.__delete__ == null){
|
if(this.__delete__ == null){
|
||||||
return this }
|
return this }
|
||||||
@ -509,10 +500,12 @@ module.BaseStore = {
|
|||||||
path = await this.exists(path)
|
path = await this.exists(path)
|
||||||
if(typeof(path) == 'string'){
|
if(typeof(path) == 'string'){
|
||||||
await this.__delete__(path)
|
await this.__delete__(path)
|
||||||
// XXX INDEX
|
|
||||||
this.index('remove', path)
|
this.index('remove', path)
|
||||||
this.onDelete(path) }
|
this.onDelete(path) }
|
||||||
return this },
|
return this },
|
||||||
|
delete: types.event.Event('delete',
|
||||||
|
function(handler, path){
|
||||||
|
return this.__delete(path) }),
|
||||||
|
|
||||||
// XXX NEXT might be a good idea to have an API to move pages from
|
// XXX NEXT might be a good idea to have an API to move pages from
|
||||||
// current store up the chain...
|
// current store up the chain...
|
||||||
@ -521,7 +514,7 @@ module.BaseStore = {
|
|||||||
//
|
//
|
||||||
// The .load(..) / .json(..) methods have two levels of implementation:
|
// The .load(..) / .json(..) methods have two levels of implementation:
|
||||||
// - generic
|
// - generic
|
||||||
// uses .update(..) and .paths()/.get(..) and is usable as-is
|
// uses .update(..) and .paths/.get(..) and is usable as-is
|
||||||
// in any store adapter implementing the base protocol.
|
// in any store adapter implementing the base protocol.
|
||||||
// - batch
|
// - batch
|
||||||
// implemented via .__batch_load__(..) and .__batch_json__(..)
|
// implemented via .__batch_load__(..) and .__batch_json__(..)
|
||||||
@ -555,7 +548,7 @@ module.BaseStore = {
|
|||||||
// NOTE: this will not serialize functions...
|
// NOTE: this will not serialize functions...
|
||||||
//__batch_json__: function(){
|
//__batch_json__: function(){
|
||||||
// // ...
|
// // ...
|
||||||
// return json},
|
// return json },
|
||||||
json: async function(options={}){
|
json: async function(options={}){
|
||||||
if(options === true){
|
if(options === true){
|
||||||
options = {stringify: true} }
|
options = {stringify: true} }
|
||||||
@ -566,7 +559,6 @@ module.BaseStore = {
|
|||||||
// generic...
|
// generic...
|
||||||
} else {
|
} else {
|
||||||
var res = {}
|
var res = {}
|
||||||
//for(var path of await this.paths()){
|
|
||||||
for(var path of await this.paths){
|
for(var path of await this.paths){
|
||||||
var page = await this.get(path)
|
var page = await this.get(path)
|
||||||
if(keep_funcs
|
if(keep_funcs
|
||||||
@ -578,7 +570,6 @@ module.BaseStore = {
|
|||||||
: res },
|
: res },
|
||||||
}
|
}
|
||||||
|
|
||||||
//index.IndexManagerMixin(BaseStore)
|
|
||||||
|
|
||||||
|
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
@ -720,13 +711,14 @@ module.MetaStore = {
|
|||||||
// ...should copy and merge...
|
// ...should copy and merge...
|
||||||
metadata: metaProxy('metadata'),
|
metadata: metaProxy('metadata'),
|
||||||
// NOTE: we intentionally do not delegate to .next here...
|
// NOTE: we intentionally do not delegate to .next here...
|
||||||
update: async function(path, data, mode='update'){
|
__update: async function(path, data, mode='update'){
|
||||||
data = data instanceof Promise ?
|
data = data instanceof Promise ?
|
||||||
await data
|
await data
|
||||||
: data
|
: data
|
||||||
// add substore...
|
// add substore...
|
||||||
if(object.childOf(data, BaseStore)){
|
if(object.childOf(data, BaseStore)){
|
||||||
path = pwpath.sanitize(path, 'string')
|
path = pwpath.sanitize(path, 'string')
|
||||||
|
//data.index('clear')
|
||||||
;(this.substores = this.substores ?? {})[path] = data
|
;(this.substores = this.substores ?? {})[path] = data
|
||||||
return this }
|
return this }
|
||||||
// add to substore...
|
// add to substore...
|
||||||
@ -736,15 +728,12 @@ module.MetaStore = {
|
|||||||
// trim path...
|
// trim path...
|
||||||
path.slice(path.indexOf(p)+p.length),
|
path.slice(path.indexOf(p)+p.length),
|
||||||
...[...arguments].slice(1))
|
...[...arguments].slice(1))
|
||||||
//this.__cache_add(path)
|
|
||||||
// XXX INDEX
|
|
||||||
this.index('update', path)
|
|
||||||
return this }
|
return this }
|
||||||
// add local...
|
// add local...
|
||||||
return object.parentCall(MetaStore.update, this, ...arguments) },
|
return object.parentCall(MetaStore.__update, this, ...arguments) },
|
||||||
// XXX Q: how do we delete a substore???
|
// XXX Q: how do we delete a substore???
|
||||||
// XXX need to call .__cache_remove(..) here if we did not super-call...
|
// XXX need to call .__cache_remove(..) here if we did not super-call...
|
||||||
delete: metaProxy('delete'),
|
__delete: metaProxy('__delete'),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -791,10 +780,10 @@ module.CachedStore = {
|
|||||||
return this.cache[path]
|
return this.cache[path]
|
||||||
?? (this.cache[path] =
|
?? (this.cache[path] =
|
||||||
await object.parentCall(CachedStore.get, this, ...arguments)) },
|
await object.parentCall(CachedStore.get, this, ...arguments)) },
|
||||||
update: async function(path, data){
|
__update: async function(path, data){
|
||||||
var that = this
|
var that = this
|
||||||
delete this.cache[path]
|
delete this.cache[path]
|
||||||
var res = object.parentCall(CachedStore.update, this, ...arguments)
|
var res = object.parentCall(CachedStore.__update, this, ...arguments)
|
||||||
// re-cache in the background...
|
// re-cache in the background...
|
||||||
res.then(async function(){
|
res.then(async function(){
|
||||||
that.cache[path] = await that.get(path) })
|
that.cache[path] = await that.get(path) })
|
||||||
@ -810,9 +799,9 @@ module.CachedStore = {
|
|||||||
?? (this.cache[path] =
|
?? (this.cache[path] =
|
||||||
await object.parentCall(CachedStore.metadata, this, ...arguments)) } },
|
await object.parentCall(CachedStore.metadata, this, ...arguments)) } },
|
||||||
//*/
|
//*/
|
||||||
delete: async function(path){
|
__delete: async function(path){
|
||||||
delete this.cache[path]
|
delete this.cache[path]
|
||||||
return object.parentCall(CachedStore.delete, this, ...arguments) },
|
return object.parentCall(CachedStore.__delete, this, ...arguments) },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -386,6 +386,7 @@ require(['./browser'], function(browser){
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
// Export...
|
// Export...
|
||||||
|
|
||||||
|
|||||||
@ -17,8 +17,6 @@
|
|||||||
* - CLI
|
* - CLI
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* XXX INDEX should we update the index in-method or on event????
|
|
||||||
* ....i.e. in .update(..)/.delete(..) vs. .onUpdate(..)/.onDelete(..) ???
|
|
||||||
* XXX INDEX DOC can index validation be async???
|
* XXX INDEX DOC can index validation be async???
|
||||||
* ...likely no
|
* ...likely no
|
||||||
* XXX INDEX add option to set default action (get/lazy/cached)
|
* XXX INDEX add option to set default action (get/lazy/cached)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user