-
Notifications
You must be signed in to change notification settings - Fork 11
/
map.js
327 lines (275 loc) · 7.4 KB
/
map.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
var resolve = require('./resolve')
var LazyWatcher = require('./lib/lazy-watcher')
var isSame = require('./lib/is-same')
var addCollectionMethods = require('./lib/add-collection-methods')
var onceIdle = require('./once-idle')
module.exports = Map
function Map (obs, lambda, opts) {
// opts: comparer, maxTime, onRemove
if (typeof lambda !== 'function') throw new Error('mutant/map lambda must be a function')
var comparer = opts && opts.comparer || null
var releases = []
var binder = LazyWatcher(update, listen, unlisten)
if (opts && opts.nextTick) binder.nextTick = true
if (opts && opts.idle) binder.idle = true
var itemInvalidators = new global.Map()
var lastValues = new global.Map()
var rawSet = new global.Set()
var items = []
var raw = []
var values = []
var watches = []
binder.value = values
// incremental update
var queue = []
var maxTime = null
if (opts && opts.maxTime) {
maxTime = opts.maxTime
}
var result = function MutantMap (listener) {
if (!listener) {
return binder.getValue()
}
return binder.addListener(listener)
}
addCollectionMethods(result, raw, binder.checkUpdated)
return result
// scoped
function listen () {
if (typeof obs === 'function') {
releases.push(obs(binder.onUpdate))
}
rebindAll()
Array.from(itemInvalidators.values()).forEach(function (invalidators) {
invalidators.forEach(function (invalidator) {
invalidator.release = invalidator.observable(invalidate.bind(null, invalidator))
})
})
if (opts && opts.onListen) {
var release = opts.onListen()
if (typeof release === 'function') {
releases.push(release)
}
}
}
function unlisten () {
while (releases.length) {
releases.pop()()
}
rebindAll()
Array.from(itemInvalidators.values()).forEach(function (invalidators) {
invalidators.forEach(invokeRelease)
})
if (opts && opts.onUnlisten) {
opts.onUnlisten()
}
}
function update () {
var changed = false
if (items.length !== getLength(obs)) {
changed = true
}
var startedAt = Date.now()
for (var i = 0, len = getLength(obs); i < len; i++) {
var item = get(obs, i)
var currentItem = items[i]
items[i] = item
if (!isSame(item, currentItem, comparer) || (!binder.live && checkInvalidated(item))) {
if (maxTime && Date.now() - startedAt > maxTime) {
queueUpdateItem(i)
} else {
updateItem(i)
}
changed = true
}
}
if (changed) {
// clean up cache
var oldLength = raw.length
var newLength = getLength(obs)
Array.from(lastValues.keys()).filter(notIncluded, obs).forEach(removeItem)
items.length = newLength
values.length = newLength
raw.length = newLength
for (var index = newLength; index < oldLength; index++) {
rebind(index)
}
Array.from(rawSet.values()).filter(notIncluded, raw).forEach(removeMapped)
}
return changed
}
function checkInvalidated (item) {
if (itemInvalidators.has(item)) {
return itemInvalidators.get(item).some(function (invalidator) {
lastValues.delete(invalidator.item)
return !isSame(invalidator.currentValue, resolve(invalidator.observable), comparer)
})
}
}
function queueUpdateItem (i) {
if (!queue.length) {
doSoon(flushQueue)
}
if (!~queue.indexOf(i)) {
queue.push(i)
}
}
function flushQueue () {
var startedAt = Date.now()
while (queue.length && (!maxTime || Date.now() - startedAt < maxTime)) {
updateItem(queue.shift())
}
binder.broadcast()
if (queue.length) {
doSoon(flushQueue)
}
}
function invalidateOn (item, obs) {
if (!itemInvalidators.has(item)) {
itemInvalidators.set(item, [])
}
var invalidators = itemInvalidators.get(item)
var invalidator = {
currentValue: resolve(obs),
observable: obs,
item: item,
release: null
}
invalidators.push(invalidator)
if (binder.live) {
invalidator.release = invalidator.observable(invalidate.bind(null, invalidator))
}
}
function addInvalidateCallback (item) {
return invalidateOn.bind(null, item)
}
function removeItem (item) {
lastValues.delete(item)
if (itemInvalidators.has(item)) {
itemInvalidators.get(item).forEach(invokeRelease)
itemInvalidators.delete(item)
}
}
function removeMapped (mappedItem) {
rawSet.delete(mappedItem)
if (opts && opts.onRemove) {
opts.onRemove(mappedItem)
}
}
function invalidate (entry) {
var changed = []
var length = getLength(obs)
lastValues.delete(entry.item)
for (var i = 0; i < length; i++) {
if (get(obs, i) === entry.item) {
changed.push(i)
}
}
if (changed.length) {
var rawValue = raw[changed[0]]
changed.forEach(function (index) {
raw[index] = null
})
if (!raw.includes(rawValue)) {
removeMapped(rawValue)
}
changed.forEach(updateItem)
binder.broadcast()
}
}
function updateItem (i) {
if (i < getLength(obs)) {
var item = get(obs, i)
// insert new items, or replace if type is not comparable (e.g. non observable object)
if (!lastValues.has(item) || !isSame(item, item, comparer)) {
if (itemInvalidators.has(item)) {
itemInvalidators.get(item).forEach(invokeRelease)
itemInvalidators.delete(item)
}
var newValue = lambda(item, addInvalidateCallback(item))
if (newValue !== raw[i]) {
raw[i] = newValue
}
rawSet.add(newValue)
lastValues.set(item, raw[i])
} else {
raw[i] = lastValues.get(item)
}
rebind(i)
values[i] = resolve(raw[i])
}
}
function rebind (index) {
if (watches[index]) {
watches[index]()
watches[index] = null
}
if (binder.live) {
if (typeof raw[index] === 'function') {
watches[index] = updateValue(raw[index], index)
}
}
}
function rebindAll () {
for (var i = 0; i < raw.length; i++) {
rebind(i)
}
}
function updateValue (obs, index) {
return obs(function (value) {
if (!isSame(values[index], value, comparer)) {
values[index] = value
binder.broadcast()
}
})
}
function doSoon (fn) {
if (opts.idle) {
onceIdle(fn)
} else if (opts.delayTime) {
setTimeout(fn, opts.delayTime)
} else {
setImmediate(fn)
}
}
}
function get (target, index) {
if (typeof target === 'function' && !target.get) {
target = target()
}
if (Array.isArray(target)) {
return target[index]
} else if (target && target.get) {
return target.get(index)
}
}
function getLength (target) {
if (typeof target === 'function' && !target.getLength) {
target = target()
}
if (Array.isArray(target)) {
return target.length
} else if (target && target.get) {
return target.getLength()
}
return 0
}
function notIncluded (value) {
if (this.includes) {
return !this.includes(value)
} else if (this.indexOf) {
return !~this.indexOf(value)
} else if (typeof this === 'function') {
var array = this()
if (array && array.includes) {
return !array.includes(value)
}
}
return true
}
function invokeRelease (item) {
if (item.release) {
item.release()
item.release = null
}
}