-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmap.js
347 lines (300 loc) · 8.33 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* Adapted from Stage0 - The MIT License - Pavel Martynov */
/* Adapted from DOM Expressions - The MIT License - Ryan Carniato */
import { api } from './index.js';
export const GROUPING = '__g';
export const FORWARD = 'nextSibling';
export const BACKWARD = 'previousSibling';
/**
* Map over a list of items that create DOM nodes.
*
* @param {Function} items - Function or observable that creates a list.
* @param {Function} expr
* @param {boolean} [cleaning]
* @return {DocumentFragment}
*/
export function map(items, expr, cleaning) {
const { subscribe, root, sample, cleanup } = api;
// Disable cleaning for templates by default.
if (cleaning == null) cleaning = !expr.$t;
let parent = document.createDocumentFragment();
const beforeNode = add(parent, '');
const afterNode = add(parent, '');
const disposers = new Map();
const unsubscribe = subscribe((a) => {
const b = items();
return sample(() =>
reconcile(
a || [],
b || [],
beforeNode,
afterNode,
createFn,
cleaning && disposeAll,
cleaning && dispose
)
);
});
cleanup(unsubscribe);
cleanup(disposeAll);
function disposeAll() {
disposers.forEach((d) => d());
disposers.clear();
}
function dispose(node) {
let disposer = disposers.get(node);
disposer && disposer();
disposers.delete(node);
}
function createFn(parent, item, i, data, afterNode) {
// The root call makes it possible the child's computations outlive
// their parents' update cycle.
return cleaning
? root((disposeFn) => {
const node = add(parent, expr(item, i, data), afterNode);
disposers.set(node, disposeFn);
return node;
})
: add(parent, expr(item, i, data), afterNode);
}
return parent;
}
// This is almost straightforward implementation of reconcillation algorithm
// based on ivi documentation:
// https://github.com/localvoid/ivi/blob/2c81ead934b9128e092cc2a5ef2d3cabc73cb5dd/packages/ivi/src/vdom/implementation.ts#L1366
// With some fast paths from Surplus implementation:
// https://github.com/adamhaile/surplus/blob/master/src/runtime/content.ts#L86
// And working with data directly from Stage0:
// https://github.com/Freak613/stage0/blob/master/reconcile.js
// This implementation is tailored for fine grained change detection and adds support for fragments
export function reconcile(
a,
b,
beforeNode,
afterNode,
createFn,
onClear,
onRemove
) {
// When parent was a DocumentFragment, then items got appended to the DOM.
const parent = afterNode.parentNode;
let length = b.length;
let i;
// Fast path for clear
if (length === 0) {
let startMark = beforeNode.previousSibling;
if ((startMark && startMark.previousSibling) || afterNode.nextSibling) {
removeNodes(parent, beforeNode.nextSibling, afterNode);
} else {
parent.textContent = '';
if (startMark) {
parent.appendChild(startMark);
}
parent.appendChild(beforeNode);
parent.appendChild(afterNode);
}
onClear && onClear();
return [];
}
// Fast path for create
if (a.length === 0) {
for (i = 0; i < length; i++) {
createFn(parent, b[i], i, b, afterNode);
}
return b.slice();
}
let aStart = 0;
let bStart = 0;
let aEnd = a.length - 1;
let bEnd = length - 1;
let tmp;
let aStartNode = beforeNode.nextSibling;
let aEndNode = afterNode.previousSibling;
let bAfterNode = afterNode;
let mark;
fixes: while (true) {
// Skip prefix
while (a[aStart] === b[bStart]) {
bStart++;
aStartNode = step(aStartNode, FORWARD);
if (aEnd < ++aStart || bEnd < bStart) break fixes;
}
// Skip suffix
while (a[aEnd] === b[bEnd]) {
bEnd--;
bAfterNode = step(aEndNode, BACKWARD, true);
aEndNode = bAfterNode.previousSibling;
if (--aEnd < aStart || bEnd < bStart) break fixes;
}
break;
}
// Fast path for shrink
if (bEnd < bStart) {
while (aStart <= aEnd--) {
tmp = step(aEndNode, BACKWARD, true);
mark = tmp.previousSibling;
removeNodes(parent, tmp, aEndNode.nextSibling);
onRemove && onRemove(tmp);
aEndNode = mark;
}
return b.slice();
}
// Fast path for add
if (aEnd < aStart) {
while (bStart <= bEnd) {
createFn(parent, b[bStart++], bStart, b, bAfterNode);
}
return b.slice();
}
// Positions for reusing nodes from current DOM state
const P = new Array(bEnd + 1 - bStart);
// Index to resolve position from current to new
const I = new Map();
for (i = bStart; i <= bEnd; i++) {
P[i] = -1;
I.set(b[i], i);
}
// Re-using `length` variable for reusing nodes count.
length = 0;
let toRemove = [];
for (i = aStart; i <= aEnd; i++) {
tmp = I.get(a[i]);
if (tmp) {
P[tmp] = i;
length++;
} else {
toRemove.push(i);
}
}
// Fast path for full replace
if (length === 0) {
return reconcile(
reconcile(a, [], beforeNode, afterNode, createFn, onClear),
b,
beforeNode,
afterNode,
createFn
);
}
// Collect nodes to work with them
const nodes = [];
tmp = aStartNode;
for (i = aStart; i <= aEnd; i++) {
nodes[i] = tmp;
tmp = step(tmp, FORWARD);
}
for (i = 0; i < toRemove.length; i++) {
let index = toRemove[i];
tmp = nodes[index];
removeNodes(parent, tmp, step(tmp, FORWARD));
onRemove && onRemove(tmp);
}
const longestSeq = longestPositiveIncreasingSubsequence(P, bStart);
// Re-use `length` for longest sequence length.
length = longestSeq.length - 1;
for (i = bEnd; i >= bStart; i--) {
if (longestSeq[length] === i) {
bAfterNode = nodes[P[longestSeq[length]]];
length--;
} else {
if (P[i] === -1) {
tmp = createFn(parent, b[i], i, b, bAfterNode);
} else {
tmp = nodes[P[i]];
insertNodes(parent, tmp, step(tmp, FORWARD), bAfterNode);
}
bAfterNode = tmp;
}
}
return b.slice();
}
let groupCounter = 0;
function add(parent, value, endMark) {
let mark;
if (typeof value === 'string') {
value = document.createTextNode(value);
} else if (!(value instanceof Node)) {
// Passing an empty array creates a DocumentFragment.
value = api.h([], value);
}
if (
value.nodeType === 11 &&
(mark = value.firstChild) &&
mark !== value.lastChild
) {
mark[GROUPING] = value.lastChild[GROUPING] = ++groupCounter;
}
// If endMark is `null`, value will be added to the end of the list.
parent.insertBefore(value, endMark);
// Explicit undefined to store if frag.firstChild is null.
return mark || value;
}
function step(node, direction, inner) {
const key = node[GROUPING];
if (key) {
node = node[direction];
while (node && node[GROUPING] !== key) {
node = node[direction];
}
}
return inner ? node : node[direction];
}
function removeNodes(parent, node, end) {
let tmp;
while (node !== end) {
tmp = node.nextSibling;
parent.removeChild(node);
node = tmp;
}
}
function insertNodes(parent, node, end, target) {
let tmp;
while (node !== end) {
tmp = node.nextSibling;
parent.insertBefore(node, target);
node = tmp;
}
}
// Picked from
// https://github.com/adamhaile/surplus/blob/master/src/runtime/content.ts#L368
// return an array of the indices of ns that comprise the longest increasing subsequence within ns
function longestPositiveIncreasingSubsequence(ns, newStart) {
let seq = [];
let is = [];
let l = -1;
let pre = new Array(ns.length);
for (var i = newStart, len = ns.length; i < len; i++) {
var n = ns[i];
if (n < 0) continue;
var j = findGreatestIndexLEQ(seq, n);
if (j !== -1) pre[i] = is[j];
if (j === l) {
l++;
seq[l] = n;
is[l] = i;
} else if (n < seq[j + 1]) {
seq[j + 1] = n;
is[j + 1] = i;
}
}
for (i = is[l]; l >= 0; i = pre[i], l--) {
seq[l] = i;
}
return seq;
}
function findGreatestIndexLEQ(seq, n) {
// invariant: lo is guaranteed to be index of a value <= n, hi to be >
// therefore, they actually start out of range: (-1, last + 1)
let lo = -1;
let hi = seq.length;
// fast path for simple increasing sequences
if (hi > 0 && seq[hi - 1] <= n) return hi - 1;
while (hi - lo > 1) {
var mid = ((lo + hi) / 2) | 0;
if (seq[mid] > n) {
hi = mid;
} else {
lo = mid;
}
}
return lo;
}