@jucie.io/state 1.0.37 → 1.0.38
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/README.md +9 -9
- package/dist/State.js +144 -231
- package/dist/lib/array.js +23 -6
- package/dist/lib/change.js +4 -0
- package/dist/lib/gsru.js +33 -9
- package/dist/lib/marker.js +42 -8
- package/dist/lib/pathEncoder.js +8 -0
- package/dist/lib/query/canonicalQuery.js +21 -0
- package/dist/lib/query/isMatch.js +5 -0
- package/dist/lib/query/isQuerySegment.js +5 -0
- package/dist/lib/reportedAs.js +5 -0
- package/dist/lib/tree/resolveChunks.js +46 -0
- package/dist/lib/tree/traverseChunks.js +29 -0
- package/dist/main.js +4 -4
- package/dist/main.js.map +4 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -220,15 +220,15 @@ endBatch(); // Manually end the batch
|
|
|
220
220
|
|
|
221
221
|
### Queries
|
|
222
222
|
|
|
223
|
-
The state system provides tree-searching capabilities using `
|
|
223
|
+
The state system provides tree-searching capabilities using `findWhere` and `findAllWhere`:
|
|
224
224
|
|
|
225
|
-
#### `
|
|
225
|
+
#### `findWhere(key, matcher, value)`
|
|
226
226
|
|
|
227
227
|
Find the first path where a key matches a condition.
|
|
228
228
|
|
|
229
229
|
```javascript
|
|
230
230
|
// Find first user with role 'admin'
|
|
231
|
-
const adminPath = state.
|
|
231
|
+
const adminPath = state.findWhere('role', 'is', 'admin');
|
|
232
232
|
// Returns: ['users', 0] (path to the matching node)
|
|
233
233
|
|
|
234
234
|
// Then get the value
|
|
@@ -237,9 +237,9 @@ if (adminPath) {
|
|
|
237
237
|
}
|
|
238
238
|
|
|
239
239
|
// Other matchers
|
|
240
|
-
state.
|
|
241
|
-
state.
|
|
242
|
-
state.
|
|
240
|
+
state.findWhere('age', '>', 18); // Greater than
|
|
241
|
+
state.findWhere('age', '>=', 18); // Greater than or equal
|
|
242
|
+
state.findWhere('status', '!==', 'inactive'); // Not equal
|
|
243
243
|
```
|
|
244
244
|
|
|
245
245
|
**Supported matchers:**
|
|
@@ -253,13 +253,13 @@ state.findWhen('status', '!==', 'inactive'); // Not equal
|
|
|
253
253
|
- `'has'` - Object has value
|
|
254
254
|
- `'in'` - Key exists in object
|
|
255
255
|
|
|
256
|
-
#### `
|
|
256
|
+
#### `findAllWhere(key, matcher, value)`
|
|
257
257
|
|
|
258
258
|
Find all paths where a key matches a condition.
|
|
259
259
|
|
|
260
260
|
```javascript
|
|
261
261
|
// Find all users with active status
|
|
262
|
-
const activePaths = state.
|
|
262
|
+
const activePaths = state.findAllWhere('active', 'is', true);
|
|
263
263
|
// Returns: [['users', 0], ['users', 2], ['users', 5]]
|
|
264
264
|
|
|
265
265
|
// Get all matching values
|
|
@@ -480,7 +480,7 @@ const state = createState({
|
|
|
480
480
|
});
|
|
481
481
|
|
|
482
482
|
// Find active users
|
|
483
|
-
const activePaths = state.
|
|
483
|
+
const activePaths = state.findAllWhere('active', 'is', true);
|
|
484
484
|
const activeUsers = activePaths.map(path => state.get(path));
|
|
485
485
|
|
|
486
486
|
// Calculate total likes manually
|
package/dist/State.js
CHANGED
|
@@ -1,30 +1,23 @@
|
|
|
1
1
|
import { get, set, remove, update } from './lib/gsru.js';
|
|
2
2
|
import { push as arrayPush, unshift as arrayUnshift, splice as arraySplice } from './lib/array.js';
|
|
3
|
-
import { createMarker, createChildMarker, dispatch } from './lib/marker.js';
|
|
3
|
+
import { createMarker, createChildMarker, dispatch, compareMarkers, isGlobalMarker, isMarkers } from './lib/marker.js';
|
|
4
4
|
import { pack } from './admin/pack.js';
|
|
5
5
|
import { unpack } from './admin/unpack.js';
|
|
6
6
|
import { clear } from './lib/tree/mutate.js';
|
|
7
|
-
import { findWhen, findAllWhen
|
|
7
|
+
import { findWhen, findAllWhen } from './lib/tree/seek.js';
|
|
8
8
|
import { traverse } from './lib/tree/traverse.js';
|
|
9
|
-
import { CREATED, DELETED, UPDATED } from './lib/TOKENS.js';
|
|
9
|
+
import { CREATED, DELETED, UPDATED, MATCH_EXACT, MATCH_PARENT } from './lib/TOKENS.js';
|
|
10
10
|
import { createChange } from './lib/change.js';
|
|
11
11
|
import { clone } from './utils/clone.js';
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
if (
|
|
20
|
-
|
|
21
|
-
const common = [];
|
|
22
|
-
const first = paths[0];
|
|
23
|
-
for (let i = 0; i < first.length; i++) {
|
|
24
|
-
if (paths.every(p => p[i] === first[i])) common.push(first[i]);
|
|
25
|
-
else break;
|
|
26
|
-
}
|
|
27
|
-
return common;
|
|
12
|
+
import { isMatch } from './lib/query/isMatch.js';
|
|
13
|
+
import { resolveChunks } from './lib/tree/resolveChunks.js';
|
|
14
|
+
import { reportedAs } from './lib/reportedAs.js';
|
|
15
|
+
|
|
16
|
+
// One level of an item copied, for `updateEach` to hand over: a list or object is a new one holding the same values.
|
|
17
|
+
function shallowCopyOf(value) {
|
|
18
|
+
if (Array.isArray(value)) return [...value];
|
|
19
|
+
if (value !== null && typeof value === 'object') return { ...value };
|
|
20
|
+
return value;
|
|
28
21
|
}
|
|
29
22
|
|
|
30
23
|
export class State {
|
|
@@ -33,7 +26,8 @@ export class State {
|
|
|
33
26
|
#batchDepth = 0;
|
|
34
27
|
#silentDepth = 0;
|
|
35
28
|
#quietDepth = 0;
|
|
36
|
-
#
|
|
29
|
+
#quietMarker = null; // the path the open quiet reports at (the outermost's), else null
|
|
30
|
+
#quietWrote = false; // whether a write under it has been noted, since nothing written means nothing reported
|
|
37
31
|
#changeHandler;
|
|
38
32
|
#accessHandler;
|
|
39
33
|
#plugins = new Map();
|
|
@@ -139,33 +133,17 @@ export class State {
|
|
|
139
133
|
return value;
|
|
140
134
|
}
|
|
141
135
|
|
|
142
|
-
//
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
const state = get(this.#state, this.#marker, null);
|
|
150
|
-
return findAllWhen(state, key, matcher, value);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// Value queries (object-query, returns values)
|
|
154
|
-
findWhere(...args) {
|
|
155
|
-
const query = args.pop();
|
|
156
|
-
const parentMarker = args.length ? createChildMarker(this.#marker, args) : this.#marker;
|
|
157
|
-
const subtree = get(this.#state, parentMarker, this.#accessHandler);
|
|
158
|
-
const targetPath = findWhere(subtree, query);
|
|
159
|
-
if (targetPath === undefined) return undefined;
|
|
160
|
-
return traverse(subtree, targetPath);
|
|
136
|
+
// ── Where: find by a condition, then write ─────────────────────
|
|
137
|
+
// `findWhere('children', 'includes', id)` reads as it says: the path to the first `children` key, searched for from
|
|
138
|
+
// this state (a ref's own path, or the root), whose value meets the condition. The writes are a find, then an
|
|
139
|
+
// ordinary write to the object holding that key, and they report as that write: the path found, never the
|
|
140
|
+
// condition, since a replica searching again could find something else.
|
|
141
|
+
findWhere(key, matcher = 'is', value) {
|
|
142
|
+
return findWhen(get(this.#state, this.#marker, null), key, matcher, value);
|
|
161
143
|
}
|
|
162
144
|
|
|
163
|
-
findAllWhere(
|
|
164
|
-
|
|
165
|
-
const parentMarker = args.length ? createChildMarker(this.#marker, args) : this.#marker;
|
|
166
|
-
const subtree = get(this.#state, parentMarker, this.#accessHandler);
|
|
167
|
-
const paths = findAllWhere(subtree, query);
|
|
168
|
-
return paths.map(p => traverse(subtree, p));
|
|
145
|
+
findAllWhere(key, matcher = 'is', value) {
|
|
146
|
+
return findAllWhen(get(this.#state, this.#marker, null), key, matcher, value);
|
|
169
147
|
}
|
|
170
148
|
|
|
171
149
|
// Mutations
|
|
@@ -184,36 +162,53 @@ export class State {
|
|
|
184
162
|
return update(this.#state, marker, fn, 'update', this.#changeHandler);
|
|
185
163
|
}
|
|
186
164
|
|
|
165
|
+
/**
|
|
166
|
+
* Every item of the list or object at a path through `fn(item, key)`, written back as one change at that path, so a
|
|
167
|
+
* reader walks once. Each item is handed over as a shallow copy, never the live one, so `fn` may change it and
|
|
168
|
+
* return it: the item as it was stays in the change's `from`, and the one written is a new object, which anything
|
|
169
|
+
* comparing by identity notices. A field nested inside an item is still the live one; copy it before changing it.
|
|
170
|
+
*
|
|
171
|
+
* What `fn` returns takes the item's place, and `undefined` removes it: a list closes up, an object loses the key.
|
|
172
|
+
* Not `update`, which deep-clones the whole value first. Returns what was written, or undefined, without calling
|
|
173
|
+
* `fn`, when there's no list or object at the path.
|
|
174
|
+
*/
|
|
175
|
+
updateEach(...args) {
|
|
176
|
+
const fn = args.pop();
|
|
177
|
+
const marker = createChildMarker(this.#marker, args);
|
|
178
|
+
const current = get(this.#state, marker, this.#accessHandler);
|
|
179
|
+
if (current === null || typeof current !== 'object') return undefined;
|
|
180
|
+
|
|
181
|
+
let next;
|
|
182
|
+
if (Array.isArray(current)) {
|
|
183
|
+
next = [];
|
|
184
|
+
for (let index = 0; index < current.length; index++) {
|
|
185
|
+
const written = fn(shallowCopyOf(current[index]), index);
|
|
186
|
+
if (written !== undefined) next.push(written);
|
|
187
|
+
}
|
|
188
|
+
} else {
|
|
189
|
+
next = {};
|
|
190
|
+
for (const key of Object.keys(current)) {
|
|
191
|
+
const written = fn(shallowCopyOf(current[key]), key);
|
|
192
|
+
if (written !== undefined) next[key] = written;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return set(this.#state, marker, next, 'updateEach', this.#changeHandler);
|
|
196
|
+
}
|
|
197
|
+
|
|
187
198
|
apply(changeEntries) {
|
|
188
199
|
for (const changeEntry of changeEntries) {
|
|
189
200
|
const { path, operation, method, to, query } = changeEntry;
|
|
190
201
|
|
|
191
|
-
//
|
|
202
|
+
// An add by key is the one change that carries a query: its `path` is the list, and the item is added unless
|
|
203
|
+
// one with that key is already there, so a replica that gets it twice still ends up with one.
|
|
192
204
|
if (method === 'addWhere' || method === 'addFirstWhere') {
|
|
193
205
|
this.#addUnlessFound([path], query, to, 'apply', method === 'addWhere' ? arrayPush : arrayUnshift);
|
|
194
206
|
continue;
|
|
195
207
|
}
|
|
196
208
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
const parentMarker = createChildMarker(this.#marker, [searchPath]);
|
|
201
|
-
const subtree = get(this.#state, parentMarker, this.#accessHandler);
|
|
202
|
-
|
|
203
|
-
let targetPath;
|
|
204
|
-
if (Array.isArray(query)) {
|
|
205
|
-
const [key, matcher, testValue] = query;
|
|
206
|
-
const fullPath = findWhen(subtree, key, matcher, testValue);
|
|
207
|
-
targetPath = fullPath ? fullPath.slice(0, -1) : undefined;
|
|
208
|
-
} else {
|
|
209
|
-
targetPath = findWhere(subtree, query);
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
if (targetPath === undefined) continue;
|
|
213
|
-
marker = createChildMarker(this.#marker, [[...searchPath, ...targetPath]]);
|
|
214
|
-
} else {
|
|
215
|
-
marker = createChildMarker(this.#marker, path);
|
|
216
|
-
}
|
|
209
|
+
// Every other change is written at its path as given. A query segment in it (`[…, { name: 'src' }]`) resolves
|
|
210
|
+
// here, in this state, to this replica's item, the way any write resolves one.
|
|
211
|
+
const marker = createChildMarker(this.#marker, path);
|
|
217
212
|
|
|
218
213
|
switch (operation) {
|
|
219
214
|
case CREATED:
|
|
@@ -288,153 +283,40 @@ export class State {
|
|
|
288
283
|
return add(this.#state, marker, value, method, (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query));
|
|
289
284
|
}
|
|
290
285
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
return
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
setWhere(path, query, value) {
|
|
297
|
-
const parentMarker = createChildMarker(this.#marker, path);
|
|
298
|
-
const subtree = get(this.#state, parentMarker, this.#accessHandler);
|
|
299
|
-
const targetPath = findWhere(subtree, query);
|
|
300
|
-
if (targetPath === undefined) return undefined;
|
|
301
|
-
const fullPath = [...this.#resolveByPath(path), ...targetPath];
|
|
302
|
-
const marker = createChildMarker(this.#marker, [fullPath]);
|
|
303
|
-
return set(this.#state, marker, value, 'setWhere', (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query));
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
updateWhere(path, query, fn) {
|
|
307
|
-
const parentMarker = createChildMarker(this.#marker, path);
|
|
308
|
-
const subtree = get(this.#state, parentMarker, this.#accessHandler);
|
|
309
|
-
const targetPath = findWhere(subtree, query);
|
|
310
|
-
if (targetPath === undefined) return undefined;
|
|
311
|
-
const fullPath = [...this.#resolveByPath(path), ...targetPath];
|
|
312
|
-
const marker = createChildMarker(this.#marker, [fullPath]);
|
|
313
|
-
return update(this.#state, marker, fn, 'updateWhere', (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query));
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
removeWhere(path, query) {
|
|
317
|
-
const parentMarker = createChildMarker(this.#marker, path);
|
|
318
|
-
const subtree = get(this.#state, parentMarker, this.#accessHandler);
|
|
319
|
-
const targetPath = findWhere(subtree, query);
|
|
320
|
-
if (targetPath === undefined) return undefined;
|
|
321
|
-
const fullPath = [...this.#resolveByPath(path), ...targetPath];
|
|
322
|
-
const marker = createChildMarker(this.#marker, [fullPath]);
|
|
323
|
-
return remove(this.#state, marker, 'removeWhere', (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query));
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
// Object-query mutations (all matches)
|
|
327
|
-
setAllWhere(path, query, value) {
|
|
328
|
-
return this.batch(() => {
|
|
329
|
-
const parentMarker = createChildMarker(this.#marker, path);
|
|
330
|
-
const subtree = get(this.#state, parentMarker, this.#accessHandler);
|
|
331
|
-
const paths = findAllWhere(subtree, query);
|
|
332
|
-
const base = this.#resolveByPath(path);
|
|
333
|
-
const cb = (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query);
|
|
334
|
-
for (const targetPath of paths) {
|
|
335
|
-
const marker = createChildMarker(this.#marker, [[...base, ...targetPath]]);
|
|
336
|
-
set(this.#state, marker, value, 'setAllWhere', cb);
|
|
337
|
-
}
|
|
338
|
-
});
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
updateAllWhere(path, query, fn) {
|
|
342
|
-
return this.batch(() => {
|
|
343
|
-
const parentMarker = createChildMarker(this.#marker, path);
|
|
344
|
-
const subtree = get(this.#state, parentMarker, this.#accessHandler);
|
|
345
|
-
const paths = findAllWhere(subtree, query);
|
|
346
|
-
const base = this.#resolveByPath(path);
|
|
347
|
-
const cb = (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query);
|
|
348
|
-
for (const targetPath of paths) {
|
|
349
|
-
const marker = createChildMarker(this.#marker, [[...base, ...targetPath]]);
|
|
350
|
-
update(this.#state, marker, fn, 'updateAllWhere', cb);
|
|
351
|
-
}
|
|
352
|
-
});
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
removeAllWhere(path, query) {
|
|
356
|
-
return this.batch(() => {
|
|
357
|
-
const parentMarker = createChildMarker(this.#marker, path);
|
|
358
|
-
const subtree = get(this.#state, parentMarker, this.#accessHandler);
|
|
359
|
-
const paths = findAllWhere(subtree, query);
|
|
360
|
-
const base = this.#resolveByPath(path);
|
|
361
|
-
const cb = (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query);
|
|
362
|
-
const results = [];
|
|
363
|
-
for (let i = paths.length - 1; i >= 0; i--) {
|
|
364
|
-
const marker = createChildMarker(this.#marker, [[...base, ...paths[i]]]);
|
|
365
|
-
results.push(remove(this.#state, marker, 'removeAllWhere', cb));
|
|
366
|
-
}
|
|
367
|
-
return results;
|
|
368
|
-
});
|
|
286
|
+
setWhere(key, matcher, testValue, newValue) {
|
|
287
|
+
const path = this.findWhere(key, matcher, testValue);
|
|
288
|
+
return path === undefined ? undefined : this.set(path.slice(0, -1), newValue);
|
|
369
289
|
}
|
|
370
290
|
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
const targetPath = this.findWhen(key, matcher, testValue);
|
|
375
|
-
if (!targetPath) return undefined;
|
|
376
|
-
const parentPath = targetPath.slice(0, -1);
|
|
377
|
-
const marker = createChildMarker(this.#marker, [parentPath]);
|
|
378
|
-
return set(this.#state, marker, newValue, 'setWhen', (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query));
|
|
291
|
+
updateWhere(key, matcher, testValue, fn) {
|
|
292
|
+
const path = this.findWhere(key, matcher, testValue);
|
|
293
|
+
return path === undefined ? undefined : this.update(path.slice(0, -1), fn);
|
|
379
294
|
}
|
|
380
295
|
|
|
381
|
-
|
|
382
|
-
const
|
|
383
|
-
|
|
384
|
-
if (!targetPath) return undefined;
|
|
385
|
-
const parentPath = targetPath.slice(0, -1);
|
|
386
|
-
const marker = createChildMarker(this.#marker, [parentPath]);
|
|
387
|
-
return update(this.#state, marker, fn, 'updateWhen', (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query));
|
|
296
|
+
removeWhere(key, matcher, testValue) {
|
|
297
|
+
const path = this.findWhere(key, matcher, testValue);
|
|
298
|
+
return path === undefined ? undefined : this.remove(path.slice(0, -1));
|
|
388
299
|
}
|
|
389
300
|
|
|
390
|
-
|
|
391
|
-
const
|
|
392
|
-
const targetPath = this.findWhen(key, matcher, testValue);
|
|
393
|
-
if (!targetPath) return undefined;
|
|
394
|
-
const parentPath = targetPath.slice(0, -1);
|
|
395
|
-
const marker = createChildMarker(this.#marker, [parentPath]);
|
|
396
|
-
return remove(this.#state, marker, 'removeWhen', (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query));
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
// Seek-based mutations (all matches)
|
|
400
|
-
setAllWhen(key, matcher, testValue, newValue) {
|
|
301
|
+
setAllWhere(key, matcher, testValue, newValue) {
|
|
302
|
+
const paths = this.findAllWhere(key, matcher, testValue);
|
|
401
303
|
return this.batch(() => {
|
|
402
|
-
const
|
|
403
|
-
const cb = (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query);
|
|
404
|
-
const paths = this.findAllWhen(key, matcher, testValue);
|
|
405
|
-
for (const targetPath of paths) {
|
|
406
|
-
const parentPath = targetPath.slice(0, -1);
|
|
407
|
-
const marker = createChildMarker(this.#marker, [parentPath]);
|
|
408
|
-
set(this.#state, marker, newValue, 'setAllWhen', cb);
|
|
409
|
-
}
|
|
304
|
+
for (const path of paths) this.set(path.slice(0, -1), newValue);
|
|
410
305
|
});
|
|
411
306
|
}
|
|
412
307
|
|
|
413
|
-
|
|
308
|
+
updateAllWhere(key, matcher, testValue, fn) {
|
|
309
|
+
const paths = this.findAllWhere(key, matcher, testValue);
|
|
414
310
|
return this.batch(() => {
|
|
415
|
-
const
|
|
416
|
-
const cb = (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query);
|
|
417
|
-
const paths = this.findAllWhen(key, matcher, testValue);
|
|
418
|
-
for (const targetPath of paths) {
|
|
419
|
-
const parentPath = targetPath.slice(0, -1);
|
|
420
|
-
const marker = createChildMarker(this.#marker, [parentPath]);
|
|
421
|
-
update(this.#state, marker, fn, 'updateAllWhen', cb);
|
|
422
|
-
}
|
|
311
|
+
for (const path of paths) this.update(path.slice(0, -1), fn);
|
|
423
312
|
});
|
|
424
313
|
}
|
|
425
314
|
|
|
426
|
-
|
|
315
|
+
removeAllWhere(key, matcher, testValue) {
|
|
316
|
+
const paths = this.findAllWhere(key, matcher, testValue);
|
|
317
|
+
// Last first: removing a list item shifts the indices of the items after it, which the later paths still hold.
|
|
427
318
|
return this.batch(() => {
|
|
428
|
-
|
|
429
|
-
const cb = (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query);
|
|
430
|
-
const paths = this.findAllWhen(key, matcher, testValue);
|
|
431
|
-
const results = [];
|
|
432
|
-
for (let i = paths.length - 1; i >= 0; i--) {
|
|
433
|
-
const parentPath = paths[i].slice(0, -1);
|
|
434
|
-
const marker = createChildMarker(this.#marker, [parentPath]);
|
|
435
|
-
results.push(remove(this.#state, marker, 'removeAllWhen', cb));
|
|
436
|
-
}
|
|
437
|
-
return results;
|
|
319
|
+
for (let i = paths.length - 1; i >= 0; i--) this.remove(paths[i].slice(0, -1));
|
|
438
320
|
});
|
|
439
321
|
}
|
|
440
322
|
|
|
@@ -479,7 +361,8 @@ export class State {
|
|
|
479
361
|
this.#batchDepth = 0;
|
|
480
362
|
this.#silentDepth = 0;
|
|
481
363
|
this.#quietDepth = 0;
|
|
482
|
-
this.#
|
|
364
|
+
this.#quietMarker = null;
|
|
365
|
+
this.#quietWrote = false;
|
|
483
366
|
}
|
|
484
367
|
|
|
485
368
|
|
|
@@ -521,57 +404,87 @@ export class State {
|
|
|
521
404
|
return this;
|
|
522
405
|
}
|
|
523
406
|
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
407
|
+
/**
|
|
408
|
+
* Many writes as one change, at a path the caller names: inside, a write under `path` is only noted, and when the
|
|
409
|
+
* outermost quiet ends one change goes out at that path, carrying the value there (or removing it, if it's gone), so
|
|
410
|
+
* another replica can apply it and a reader walks once. The path is named, not worked out from the writes, so the
|
|
411
|
+
* change is at the address the caller meant, a query in it included. It's relative to a ref, as every path is;
|
|
412
|
+
* `[]` is the ref's own path, or the whole state.
|
|
413
|
+
*
|
|
414
|
+
* A nested quiet names a path inside the open one's, and its writes count toward the outermost. A write outside the
|
|
415
|
+
* path is a mistake, but a write only reaches here once it's made: it's reported on its own, so nothing is lost,
|
|
416
|
+
* and then it throws.
|
|
417
|
+
*/
|
|
418
|
+
quiet(path, fn) {
|
|
419
|
+
if (!Array.isArray(path)) {
|
|
420
|
+
throw new TypeError('quiet: name the path its writes fall under, as in quiet(["nodeGraph"], (state) => …). Use [] for the whole state, or a ref\'s own path.');
|
|
534
421
|
}
|
|
535
|
-
this.#
|
|
536
|
-
|
|
422
|
+
const marker = path.length === 0 ? this.#marker : createChildMarker(this.#marker, [path]);
|
|
423
|
+
this.#root.#beginQuiet(marker);
|
|
537
424
|
if (!fn) return () => this.endQuiet();
|
|
538
425
|
try { fn(this); } finally { this.endQuiet(); }
|
|
539
426
|
return this;
|
|
540
427
|
}
|
|
541
428
|
|
|
542
429
|
endQuiet() {
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
430
|
+
this.#root.#endQuiet();
|
|
431
|
+
return this;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
#beginQuiet(marker) {
|
|
435
|
+
if (this.#quietDepth > 0 && !this.#isInsideQuiet(marker)) {
|
|
436
|
+
throw new Error(`quiet: "${marker.address}" isn't inside the quiet already open at "${this.#quietMarker.address}". Nest a quiet only under its parent's path.`);
|
|
546
437
|
}
|
|
547
|
-
this.#quietDepth--;
|
|
548
438
|
if (this.#quietDepth === 0) {
|
|
549
|
-
|
|
550
|
-
this.#
|
|
551
|
-
if (paths && paths.length > 0) {
|
|
552
|
-
const coalescedPath = findCommonAncestor(paths);
|
|
553
|
-
const marker = createMarker(coalescedPath.length ? [coalescedPath] : []);
|
|
554
|
-
const to = coalescedPath.length ? traverse(this.#state, coalescedPath) : this.#state;
|
|
555
|
-
const change = createChange(marker, 'quiet', to);
|
|
556
|
-
if (to === undefined) change.operation = DELETED;
|
|
557
|
-
this.#notifyPlugins('onStateChange', marker, change, false);
|
|
558
|
-
}
|
|
439
|
+
this.#quietMarker = marker;
|
|
440
|
+
this.#quietWrote = false;
|
|
559
441
|
}
|
|
560
|
-
|
|
442
|
+
this.#quietDepth++;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
#endQuiet() {
|
|
446
|
+
this.#quietDepth--;
|
|
447
|
+
if (this.#quietDepth > 0) return;
|
|
448
|
+
const marker = this.#quietMarker;
|
|
449
|
+
const wrote = this.#quietWrote;
|
|
450
|
+
this.#quietMarker = null;
|
|
451
|
+
this.#quietWrote = false;
|
|
452
|
+
if (!wrote) return;
|
|
453
|
+
|
|
454
|
+
if (isGlobalMarker(marker)) {
|
|
455
|
+
this.#notifyPlugins('onStateChange', marker, createChange(marker, 'quiet', this.#state), false);
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
// Read through the path's queries as any write does: a query that no longer finds its item reports it removed.
|
|
459
|
+
const resolved = resolveChunks(this.#state, marker.chunks);
|
|
460
|
+
const reported = resolved === undefined ? marker : reportedAs(marker, resolved);
|
|
461
|
+
const to = resolved === undefined ? undefined : traverse(this.#state, resolved);
|
|
462
|
+
const change = createChange(reported, 'quiet', to);
|
|
463
|
+
if (to === undefined) change.operation = DELETED;
|
|
464
|
+
this.#notifyPlugins('onStateChange', reported, change, false);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// A write at the open quiet's path or under it. Compared by address, so a query matches the same query.
|
|
468
|
+
#isInsideQuiet(marker) {
|
|
469
|
+
const quiet = this.#quietMarker;
|
|
470
|
+
if (isGlobalMarker(quiet)) return true;
|
|
471
|
+
if (isMarkers(marker)) return marker.children.every((child) => this.#isInsideQuiet(child));
|
|
472
|
+
const match = compareMarkers(marker, quiet);
|
|
473
|
+
return match === MATCH_EXACT || match === MATCH_PARENT;
|
|
561
474
|
}
|
|
562
475
|
|
|
563
476
|
// Private change handler
|
|
564
477
|
#createChangeHandler() {
|
|
565
478
|
return (marker, method, to = undefined, from = undefined, query) => {
|
|
566
479
|
if (this.#silentDepth > 0) return;
|
|
567
|
-
if (this.#
|
|
568
|
-
|
|
569
|
-
global: () => this.#quietPaths.push([]),
|
|
570
|
-
path: ({ path }) => this.#quietPaths.push(path),
|
|
571
|
-
});
|
|
480
|
+
if (this.#quietMarker && this.#isInsideQuiet(marker)) {
|
|
481
|
+
this.#quietWrote = true;
|
|
572
482
|
return;
|
|
573
483
|
}
|
|
574
484
|
this.#notifyPlugins('onStateChange', marker, createChange(marker, method, to, from, query), this.batching);
|
|
485
|
+
if (this.#quietMarker) {
|
|
486
|
+
throw new Error(`quiet: a write to "${marker.address}" falls outside the quiet path "${this.#quietMarker.address}". It was made, and reported on its own; write only under the quiet path, or name a wider one.`);
|
|
487
|
+
}
|
|
575
488
|
}
|
|
576
489
|
}
|
|
577
490
|
|
package/dist/lib/array.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { dispatch } from './marker.js';
|
|
2
2
|
import { traverse } from './tree/traverse.js';
|
|
3
3
|
import { graft } from './tree/mutate.js';
|
|
4
|
+
import { resolveChunks } from './tree/resolveChunks.js';
|
|
5
|
+
import { reportedAs } from './reportedAs.js';
|
|
4
6
|
|
|
5
7
|
function resolveArray(state, path) {
|
|
6
8
|
let arr = traverse(state, path);
|
|
@@ -15,9 +17,14 @@ function resolveArray(state, path) {
|
|
|
15
17
|
|
|
16
18
|
export function push(state, marker, value, method, cb) {
|
|
17
19
|
let shouldCallCallback = false;
|
|
20
|
+
let reportedMarker = marker;
|
|
18
21
|
try {
|
|
19
22
|
return dispatch(marker, {
|
|
20
|
-
path: (
|
|
23
|
+
path: (pathMarker) => {
|
|
24
|
+
const path = resolveChunks(state, pathMarker.chunks);
|
|
25
|
+
// Inside an item a query can't find: no list to write to, and no change.
|
|
26
|
+
if (path === undefined) return undefined;
|
|
27
|
+
reportedMarker = reportedAs(pathMarker, path);
|
|
21
28
|
const arr = resolveArray(state, path);
|
|
22
29
|
arr.push(value);
|
|
23
30
|
shouldCallCallback = true;
|
|
@@ -29,16 +36,21 @@ export function push(state, marker, value, method, cb) {
|
|
|
29
36
|
return undefined;
|
|
30
37
|
} finally {
|
|
31
38
|
if (shouldCallCallback && cb) {
|
|
32
|
-
cb(
|
|
39
|
+
cb(reportedMarker, method, value, undefined);
|
|
33
40
|
}
|
|
34
41
|
}
|
|
35
42
|
}
|
|
36
43
|
|
|
37
44
|
export function unshift(state, marker, value, method, cb) {
|
|
38
45
|
let shouldCallCallback = false;
|
|
46
|
+
let reportedMarker = marker;
|
|
39
47
|
try {
|
|
40
48
|
return dispatch(marker, {
|
|
41
|
-
path: (
|
|
49
|
+
path: (pathMarker) => {
|
|
50
|
+
const path = resolveChunks(state, pathMarker.chunks);
|
|
51
|
+
// Inside an item a query can't find: no list to write to, and no change.
|
|
52
|
+
if (path === undefined) return undefined;
|
|
53
|
+
reportedMarker = reportedAs(pathMarker, path);
|
|
42
54
|
const arr = resolveArray(state, path);
|
|
43
55
|
arr.unshift(value);
|
|
44
56
|
shouldCallCallback = true;
|
|
@@ -50,7 +62,7 @@ export function unshift(state, marker, value, method, cb) {
|
|
|
50
62
|
return undefined;
|
|
51
63
|
} finally {
|
|
52
64
|
if (shouldCallCallback && cb) {
|
|
53
|
-
cb(
|
|
65
|
+
cb(reportedMarker, method, value, undefined);
|
|
54
66
|
}
|
|
55
67
|
}
|
|
56
68
|
}
|
|
@@ -58,9 +70,14 @@ export function unshift(state, marker, value, method, cb) {
|
|
|
58
70
|
export function splice(state, marker, spliceArgs, method, cb) {
|
|
59
71
|
let removed;
|
|
60
72
|
let shouldCallCallback = false;
|
|
73
|
+
let reportedMarker = marker;
|
|
61
74
|
try {
|
|
62
75
|
return dispatch(marker, {
|
|
63
|
-
path: (
|
|
76
|
+
path: (pathMarker) => {
|
|
77
|
+
const path = resolveChunks(state, pathMarker.chunks);
|
|
78
|
+
// Inside an item a query can't find: no list to write to, and no change.
|
|
79
|
+
if (path === undefined) return undefined;
|
|
80
|
+
reportedMarker = reportedAs(pathMarker, path);
|
|
64
81
|
const arr = resolveArray(state, path);
|
|
65
82
|
removed = arr.splice(...spliceArgs);
|
|
66
83
|
shouldCallCallback = true;
|
|
@@ -72,7 +89,7 @@ export function splice(state, marker, spliceArgs, method, cb) {
|
|
|
72
89
|
return undefined;
|
|
73
90
|
} finally {
|
|
74
91
|
if (shouldCallCallback && cb) {
|
|
75
|
-
cb(
|
|
92
|
+
cb(reportedMarker, method, spliceArgs, removed);
|
|
76
93
|
}
|
|
77
94
|
}
|
|
78
95
|
}
|
package/dist/lib/change.js
CHANGED
|
@@ -42,6 +42,9 @@ export function createChange(marker, method, to, from, query) {
|
|
|
42
42
|
to
|
|
43
43
|
}),
|
|
44
44
|
path: (marker) => {
|
|
45
|
+
// `path` is the path as written, queries and all: what the write named. A path with a query also has
|
|
46
|
+
// `resolved`, where its queries landed then: a value for what needs to know where (history), never an address.
|
|
47
|
+
// A path with no query has none, so read `change.resolved ?? change.path`.
|
|
45
48
|
const change = {
|
|
46
49
|
path: marker.path,
|
|
47
50
|
method,
|
|
@@ -50,6 +53,7 @@ export function createChange(marker, method, to, from, query) {
|
|
|
50
53
|
to
|
|
51
54
|
};
|
|
52
55
|
if (query) change.query = query;
|
|
56
|
+
if (marker.resolved) change.resolved = marker.resolved;
|
|
53
57
|
return change;
|
|
54
58
|
},
|
|
55
59
|
});
|