@signaldb/svelte 2.0.0-beta.13 → 2.0.0-beta.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/base/core/src/AsyncDataAdapter.d.ts +11 -2
- package/dist/base/core/src/AsyncDataAdapter.js +91 -28
- package/dist/base/core/src/Collection/Cursor.d.ts +11 -1
- package/dist/base/core/src/Collection/Cursor.js +18 -1
- package/dist/base/core/src/Collection/Observer.d.ts +31 -0
- package/dist/base/core/src/Collection/Observer.js +92 -46
- package/dist/base/core/src/Collection/index.js +66 -49
- package/dist/base/core/src/DataAdapter.d.ts +15 -2
- package/dist/base/core/src/DefaultDataAdapter.js +24 -16
- package/dist/base/core/src/WorkerDataAdapter.d.ts +25 -2
- package/dist/base/core/src/WorkerDataAdapter.js +360 -93
- package/dist/base/core/src/WorkerDataAdapterHost.d.ts +2 -0
- package/dist/base/core/src/WorkerDataAdapterHost.js +96 -26
- package/dist/base/core/src/index.d.ts +2 -0
- package/dist/base/core/src/utils/incrementalQueryUpdate.d.ts +60 -0
- package/dist/base/core/src/utils/incrementalQueryUpdate.js +159 -0
- package/dist/base/core/src/utils/projectItems.d.ts +12 -0
- package/dist/base/core/src/utils/projectItems.js +25 -0
- package/dist/base/core/src/utils/queryDelta.d.ts +83 -0
- package/dist/base/core/src/utils/queryDelta.js +231 -0
- package/dist/base/core/src/utils/queryId.js +32 -1
- package/package.json +1 -1
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.isEmptyQueryDelta = isEmptyQueryDelta;
|
|
7
|
+
exports.callWithDelta = callWithDelta;
|
|
8
|
+
exports.canApplyQueryDelta = canApplyQueryDelta;
|
|
9
|
+
exports.diffQueryResults = diffQueryResults;
|
|
10
|
+
exports.applyQueryDelta = applyQueryDelta;
|
|
11
|
+
const isEqual_1 = __importDefault(require("./isEqual"));
|
|
12
|
+
/**
|
|
13
|
+
* Checks whether a delta leaves the result it is applied to unchanged.
|
|
14
|
+
* @param delta - The delta to inspect.
|
|
15
|
+
* @returns `true` when applying the delta would be a no-op.
|
|
16
|
+
*/
|
|
17
|
+
function isEmptyQueryDelta(delta) {
|
|
18
|
+
return delta.added.length === 0
|
|
19
|
+
&& delta.changed.length === 0
|
|
20
|
+
&& delta.removed.length === 0
|
|
21
|
+
&& delta.moved.length === 0;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Calls a state-change callback, passing the delta only when there is one.
|
|
25
|
+
*
|
|
26
|
+
* A callback invoked as `callback(state, undefined)` has been handed two arguments, which is a
|
|
27
|
+
* different thing from being handed one — visible to anything that inspects arity, and to any test
|
|
28
|
+
* that asserts on the call.
|
|
29
|
+
* @template T - The type of the items.
|
|
30
|
+
* @param callback - The callback to invoke.
|
|
31
|
+
* @param state - The state to report.
|
|
32
|
+
* @param delta - The delta to report, if there is one.
|
|
33
|
+
*/
|
|
34
|
+
function callWithDelta(callback, state, delta) {
|
|
35
|
+
if (delta == null) {
|
|
36
|
+
callback(state);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
callback(state, delta);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Checks whether a delta describes a change to the given result.
|
|
43
|
+
*
|
|
44
|
+
* A delta is only meaningful against the exact result it was computed from — it names positions in
|
|
45
|
+
* an array and items by id alone. Applying one to anything else produces a result that looks
|
|
46
|
+
* plausible and is wrong, and from then on every further delta compounds the error. This is the
|
|
47
|
+
* cheap structural check that catches that: every id the delta expects to find is there, every id
|
|
48
|
+
* it expects to be new is not, and the arithmetic on the length works out. It costs the size of the
|
|
49
|
+
* delta, not the size of the result.
|
|
50
|
+
* @template T - The type of the items.
|
|
51
|
+
* @param previous - The result the delta would be applied to.
|
|
52
|
+
* @param delta - The delta to check.
|
|
53
|
+
* @returns `true` when the delta can be applied.
|
|
54
|
+
*/
|
|
55
|
+
function canApplyQueryDelta(previous, delta) {
|
|
56
|
+
const present = new Set(previous.map(item => item.id));
|
|
57
|
+
const seen = new Set();
|
|
58
|
+
const claim = (id, shouldExist) => {
|
|
59
|
+
if (seen.has(id))
|
|
60
|
+
return false;
|
|
61
|
+
seen.add(id);
|
|
62
|
+
return present.has(id) === shouldExist;
|
|
63
|
+
};
|
|
64
|
+
const expectedCount = previous.length - delta.removed.length + delta.added.length;
|
|
65
|
+
if (expectedCount !== delta.resultCount)
|
|
66
|
+
return false;
|
|
67
|
+
if (!delta.removed.every(id => claim(id, true)))
|
|
68
|
+
return false;
|
|
69
|
+
if (!delta.added.every(({ index, item }) => claim(item.id, false)
|
|
70
|
+
&& index >= 0 && index < delta.resultCount))
|
|
71
|
+
return false;
|
|
72
|
+
if (!delta.changed.every(item => present.has(item.id)))
|
|
73
|
+
return false;
|
|
74
|
+
return delta.moved.every(({ index, id }) => present.has(id)
|
|
75
|
+
&& !delta.removed.includes(id)
|
|
76
|
+
&& index >= 0 && index < delta.resultCount);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Indices of the longest strictly increasing subsequence of the given numbers.
|
|
80
|
+
* Used to decide which items keep their place when a result is reordered: everything outside the
|
|
81
|
+
* subsequence has to move, everything inside it is already in the right relative order.
|
|
82
|
+
* @param sequence - The numbers to inspect.
|
|
83
|
+
* @returns The indices into `sequence` that form the longest increasing subsequence.
|
|
84
|
+
*/
|
|
85
|
+
function longestIncreasingSubsequence(sequence) {
|
|
86
|
+
if (sequence.length === 0)
|
|
87
|
+
return [];
|
|
88
|
+
// `tails[length - 1]` is the index of the smallest possible tail of an increasing subsequence of
|
|
89
|
+
// that length; `previous` links each index back to its predecessor so the run can be walked out.
|
|
90
|
+
const tails = [];
|
|
91
|
+
const previous = Array.from({ length: sequence.length }).fill(-1);
|
|
92
|
+
for (let index = 0; index < sequence.length; index += 1) {
|
|
93
|
+
const value = sequence[index];
|
|
94
|
+
let low = 0;
|
|
95
|
+
let high = tails.length;
|
|
96
|
+
while (low < high) {
|
|
97
|
+
const middle = (low + high) >> 1;
|
|
98
|
+
if (sequence[tails[middle]] < value) {
|
|
99
|
+
low = middle + 1;
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
high = middle;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (low > 0)
|
|
106
|
+
previous[index] = tails[low - 1];
|
|
107
|
+
tails[low] = index;
|
|
108
|
+
}
|
|
109
|
+
const result = [];
|
|
110
|
+
let cursor = tails.at(-1);
|
|
111
|
+
while (cursor !== -1) {
|
|
112
|
+
result.push(cursor);
|
|
113
|
+
cursor = previous[cursor];
|
|
114
|
+
}
|
|
115
|
+
return result.toReversed();
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Computes the delta between two results of the same query.
|
|
119
|
+
*
|
|
120
|
+
* A fallback for the cases where the change that produced the new result is not available — a query
|
|
121
|
+
* that had to be re-executed in full, for instance. It costs a pass over both results, but it is
|
|
122
|
+
* paid once, on the side that has both of them, instead of shipping the entire new result to
|
|
123
|
+
* everyone who only needs to know what changed.
|
|
124
|
+
* @template T - The type of the items.
|
|
125
|
+
* @param previous - The result the delta should be relative to.
|
|
126
|
+
* @param next - The result the delta should produce.
|
|
127
|
+
* @returns The delta between the two results.
|
|
128
|
+
*/
|
|
129
|
+
function diffQueryResults(previous, next) {
|
|
130
|
+
// Both arrays are walked in full here, so the cheap ways out are worth taking. Two results
|
|
131
|
+
// holding the same items in the same places are the common case by some margin: this is asked
|
|
132
|
+
// several times per write, and most of those ask about a query the write did not really change.
|
|
133
|
+
if (holdsTheSameItems(previous, next)) {
|
|
134
|
+
return { added: [], changed: [], removed: [], moved: [], resultCount: next.length };
|
|
135
|
+
}
|
|
136
|
+
const previousIndexById = new Map();
|
|
137
|
+
previous.forEach((item, index) => previousIndexById.set(item.id, index));
|
|
138
|
+
const added = [];
|
|
139
|
+
const changed = [];
|
|
140
|
+
// Positions in `previous` of the items that survive, in the order they appear in `next`. An
|
|
141
|
+
// increasing run in here is a stretch of items whose relative order did not change — and when
|
|
142
|
+
// the whole thing is increasing, nothing moved and the work below can be skipped entirely.
|
|
143
|
+
const survivingPreviousIndices = [];
|
|
144
|
+
const survivingNextIndices = [];
|
|
145
|
+
const survived = Array.from({ length: previous.length }).fill(false);
|
|
146
|
+
let orderPreserved = true;
|
|
147
|
+
let lastPreviousIndex = -1;
|
|
148
|
+
next.forEach((item, index) => {
|
|
149
|
+
const previousIndex = previousIndexById.get(item.id);
|
|
150
|
+
if (previousIndex == null) {
|
|
151
|
+
added.push({ index, item });
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
survived[previousIndex] = true;
|
|
155
|
+
if (!(0, isEqual_1.default)(previous[previousIndex], item))
|
|
156
|
+
changed.push(item);
|
|
157
|
+
if (previousIndex < lastPreviousIndex)
|
|
158
|
+
orderPreserved = false;
|
|
159
|
+
lastPreviousIndex = previousIndex;
|
|
160
|
+
survivingPreviousIndices.push(previousIndex);
|
|
161
|
+
survivingNextIndices.push(index);
|
|
162
|
+
});
|
|
163
|
+
const removed = [];
|
|
164
|
+
previous.forEach((item, index) => {
|
|
165
|
+
if (!survived[index])
|
|
166
|
+
removed.push(item.id);
|
|
167
|
+
});
|
|
168
|
+
const moved = [];
|
|
169
|
+
if (!orderPreserved) {
|
|
170
|
+
const stationary = new Set(longestIncreasingSubsequence(survivingPreviousIndices)
|
|
171
|
+
.map(position => survivingNextIndices[position]));
|
|
172
|
+
survivingNextIndices.forEach((nextIndex) => {
|
|
173
|
+
if (stationary.has(nextIndex))
|
|
174
|
+
return;
|
|
175
|
+
moved.push({ index: nextIndex, id: next[nextIndex].id });
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
return { added, changed, removed, moved, resultCount: next.length };
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Whether two results hold the same items in the same order, by identity.
|
|
182
|
+
*
|
|
183
|
+
* Items are replaced rather than mutated wherever they come from, so identity is a sound answer to
|
|
184
|
+
* "unchanged" — and a wrong one is impossible, only a missed shortcut.
|
|
185
|
+
* @template T - The type of the items.
|
|
186
|
+
* @param previous - One result.
|
|
187
|
+
* @param next - The other.
|
|
188
|
+
* @returns `true` when the two are element-for-element the same objects.
|
|
189
|
+
*/
|
|
190
|
+
function holdsTheSameItems(previous, next) {
|
|
191
|
+
if (previous === next)
|
|
192
|
+
return true;
|
|
193
|
+
if (previous.length !== next.length)
|
|
194
|
+
return false;
|
|
195
|
+
return previous.every((item, index) => item === next[index]);
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Applies a delta to the result it was computed against.
|
|
199
|
+
* @template T - The type of the items.
|
|
200
|
+
* @param previous - The result the delta is relative to. Not modified.
|
|
201
|
+
* @param delta - The delta to apply.
|
|
202
|
+
* @returns The resulting items.
|
|
203
|
+
*/
|
|
204
|
+
function applyQueryDelta(previous, delta) {
|
|
205
|
+
const removed = new Set(delta.removed);
|
|
206
|
+
const changedById = new Map(delta.changed.map(item => [item.id, item]));
|
|
207
|
+
const movedIds = new Set(delta.moved.map(({ id }) => id));
|
|
208
|
+
const stationary = [];
|
|
209
|
+
const byId = new Map();
|
|
210
|
+
previous.forEach((item) => {
|
|
211
|
+
if (removed.has(item.id))
|
|
212
|
+
return;
|
|
213
|
+
const current = changedById.get(item.id) ?? item;
|
|
214
|
+
byId.set(current.id, current);
|
|
215
|
+
if (!movedIds.has(current.id))
|
|
216
|
+
stationary.push(current);
|
|
217
|
+
});
|
|
218
|
+
// Insertions carry positions in the resulting array, so splicing them in ascending order lands
|
|
219
|
+
// every one of them at its final index — each is placed only after everything before it is there.
|
|
220
|
+
const insertions = [
|
|
221
|
+
...delta.added.map(({ index, item }) => ({ index, item })),
|
|
222
|
+
...delta.moved.map(({ index, id }) => ({ index, item: byId.get(id) })),
|
|
223
|
+
].sort((a, b) => a.index - b.index); // eslint-disable-line unicorn/no-array-sort -- unavailable on Hermes
|
|
224
|
+
const result = stationary;
|
|
225
|
+
insertions.forEach(({ index, item }) => {
|
|
226
|
+
if (item == null)
|
|
227
|
+
return;
|
|
228
|
+
result.splice(index, 0, item);
|
|
229
|
+
});
|
|
230
|
+
return result;
|
|
231
|
+
}
|
|
@@ -17,6 +17,19 @@ function isEmptyOptions(options) {
|
|
|
17
17
|
return false;
|
|
18
18
|
return Object.keys(options).length === 0;
|
|
19
19
|
}
|
|
20
|
+
// Stands in for absent options so they can be a `WeakMap` key like any other pair half.
|
|
21
|
+
const noOptions = {};
|
|
22
|
+
// Ids are asked for far more often than queries are created — every cursor read resolves its query
|
|
23
|
+
// through one, and a single `postMessage` round trip goes through several. Serializing the same two
|
|
24
|
+
// objects over and over is pure waste, so a pair of objects that has been seen before answers from
|
|
25
|
+
// here. Keyed weakly on both halves: an entry lives exactly as long as the objects it describes,
|
|
26
|
+
// and a selector built fresh at the call site simply misses and is collected again.
|
|
27
|
+
//
|
|
28
|
+
// The cache assumes a selector or options object is not mutated after it has been used to identify
|
|
29
|
+
// a query. That already holds today for a different reason — a query is registered, cached and
|
|
30
|
+
// looked up under the id its selector had at registration time, so mutating it afterwards loses the
|
|
31
|
+
// query either way.
|
|
32
|
+
const cache = new WeakMap();
|
|
20
33
|
/**
|
|
21
34
|
* Generates a unique identifier for a query based on its selector and options.
|
|
22
35
|
* @param selector - The selector object.
|
|
@@ -24,7 +37,25 @@ function isEmptyOptions(options) {
|
|
|
24
37
|
* @returns A unique identifier string for the query.
|
|
25
38
|
*/
|
|
26
39
|
function queryId(selector, options) {
|
|
40
|
+
const isCacheable = selector != null && typeof selector === 'object'
|
|
41
|
+
&& (options == null || typeof options === 'object');
|
|
42
|
+
if (!isCacheable) {
|
|
43
|
+
const optionsId = isEmptyOptions(options) ? -1 : JSON.stringify(options);
|
|
44
|
+
return `${JSON.stringify(selector)}:${optionsId}`;
|
|
45
|
+
}
|
|
46
|
+
const optionsKey = (options ?? noOptions);
|
|
47
|
+
const cachedForSelector = cache.get(selector);
|
|
48
|
+
const cached = cachedForSelector?.get(optionsKey);
|
|
49
|
+
if (cached != null)
|
|
50
|
+
return cached;
|
|
27
51
|
const selectorId = JSON.stringify(selector);
|
|
28
52
|
const optionsId = isEmptyOptions(options) ? -1 : JSON.stringify(options);
|
|
29
|
-
|
|
53
|
+
const id = `${selectorId}:${optionsId}`;
|
|
54
|
+
if (cachedForSelector) {
|
|
55
|
+
cachedForSelector.set(optionsKey, id);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
cache.set(selector, new WeakMap([[optionsKey, id]]));
|
|
59
|
+
}
|
|
60
|
+
return id;
|
|
30
61
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@signaldb/svelte",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.0-beta.
|
|
4
|
+
"version": "2.0.0-beta.14",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "tsc -d --noEmit false",
|
|
7
7
|
"analyze-bundle": "bundle-analyzer ./dist --upload-token=$BUNDLE_ANALYZER_UPLOAD_TOKEN --bundle-name=@signaldb/svelte",
|