-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
333 lines (272 loc) · 7.19 KB
/
index.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
//inject a matchRef, isRef, and a getRef function?
//could use the same pattern with objects.
//I don't really want to force __id__
//should be able to use anything, aslong as you
function shallowEqual (a, b) {
if(isObject(a)
&& isObject(b)
&& (a.__id__ == b.__id__ || a === b))
return true
if(a && !b) return false
return a == b
}
function equal (a, b) {
if((a && !b) || (!a && b)) return false
if(Array.isArray(a))
if(a.length != b.length) return false
if(isObject(a) && isObject(b)) {
if (a.__id__ == b.__id__ || a === b)
return true
for(var i in a)
if(!equal(a[i], b[i])) return false
return true
}
if(a == null && b == null) return true
return a === b
}
var adiff = require('adiff')({ equal: equal })
function getPath (obj, path) {
if(!Array.isArray(path))
return obj[path]
for(var i in path) {
obj = obj[path[i]]
}
return obj
}
function findRefs(obj, refs) {
refs = refs || {}
//add leaves before branches.
//this will FAIL if there are circular references.
if(!obj)
return refs
for(var k in obj) {
if(obj[k] && 'object' == typeof obj[k])
findRefs(obj[k], refs)
}
if(obj.__id__ && !refs[obj.__id__])
refs[obj.__id__] = obj
return refs
}
function toRef(v) {
//TODO escape strings that happen to start with #*=
var r
if(r = isRef(v)) return '#*='+r
return v
}
function isObject (o) {
return o && 'object' == typeof o
}
function isRef(x) {
return x ? x.__id__ : undefined
}
function sameRef(a, b) {
return a && b && isRef(a) == isRef(b)
}
//traverse o, and replace every object with __id__ with a pointer.
//make diffing references easy.
exports.deref = function (o, mutate) {
var refs = findRefs(o)
var derefed = {}
function deref (o, K) {
if(isRef(o) && K != isRef(o))
return toRef(o)
var p = mutate ? o : Array.isArray(o) ? [] : {} //will copy the tree!
for (var k in o) {
var r
if(isRef(o[k])) p[k] = toRef(o[k])
else if(isObject(o[k])) p[k] = deref(o[k])
else p[k] = o[k]
}
return p
}
refs.root = o
for (var k in refs)
refs[k] = deref(refs[k], k)
return refs
}
exports.reref = function (refs, mutate) {
function fromRef(v) {
//TODO escape strings that happen to start with #*=
if('string' == typeof v && /^#\*=/.test(v)) return refs[v.substring(3)]
return v
}
function reref (o) { //will MUTATE the tree
if(!isObject(o))
return fromRef(o)
var p = mutate ? o : Array.isArray(o) ? [] : {} //will copy the tree!
for (var k in o) {
if(isObject(o[k]))
p[k] = reref(o[k])
else
p[k] = fromRef(o[k])
}
return p
}
//if the root is a ref. need a special case
for (var k in refs) {
refs[k] = reref(refs[k])
}
return refs.root
}
exports.diff = function (a, b) {
var aRefs = exports.deref(a)
var bRefs = exports.deref(b)
var seen = []
for (var k in aRefs)
seen.push(k)
function isSeen(o) {
if(isRef(o)) return ~seen.indexOf(o.__id__)
return true
}
function addSeen(o) {
if(!isRef(o)) return o
if(!isSeen(o)) seen.push(o.__id__)
return o
}
// how to handle references?
// this is necessary to handle objects in arrays nicely
// otherwise mergeing an edit and a move is ambigous. // will need to perform a topoligical sort of the refs and diff them first, in that order.
// first, apply changes to all refs,
// then traverse over the root object,
function _diff (a, b, path) {
path = path || []
if(Array.isArray(a) && Array.isArray(b)) {
var d = adiff.diff(a, b)
if(d.length) delta.push(['splice', path, d])
return delta
}
// references to objects with ids are
// changed into strings of thier id.
// the string is prepended with '#*='
// to distinguish it from other strings
// if you use that string in your model,
// it will break.
// TODO escape strings so this is safe
//ah, treat root like it's a __id__
var isRoot = path.length === 1 && path[0] === 'root'
for (var k in b) {
// if both are nonRef objects, or are the same object, branch into them.
if(isObject(a[k]) && isObject(b[k]) && sameRef(b[k], a[k]))
_diff(a[k], b[k], path.concat(k))
else if(b[k] !== a[k])
delta.push(['set', path.concat(k), cpy(b[k])])
}
for (var k in a)
if('undefined' == typeof b[k])
delta.push(['del', path.concat(k)])
}
var delta = []
_diff(aRefs, bRefs, [])
if(delta.length)
return cpy(delta)
}
exports.patch = function (a, patch) {
if(!patch) throw new Error('expected patch')
var refs = exports.deref(a, true)
refs.root = a
var methods = {
set: function (key, value) {
this[key] = cpy(value) // incase this was a reference, remove it.
},
del: function (key) {
delete this[key]
},
splice: function (changes) {
adiff.patch(this, changes, true)
}
}
function pathTo(a, p) {
for (var i in p) a = a[p[i]]
return a
}
patch.forEach(function (args) {
args = args.slice()
var method = args.shift()
var path = args.shift().slice()
var key
if(method != 'splice') {
key = path.pop()
args.unshift(key)
}
var obj = pathTo(refs, path)
methods[method].apply(obj, args)
})
return exports.reref(refs, true)
}
function cpy(o) {
if(!o) return o
return JSON.parse(JSON.stringify(o))
}
exports.diff3 = function (a, o, b) {
if(arguments.length == 1)
o = a[1], b = a[2], a = a[0]
var _a = exports.diff(o, a) || [] // if there where no changes, still merge
, _b = exports.diff(o, b) || []
function cmp (a, b) {
//check if a[1] > b[1]
if(!b)
return 1
var p = a[1], q = b[1]
var i = 0
while (p[i] === q[i] && p[i] != null)
i++
if(p[i] === q[i]) return 0
return p[i] < q[i] ? -1 : 1
}
function isPrefix(a, b) {
if(!b) return 1
var p = a[1], q = b[1]
var i = 0
while (p[i] === q[i] && i < p.length && i < q.length)
i++
if(i == p.length || i == q.length) return 0
return p[i] < q[i] ? -1 : 1
}
//merge two lists, which must be sorted.
function cmpSp (a, b) {
if(a[0] == b[0])
return 0
function max(k) {
return k[0] + (k[1] >= 1 ? k[1] - 1 : 0)
}
if(max(a) < b[0] || max(b) < a[0])
return a[0] - b[0]
return 0
}
function resolveAry(a, b) {
return a
}
function resolve(a, b) {
if(a[1].length == b[1].length) {
if(a[0] == b[0]) {
if(a[0] == 'splice') {
var R = merge(a[2], b[2], cmpSp, resolveAry)
return ['splice', a[1].slice(), R]
} else if(equal(a[2], b[2])) //same change both sides.
return a
}
}
return a
}
function merge(a, b, cmp, resolve) {
var i = a.length - 1, j = b.length - 1, r = []
while(~i && ~j) {
var c = cmp(a[i], b[j])
if(c > 0) r.push(a[i--])
if(c < 0) r.push(b[j--])
if(!c) {
var R = resolve(a[i], b[j])
j--, i--
r.push(R)
}
}
//finish off the list if there are any left over
while(~i) r.push(a[i--])
while(~j) r.push(b[j--])
return r
}
_a.sort(cmp)
_b.sort(cmp)
var m = merge(_a, _b, isPrefix, resolve)
return m.length ? m : null
}