revising item ID generation...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2019-05-29 16:27:14 +03:00
parent 852c343ee4
commit 21106d854c

View File

@ -55,6 +55,9 @@ var walk = require('../../node_modules/generic-walk/walk').walk
// ...this would enable us to uniquely identify the actual items // ...this would enable us to uniquely identify the actual items
// and prevent allot of specific errors... // and prevent allot of specific errors...
var collectItems = function(make, items){ var collectItems = function(make, items){
items = items instanceof Array ?
items
: [items]
var made = items var made = items
.filter(function(e){ .filter(function(e){
return e === make }) return e === make })
@ -546,6 +549,7 @@ var BaseBrowserClassPrototype = {
} }
// XXX need a way to identify items... // XXX need a way to identify items...
// ...try order + text if all else fails...
var BaseBrowserPrototype = { var BaseBrowserPrototype = {
// XXX should we mix item/list options or separate them into sub-objects??? // XXX should we mix item/list options or separate them into sub-objects???
options: { options: {
@ -612,6 +616,30 @@ var BaseBrowserPrototype = {
this.__item_index = value }, this.__item_index = value },
// XXX EXPERIMENTAL...
// these are two ways to create item ids...
get pathIndex(){
return this.reduce(function(index, e, i, p){
var id = p = p.join('/')
var c = 0
while(id in index){
id = p + ':' + (++c)
}
index[id] = e
return index
}, {}, {iterateAll: true}) },
get flatIndex(){
return this.reduce(function(index, e, i, p){
var id = e.value || e.id
var c = 0
while(id in index){
id = (e.value || e.id) + ':' + (++c)
}
index[id] = e
return index
}, {}, {iterateAll: true}) },
// XXX should we cache the value here???? // XXX should we cache the value here????
get focused(){ get focused(){
return this.get('focused') }, return this.get('focused') },
@ -731,26 +759,26 @@ var BaseBrowserPrototype = {
// ID generator... // ID generator...
// //
// .__id__()
// .__id__(prefix)
// .__id__(prefix, count)
// -> id
//
// Format: // Format:
// "<date>" // "<date>"
// "<prefix> <date>" // "<prefix> (<count>)"
// "<prefix> (<date>)"
// //
// XXX do a better id...
// Ex:
// "abc"
// "abc (1)"
// "abc (2)"
// XXX not sure about the logic of this, should this take an item as // XXX not sure about the logic of this, should this take an item as
// input and return an id??? // input and return an id???
// ...should this check for uniqueness??? // ...should this check for uniqueness???
// think merging this with any of the actual ID generators would be best... // think merging this with any of the actual ID generators would be best...
__id__: function(prefix){ __id__: function(prefix, count){
// id prefix... return prefix ?
return (prefix || '') // id prefix...
// separator... `${prefix} (${count || Date.now()})`
+ (prefix ? ' ' : '') // plain id...
// date... : `item${Date.now()}` },
+ Date.now() },
// Walk the browser... // Walk the browser...
@ -1732,6 +1760,7 @@ var BaseBrowserPrototype = {
// calls for such items... // calls for such items...
// //
// XXX revise options handling for .__list__(..) // XXX revise options handling for .__list__(..)
// XXX see .flatIndex (preffered) / .pathIndex for alternative id generation...
make: function(options){ make: function(options){
options = Object.assign(Object.create(this.options || {}), options || {}) options = Object.assign(Object.create(this.options || {}), options || {})
@ -1814,9 +1843,17 @@ var BaseBrowserPrototype = {
// to such elements in .__list__(..)... // to such elements in .__list__(..)...
// XXX can we go around this without requiring the user // XXX can we go around this without requiring the user
// to manage ids??? // to manage ids???
var k = key // XXX might be a good idea to handle id's in a separate pass
// to avoid odd things from happening with changed ids when
// items are re-ordered on the fly (via make.nest(..) and
// friends)
// i.e. handle id's in order of occurrence and not in order
// of make(..) calls...
// XXX see .flatIndex (preffered) / .pathIndex for alternative id generation...
var k = okey = key
var c = 0
while(k in new_index){ while(k in new_index){
// duplicate keys disabled... // duplicate keys not allowed...
if(options.noDuplicateValues){ if(options.noDuplicateValues){
throw new Error(`make(..): duplicate key "${key}": ` throw new Error(`make(..): duplicate key "${key}": `
+`can't create multiple items with the same key.`) } +`can't create multiple items with the same key.`) }
@ -1826,9 +1863,10 @@ var BaseBrowserPrototype = {
new_index[key].id_changed = true new_index[key].id_changed = true
// create a new key... // create a new key...
k = this.__id__(key) k = this.__id__(key, ++c)
} }
key = opts.id = k key = opts.id = k
//*/
// build the item... // build the item...
var item = Object.assign( var item = Object.assign(