git ssb

0+

ev / microbay



forked from Dominic / patchbay

Commit b5a110f05db20715d91f8c4487810adc05a33d09

break ties my most recent timestamp

Dominic Tarr committed on 8/1/2016, 5:54:30 AM
Parent: 3369e03e90e8b441a5c23b79c41b65724a98a39e

Files changed

modules/names.jschanged
modules/names.jsView
@@ -30,9 +30,10 @@
3030 var reduce = {
3131 $reduce: {
3232 name: 'name',
3333 id: 'id',
34- rank: {$count: true}
34+ rank: {$count: true},
35+ ts: {$max: 'ts'}
3536 }
3637 }
3738
3839 var filter2 = {
@@ -96,8 +97,10 @@
9697 many([
9798 sbot_links2({query: [filter, map, reduce]}),
9899 add_at(sbot_query({query: [filter2, map2, reduce]}))
99100 ]),
101+ //reducing also ensures order by the lookup properties
102+ //in this case: [name, id]
100103 mfr.reduce(merge),
101104 pull.collect(function (err, ary) {
102105 if(!err) {
103106 names = ary
@@ -123,11 +126,18 @@
123126 }
124127 }
125128
126129 function rank(ary) {
127- return ary.sort(function (a, b) { return b.rank - a.rank })
130+ //sort by most used, or most recently used
131+ return ary.sort(function (a, b) { return b.rank - a.rank || b.ts - a.ts })
128132 }
129133
134+//we are just iterating over the entire array.
135+//if this becomes a problem, maintain two arrays
136+//one of each sort order, but do not duplicate the objects.
137+//that should mean the space required is just 2x object references,
138+//not 2x objects, and we can use binary search to find matches.
139+
130140 exports.signifier = async(function (id) {
131141 return rank(names.filter(function (e) { return e.id == id}))
132142 })
133143
@@ -135,8 +145,4 @@
135145 var rx = new RegExp('^'+name)
136146 return rank(names.filter(function (e) { return rx.test(e.name) }))
137147 })
138148
139-
140-
141-
142-

Built with git-ssb-web