From 89301d116f4441e423a1b07b96d89bd485e454c2 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Fri, 17 Feb 2023 02:44:12 +0300 Subject: [PATCH] a new/better path search/aquisition algorithm... Signed-off-by: Alex A. Naanou --- browser.js | 2 - pwiki/path.js | 91 ++++++++++++++++++++++++++++++++++++++++----- pwiki/store/base.js | 5 ++- 3 files changed, 85 insertions(+), 13 deletions(-) diff --git a/browser.js b/browser.js index f1a9e90..055cbf0 100755 --- a/browser.js +++ b/browser.js @@ -44,8 +44,6 @@ module.store = { } //*/ -console.log('---', page.Config) - module.setup = Promise.all([ // static stores... diff --git a/pwiki/path.js b/pwiki/path.js index 6b3b952..4efd32f 100755 --- a/pwiki/path.js +++ b/pwiki/path.js @@ -254,16 +254,88 @@ module = { // // NOTE: if seen is given (when called recursively) this will not // search for .ALTERNATIVE_PAGES... - // NOTE: this will search for basename and each subpath, e.g: - // a/b/c - // -> a/b/c/d - // -> a/c/d - // -> c/d - // -> d - // // now search for 'c/d'... - // -> a/c/d - // -> ... // XXX should we keep the trailing '/'??? + // XXX EXPERIMENTAL... + paths: function*(path='/', strict=false, seen=new Set()){ + if(path === true || path === false){ + strict = path + path = '/' } + if(strict instanceof Set){ + seen = strict + strict = false } + var alt_pages = !strict + path = this.normalize(path, 'string') + // special case: root... + if(path == '/' || path == ''){ + // normalize... + path = '/' + // as-is... + seen.add(path) + yield path + // special case: root page... + if(this.ROOT_PAGE){ + yield* this.paths(this.normalize('/'+ this.ROOT_PAGE, 'string'), seen) }} + // NOTE: since path is already normalized we can trust the delimiter... + path = path.split(/\//g) + // normalize relative paths to root... + path[0] != '' + && path.unshift('') + // paths ending in '/'... + if(path[path.length-1] == ''){ + path.pop() + this.INDEX_PAGE + && path.push(this.INDEX_PAGE) } + + var searchPath = function*(path, search_paths){ + // full path... + var p = this.normalize(['', ...path], 'string') + if(!seen.has(p)){ + seen.add(p) + yield p } + + path = path.slice() + search_paths = search_paths instanceof Array ? + search_paths + : [search_paths] + // search up the levels... + var cur, sub, sub_cur + do{ + cur = [path.pop(), ...(cur ?? [])] + sub = cur.slice() + sub_cur = [] + // template paths... + for(var tpl of search_paths){ + // search the sub paths in the template dir... + var parent = ['', ...path] + do{ + sub_cur = [sub.pop(), ...(sub_cur ?? [])] + p = this.relative(parent, tpl +'/'+ sub_cur.join('/'), 'string') + if(!seen.has(p)){ + seen.add(p) + yield p } + }while(sub.length > 0) + } + // search the sub paths in the parent dir... + parent = parent.length > 1 ? + parent.slice(0, -1) + : parent + sub = cur.slice() + sub_cur = [] + do{ + sub_cur = [sub.pop(), ...(sub_cur ?? [])] + p = this.relative(parent, sub_cur.join('/'), 'string') + if(!seen.has(p)){ + seen.add(p) + yield p } + }while(sub.length > 0) + }while(path.length > 0) }.bind(this) + + yield* searchPath(path, this.SEARCH_PATHS) + yield* searchPath(path, this.SYSTEM_PATH) + if(alt_pages){ + for(var page of [...this.ALTERNATIVE_PAGES]){ + yield* this.paths(path.slice(0, -1).concat(page), true, seen) } } }, + /*/ paths: function*(path='/', strict=false){ if(path === true || path === false){ strict = path @@ -320,6 +392,7 @@ module = { if(alt_pages){ for(var page of [...this.ALTERNATIVE_PAGES]){ yield* this.paths(path.concat(page), seen) }} }, + //*/ names: function(path='/'){ path = path == '' ? diff --git a/pwiki/store/base.js b/pwiki/store/base.js index 9f0b262..3ccc12a 100755 --- a/pwiki/store/base.js +++ b/pwiki/store/base.js @@ -759,8 +759,9 @@ module.BaseStore = { // async attr... : typeof(cmp) == 'string' ? // NOTE: we only get page data once per page... - (d = d ?? that.get(p)) - .then(function(data){ + Promise.awaitOrRun( + (d = d ?? that.get(p)), + function(data){ return data[cmp] }) : null)) } _async = _async || !!d