Skip to content
This repository has been archived by the owner on Nov 12, 2019. It is now read-only.

Commit

Permalink
handle order collisions (automatic nudge)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominictarr committed May 13, 2012
1 parent f546d74 commit 0e21b87
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions seq.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,21 +21,42 @@ function sort (array) {


inherits(Seq, Set) inherits(Seq, Set)


function find (obj, iter) {

for(var k in obj) {
var v = obj[k]
if(iter(v, k, obj)) return v
}
return null
}

function Seq (doc, key, val) { function Seq (doc, key, val) {


Set.call(this, doc, key, val) Set.call(this, doc, key, val)
var seq = this var seq = this
this.on('changes', function (row, changes) { this.on('changes', function (row, changes) {
if(!changes._sort) return if(!changes._sort) return
sort(seq._array) sort(seq._array)
seq.emit('move', row) //check if there is already an item with this sort key.
var prev =
find(seq._array, function (other) {
return other != row && other.get('_sort') == row.get('_sort')
})

if(prev) {
var next = seq.next(row)
console.log('P:', toKey(prev), toKey(next))
seq.insert(row, prev, seq.next(row))
}
else
seq.emit('move', row)
}) })
this.insert = function (obj, before, after) { this.insert = function (obj, before, after) {
before = toKey(before) before = toKey(before)
after = toKey(after) after = toKey(after)


if(before == after) // if(before == after)
throw new Error('equal before/after') // throw new Error('equal before/after')
/* /*
there could be a arbitary number of equal items. there could be a arbitary number of equal items.
find the last one, and nudge it across. find the last one, and nudge it across.
Expand Down Expand Up @@ -97,11 +118,11 @@ function toKey (key) {
*/ */


function max (ary, test, wantIndex) { function max (ary, test, wantIndex) {
var max = ary[0], _max = 0 var max = null, _max = -1
if(ary.length < 1) if(!ary.length) return
return
for (var i = 1; i < ary.length; i++) for (var i = 0; i < ary.length; i++)
if(test(max, ary[i])) { max = ary[i]; _max = i } if(test(max, ary[i])) max = ary[_max = i]
return wantIndex ? _max : max return wantIndex ? _max : max
} }


Expand All @@ -111,9 +132,9 @@ Seq.prototype.prev = function (key) {
//since the list is kept in order, //since the list is kept in order,
//a binary search is used. //a binary search is used.
//think about that later //think about that later
return max(this.array, function (M, m) { return max(this._array, function (M, m) {
if(toKey(m) < key) if(toKey(m) < key)
return toKey(m) > toKey(M) return M ? toKey(m) > toKey(M) : true
}) })
} }


Expand All @@ -123,9 +144,9 @@ Seq.prototype.next = function (key) {
//since the list is kept in order, //since the list is kept in order,
//a binary search is used. //a binary search is used.
//think about that later //think about that later
return max(this.array, function (M, m) { return max(this._array, function (M, m) {
if(toKey(m) > key) if(toKey(m) > key)
return toKey(m) < toKey(M) return M ? toKey(m) < toKey(M) : true
}) })
} }


Expand Down

0 comments on commit 0e21b87

Please sign in to comment.