deno.land / x / replicache@v10.0.0-beta.0 / btree / splice.ts

نووسراو ببینە
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
import {deepEqual} from '../json.js';import type {ReadonlyEntry} from './node';
export type Splice = [at: number, removed: number, added: number, from: number];
const SPLICE_UNASSIGNED = -1;export const SPLICE_AT = 0;export const SPLICE_REMOVED = 1;export const SPLICE_ADDED = 2;export const SPLICE_FROM = 3;
const KEY = 0;const VALUE = 1;
export function* computeSplices<T>( previous: readonly ReadonlyEntry<T>[], current: readonly ReadonlyEntry<T>[],): Generator<Splice, void> { let previousIndex = 0; let currentIndex = 0; let splice: Splice | undefined;
function ensureAssigned(splice: Splice, index: number): void { if (splice[SPLICE_FROM] === SPLICE_UNASSIGNED) { splice[SPLICE_FROM] = index; } }
function newSplice(): Splice { return [previousIndex, 0, 0, SPLICE_UNASSIGNED]; }
while (previousIndex < previous.length && currentIndex < current.length) { if (previous[previousIndex][KEY] === current[currentIndex][KEY]) { if ( deepEqual(previous[previousIndex][VALUE], current[currentIndex][VALUE]) ) { if (splice) { ensureAssigned(splice, 0); yield splice; splice = undefined; } } else { if (!splice) { splice = newSplice(); } splice[SPLICE_ADDED]++; splice[SPLICE_REMOVED]++; ensureAssigned(splice, currentIndex); } previousIndex++; currentIndex++; } else if (previous[previousIndex][KEY] < current[currentIndex][KEY]) { // previous was removed if (!splice) { splice = newSplice(); } splice[SPLICE_REMOVED]++;
previousIndex++; } else { // current was added if (!splice) { splice = newSplice(); } splice[SPLICE_ADDED]++; ensureAssigned(splice, currentIndex);
currentIndex++; } }
if (currentIndex < current.length) { if (!splice) { splice = newSplice(); } splice[SPLICE_ADDED] += current.length - currentIndex; ensureAssigned(splice, currentIndex); }
if (previousIndex < previous.length) { if (!splice) { splice = newSplice(); } splice[SPLICE_REMOVED] += previous.length - previousIndex; }
if (splice) { ensureAssigned(splice, 0); yield splice; }}
replicache

Version Info

Tagged at
2 years ago