experimenting....

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-05-06 18:27:41 +03:00
parent 6d76b0cb64
commit b580c6b0c3

128
pwiki2.js
View File

@ -148,9 +148,13 @@ module.path = {
// To create a store adapter: // To create a store adapter:
// - inherit from BaseStore // - inherit from BaseStore
// - overload: // - overload:
// .__paths__(..) // .__paths__()
// -> <keys>
// .__exists__(..) // .__exists__(..)
// -> false
// -> <path>
// .__get__(..) // .__get__(..)
// -> <data>
// - optionally (for writable stores) // - optionally (for writable stores)
// .__update__(..) // .__update__(..)
// .__set__(..) // .__set__(..)
@ -231,22 +235,26 @@ module.BaseStore = {
__exists__: function(path){ __exists__: function(path){
var data = this.data var data = this.data
return (path in data return (path in data
&& path) && path) },
// NOTE: all paths at this point and in store are absolute,
// so we check both with the leading '/' and without
// it to make things a bit more relaxed and return the
// actual matching path...
|| (path[0] == '/' ?
(path.slice(1) in data
&& path.slice(1))
: (('/'+ path) in data)
&& '/'+path) },
exists: function(path){ exists: function(path){
return this.__exists__(module.path.normalize(path, 'string')) path = module.path.normalize(path, 'string')
// delegate to .next... return this.__exists__(path, 'string')
// NOTE: all paths at this point and in store are
// absolute, so we check both with the leading
// '/' and without it to make things a bit more
// relaxed and return the actual matching path...
|| this.__exists__(
path[0] == '/' ?
path.slice(1)
: ('/'+ path))
// XXX NEXT // XXX NEXT
// delegate to .next...
|| ((this.next || {}).__exists__ || ((this.next || {}).__exists__
&& this.next.__exists__(path)) }, && (this.next.__exists__(path)
|| this.next.__exists__(
path[0] == '/' ?
path.slice(1)
: ('/'+ path)))) },
/*/ XXX do we actually need this??? /*/ XXX do we actually need this???
// ...this is the same as .get('**') // ...this is the same as .get('**')
@ -277,7 +285,6 @@ module.BaseStore = {
// match all sub-paths. // match all sub-paths.
// //
match: function(path, strict=false){ match: function(path, strict=false){
var data = this.data
// pattern match * / ** // pattern match * / **
if(path.includes('*') if(path.includes('*')
|| path.includes('**')){ || path.includes('**')){
@ -325,7 +332,6 @@ module.BaseStore = {
return this.data[key] }, return this.data[key] },
get: function(path, strict=false){ get: function(path, strict=false){
var that = this var that = this
var data = this.data
path = this.match(path, strict) path = this.match(path, strict)
return path instanceof Array ? return path instanceof Array ?
// XXX should we return matched paths??? // XXX should we return matched paths???
@ -418,43 +424,79 @@ module.store =
// XXX EXPERIMENTAL // XXX EXPERIMENTAL
var localStorageStore = var localStorageStore =
module.localStorageStore = { module.localStorageStore = {
__proto__: BaseParser, __proto__: BaseStore,
// XXX __prefix__: '--pwiki:',
__store__:
// XXX add caching of unserialized data...
data:
typeof(localStorage) != 'undefined' ? typeof(localStorage) != 'undefined' ?
localStorage localStorage
: undefined, : undefined,
// XXX key to store data under...
// ....if undefined then each page will be stored as a root path...
__key__: 'pWiki-data',
__data: undefined, __paths__: function(path){
get data(){ var that = this
return this.__key__ ? return Object.keys(this.data)
(this.__data = this.__data .map(function(k){
?? JSON.parse(this.__store__[this.__key__])) return k.startsWith(that.__prefix__) ?
: this.__store__ }, k.slice((that.__prefix__ ?? '').length)
: [] })
//__paths__: function(path){}, .flat() },
//__exists__: function(path){}, __exists__: function(path){
return ((this.__prefix__ ?? '')+ path) in this.data
&& path },
__get__: function(path){ __get__: function(path){
return this.__key__ ? path = (this.__prefix__ ?? '')+ path
this.data[path] return path in this.data ?
// XXX CACHE... JSON.parse(this.data[path])
: JSON.parse(this.data[path]) }, : undefined },
// XXX *time... __set__: function(path, data={}){
__set__: function(path, data){ path = (this.__prefix__ ?? '')+ path
this.data[path] = this.__key__ ? var t = Date.now()
data this.data[path] =
: JSON.stringify(data) }, JSON.stringify(
__update__: function(){ Object.assign(
// XXX {ctime: t},
data,
{mtime: t}))
return this }, return this },
delete: function(){}, // XXX for some magical reason this overwrites both ctime and mtime...
__update__: function(path, data={}){
path = (this.__prefix__ ?? '')+ path
var t = Date.now()
this.data[path] =
JSON.stringify(
Object.assign(
{ctime: t},
this.__get__(path),
data,
{mtime: t}))
return this },
// XXX
delete: function(){
},
load: function(){}, load: function(){},
} }
var localStorageNestedStore =
module.localStorageNestedStore = {
__proto__: BaseStore,
__data__: '__pwiki_data__',
__cache__: '__pwiki_cache__',
__data: undefined,
get data(){
return this.__data
?? (this.__data =
Object.assign(
{ __proto__: JSON.parse(localStorage[this.__data__] || '{}') },
JSON.parse(localStorage[this.__cache__] || '{}') )) },
// XXX do partials saves -> cache + write cache...
// XXX on full save merge cache and save...
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -