forked from dominictarr/crdt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seq.js
193 lines (156 loc) · 4.1 KB
/
seq.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
'use strict';
var Set = require('./set')
var Row = require('./row')
var inherits = require('util').inherits
var between = require('between')
module.exports = Seq
function sort (array) {
return array.sort(function (a, b) {
return between.strord(a.get('_sort'), b.get('_sort'))
})
}
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) {
if(key == '__proto__')
throw new Error('__proto__ is invalid key')
Set.call(this, doc, key, val)
if (typeof key !== 'string') {
key = null
}
var seq = this
this.on('changes', function (row, changes) {
if(!changes._sort) return
sort(seq._array)
//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')
})
//nudge it forward if it has the same key.
if(prev)
seq.insert(row, prev, seq.next(row))
else
seq.emit('move', row)
})
this.insert = function (obj, before, after) {
before = toKey(this.get(before) || '!')
after = toKey(this.get(after) || '~')
//must get id from the doc,
//because may be moving this item into this set.
if('string' === typeof obj)
obj = doc.rows[obj]
var _sort =
between.between(before, after )
+ between.randstr(3) //add a random tail so it's hard
//to concurrently add two items with the
//same sort.
var r, changes
if(obj instanceof Row) {
r = obj
changes = {_sort: _sort}
if (key && r.get(key) != val) {
changes[key] = val
}
r.set(changes)
} else {
obj._sort = _sort
if (key) {
obj[key] = val
}
r = doc.set(id(obj), obj)
}
sort(this._array)
return r
}
}
function toKey (key) {
return (
'string' === typeof key ? key
: key instanceof Row ? key.get()._sort
: key ? key._sort
: null
)
}
/*
items are relative to each other,
more like a linked list.
although it is possible to make an
index based interface, before after,
etc is more natural
*/
function max (ary, test, wantIndex) {
var max = null, _max = -1
if(!ary.length) return
for (var i = 0; i < ary.length; i++)
if(test(max, ary[i])) max = ary[_max = i]
return wantIndex ? _max : max
}
Seq.prototype.prev = function (key) {
key = toKey(this.get(key) || '~')
//find the greatest item that is less than `key`.
//since the list is kept in order,
//a binary search is used.
//think about that later
return max(this._array, function (M, m) {
if(toKey(m) < key)
return M ? toKey(m) > toKey(M) : true
})
}
Seq.prototype.next = function (key) {
key = toKey(this.get(key) || '!')
return max(this._array, function (M, m) {
if(toKey(m) > key)
return M ? toKey(m) < toKey(M) : true
})
}
function id(obj) {
return (obj.id
|| obj._id
|| '_' + Date.now()
+ '_' + Math.round(Math.random()*1000)
)
}
Seq.prototype.asArray = function () {
return sort(this._array)
}
Seq.prototype.before = function (obj, before) {
return this.insert(obj, this.prev(before), before)
}
Seq.prototype.after = function (obj, after) {
return this.insert(obj, after, this.next(after))
}
Seq.prototype.first = function () {
return this._array[0]
}
Seq.prototype.last = function () {
return this._array[this._array.length - 1]
}
Seq.prototype.indexOf = function (obj) {
return this._array.indexOf('string' == typeof obj ? this.rows[obj] : obj)
}
Seq.prototype.at = function (i) {
if('__proto__' === i) throw new Error('__proto__ invalid index')
return this._array[i]
}
Seq.prototype.unshift = function (obj) {
return this.insert(obj, '!', this.first())
}
Seq.prototype.push = function (obj) {
return this.insert(obj, this.last(), '~')
}
Seq.prototype.length = function () {
return this._array.length
}
Seq.prototype.pop = function () {
return this.remove(this.last())
}
Seq.prototype.shift = function () {
return this.remove(this.first())
}