@jucie.io/state 1.0.36 → 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/LICENSE +61 -0
- package/README.md +9 -9
- package/dist/State.js +170 -209
- 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 +3 -2
- package/plugins/history/README.md +11 -9
package/LICENSE
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
MIT License with Commons Clause
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Jucio
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
"Commons Clause" License Condition v1.0
|
|
26
|
+
|
|
27
|
+
The Software is provided to you by the Licensor under the License, as defined
|
|
28
|
+
below, subject to the following condition.
|
|
29
|
+
|
|
30
|
+
Without limiting other conditions in the License, the grant of rights under the
|
|
31
|
+
License will not include, and the License does not grant to you, the right to
|
|
32
|
+
Sell the Software.
|
|
33
|
+
|
|
34
|
+
For purposes of the foregoing, "Sell" means practicing any or all of the rights
|
|
35
|
+
granted to you under the License to provide to third parties, for a fee or other
|
|
36
|
+
consideration (including without limitation fees for hosting or consulting/
|
|
37
|
+
support services related to the Software), a product or service whose value
|
|
38
|
+
derives, entirely or substantially, from the functionality of the Software.
|
|
39
|
+
|
|
40
|
+
Any license notice or attribution required by the License must also include
|
|
41
|
+
this Commons Clause License Condition notice.
|
|
42
|
+
|
|
43
|
+
Software: @jucie.io/state
|
|
44
|
+
License: MIT License
|
|
45
|
+
Licensor: Jucio
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
ADDITIONAL TERMS
|
|
50
|
+
|
|
51
|
+
This software is provided "as-is" without any warranty, support, or guarantee.
|
|
52
|
+
The author(s) are not obligated to:
|
|
53
|
+
- Provide support or answer questions
|
|
54
|
+
- Accept or implement feature requests
|
|
55
|
+
- Review or merge pull requests
|
|
56
|
+
- Fix bugs or security issues
|
|
57
|
+
- Maintain or update the software
|
|
58
|
+
|
|
59
|
+
You may submit issues and pull requests, but there is no expectation that they
|
|
60
|
+
will be addressed. Use this software at your own risk.
|
|
61
|
+
|
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,25 +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
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
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;
|
|
23
21
|
}
|
|
24
22
|
|
|
25
23
|
export class State {
|
|
@@ -28,10 +26,14 @@ export class State {
|
|
|
28
26
|
#batchDepth = 0;
|
|
29
27
|
#silentDepth = 0;
|
|
30
28
|
#quietDepth = 0;
|
|
31
|
-
#
|
|
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
|
|
32
31
|
#changeHandler;
|
|
33
32
|
#accessHandler;
|
|
34
33
|
#plugins = new Map();
|
|
34
|
+
// The state a ref was made from. Its change handler is the one every ref
|
|
35
|
+
// shares, so quiet's bookkeeping and plugins live there.
|
|
36
|
+
#root;
|
|
35
37
|
|
|
36
38
|
static create (state = {}) {
|
|
37
39
|
const instance = new State(state);
|
|
@@ -39,7 +41,8 @@ export class State {
|
|
|
39
41
|
return instance;
|
|
40
42
|
}
|
|
41
43
|
|
|
42
|
-
constructor(state, marker = null, changeHandler = null, accessHandler = null) {
|
|
44
|
+
constructor(state, marker = null, changeHandler = null, accessHandler = null, root = null) {
|
|
45
|
+
this.#root = root || this;
|
|
43
46
|
this.#state = state;
|
|
44
47
|
this.#changeHandler = changeHandler || this.#createChangeHandler();
|
|
45
48
|
this.#accessHandler = accessHandler || this.#createAccessHandler();
|
|
@@ -130,33 +133,17 @@ export class State {
|
|
|
130
133
|
return value;
|
|
131
134
|
}
|
|
132
135
|
|
|
133
|
-
//
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
const state = get(this.#state, this.#marker, null);
|
|
141
|
-
return findAllWhen(state, key, matcher, value);
|
|
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);
|
|
142
143
|
}
|
|
143
144
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const query = args.pop();
|
|
147
|
-
const parentMarker = args.length ? createChildMarker(this.#marker, args) : this.#marker;
|
|
148
|
-
const subtree = get(this.#state, parentMarker, this.#accessHandler);
|
|
149
|
-
const targetPath = findWhere(subtree, query);
|
|
150
|
-
if (targetPath === undefined) return undefined;
|
|
151
|
-
return traverse(subtree, targetPath);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
findAllWhere(...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 paths = findAllWhere(subtree, query);
|
|
159
|
-
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);
|
|
160
147
|
}
|
|
161
148
|
|
|
162
149
|
// Mutations
|
|
@@ -175,31 +162,54 @@ export class State {
|
|
|
175
162
|
return update(this.#state, marker, fn, 'update', this.#changeHandler);
|
|
176
163
|
}
|
|
177
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
|
+
|
|
178
198
|
apply(changeEntries) {
|
|
179
199
|
for (const changeEntry of changeEntries) {
|
|
180
200
|
const { path, operation, method, to, query } = changeEntry;
|
|
181
201
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
let targetPath;
|
|
189
|
-
if (Array.isArray(query)) {
|
|
190
|
-
const [key, matcher, testValue] = query;
|
|
191
|
-
const fullPath = findWhen(subtree, key, matcher, testValue);
|
|
192
|
-
targetPath = fullPath ? fullPath.slice(0, -1) : undefined;
|
|
193
|
-
} else {
|
|
194
|
-
targetPath = findWhere(subtree, query);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
if (targetPath === undefined) continue;
|
|
198
|
-
marker = createChildMarker(this.#marker, [[...searchPath, ...targetPath]]);
|
|
199
|
-
} else {
|
|
200
|
-
marker = createChildMarker(this.#marker, path);
|
|
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.
|
|
204
|
+
if (method === 'addWhere' || method === 'addFirstWhere') {
|
|
205
|
+
this.#addUnlessFound([path], query, to, 'apply', method === 'addWhere' ? arrayPush : arrayUnshift);
|
|
206
|
+
continue;
|
|
201
207
|
}
|
|
202
208
|
|
|
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);
|
|
212
|
+
|
|
203
213
|
switch (operation) {
|
|
204
214
|
case CREATED:
|
|
205
215
|
case UPDATED:
|
|
@@ -255,153 +265,58 @@ export class State {
|
|
|
255
265
|
return arraySplice(this.#state, marker, spliceArgs, 'splice', this.#changeHandler);
|
|
256
266
|
}
|
|
257
267
|
|
|
258
|
-
//
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
setWhere(path, query, value) {
|
|
264
|
-
const parentMarker = createChildMarker(this.#marker, path);
|
|
265
|
-
const subtree = get(this.#state, parentMarker, this.#accessHandler);
|
|
266
|
-
const targetPath = findWhere(subtree, query);
|
|
267
|
-
if (targetPath === undefined) return undefined;
|
|
268
|
-
const fullPath = [...this.#resolveByPath(path), ...targetPath];
|
|
269
|
-
const marker = createChildMarker(this.#marker, [fullPath]);
|
|
270
|
-
return set(this.#state, marker, value, 'setWhere', (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query));
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
updateWhere(path, query, fn) {
|
|
274
|
-
const parentMarker = createChildMarker(this.#marker, path);
|
|
275
|
-
const subtree = get(this.#state, parentMarker, this.#accessHandler);
|
|
276
|
-
const targetPath = findWhere(subtree, query);
|
|
277
|
-
if (targetPath === undefined) return undefined;
|
|
278
|
-
const fullPath = [...this.#resolveByPath(path), ...targetPath];
|
|
279
|
-
const marker = createChildMarker(this.#marker, [fullPath]);
|
|
280
|
-
return update(this.#state, marker, fn, 'updateWhere', (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query));
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
removeWhere(path, query) {
|
|
284
|
-
const parentMarker = createChildMarker(this.#marker, path);
|
|
285
|
-
const subtree = get(this.#state, parentMarker, this.#accessHandler);
|
|
286
|
-
const targetPath = findWhere(subtree, query);
|
|
287
|
-
if (targetPath === undefined) return undefined;
|
|
288
|
-
const fullPath = [...this.#resolveByPath(path), ...targetPath];
|
|
289
|
-
const marker = createChildMarker(this.#marker, [fullPath]);
|
|
290
|
-
return remove(this.#state, marker, 'removeWhere', (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query));
|
|
268
|
+
// Adds an item unless the list already has one matching `query`. The change
|
|
269
|
+
// carries the query, so a replica that gets it twice still ends up with one.
|
|
270
|
+
addWhere(path, query, value) {
|
|
271
|
+
return this.#addUnlessFound(path, query, value, 'addWhere', arrayPush);
|
|
291
272
|
}
|
|
292
273
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
return this.batch(() => {
|
|
296
|
-
const parentMarker = createChildMarker(this.#marker, path);
|
|
297
|
-
const subtree = get(this.#state, parentMarker, this.#accessHandler);
|
|
298
|
-
const paths = findAllWhere(subtree, query);
|
|
299
|
-
const base = this.#resolveByPath(path);
|
|
300
|
-
const cb = (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query);
|
|
301
|
-
for (const targetPath of paths) {
|
|
302
|
-
const marker = createChildMarker(this.#marker, [[...base, ...targetPath]]);
|
|
303
|
-
set(this.#state, marker, value, 'setAllWhere', cb);
|
|
304
|
-
}
|
|
305
|
-
});
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
updateAllWhere(path, query, fn) {
|
|
309
|
-
return this.batch(() => {
|
|
310
|
-
const parentMarker = createChildMarker(this.#marker, path);
|
|
311
|
-
const subtree = get(this.#state, parentMarker, this.#accessHandler);
|
|
312
|
-
const paths = findAllWhere(subtree, query);
|
|
313
|
-
const base = this.#resolveByPath(path);
|
|
314
|
-
const cb = (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query);
|
|
315
|
-
for (const targetPath of paths) {
|
|
316
|
-
const marker = createChildMarker(this.#marker, [[...base, ...targetPath]]);
|
|
317
|
-
update(this.#state, marker, fn, 'updateAllWhere', cb);
|
|
318
|
-
}
|
|
319
|
-
});
|
|
274
|
+
addFirstWhere(path, query, value) {
|
|
275
|
+
return this.#addUnlessFound(path, query, value, 'addFirstWhere', arrayUnshift);
|
|
320
276
|
}
|
|
321
277
|
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
const cb = (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query);
|
|
329
|
-
const results = [];
|
|
330
|
-
for (let i = paths.length - 1; i >= 0; i--) {
|
|
331
|
-
const marker = createChildMarker(this.#marker, [[...base, ...paths[i]]]);
|
|
332
|
-
results.push(remove(this.#state, marker, 'removeAllWhere', cb));
|
|
333
|
-
}
|
|
334
|
-
return results;
|
|
335
|
-
});
|
|
278
|
+
#addUnlessFound(path, query, value, method, add) {
|
|
279
|
+
const marker = createChildMarker(this.#marker, path);
|
|
280
|
+
const list = get(this.#state, marker, this.#accessHandler);
|
|
281
|
+
const existing = Array.isArray(list) ? list.find((item) => isMatch(item, query)) : undefined;
|
|
282
|
+
if (existing !== undefined) return existing;
|
|
283
|
+
return add(this.#state, marker, value, method, (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query));
|
|
336
284
|
}
|
|
337
285
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
const targetPath = this.findWhen(key, matcher, testValue);
|
|
342
|
-
if (!targetPath) return undefined;
|
|
343
|
-
const parentPath = targetPath.slice(0, -1);
|
|
344
|
-
const marker = createChildMarker(this.#marker, [parentPath]);
|
|
345
|
-
return set(this.#state, marker, newValue, 'setWhen', (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query));
|
|
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);
|
|
346
289
|
}
|
|
347
290
|
|
|
348
|
-
|
|
349
|
-
const
|
|
350
|
-
|
|
351
|
-
if (!targetPath) return undefined;
|
|
352
|
-
const parentPath = targetPath.slice(0, -1);
|
|
353
|
-
const marker = createChildMarker(this.#marker, [parentPath]);
|
|
354
|
-
return update(this.#state, marker, fn, 'updateWhen', (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);
|
|
355
294
|
}
|
|
356
295
|
|
|
357
|
-
|
|
358
|
-
const
|
|
359
|
-
|
|
360
|
-
if (!targetPath) return undefined;
|
|
361
|
-
const parentPath = targetPath.slice(0, -1);
|
|
362
|
-
const marker = createChildMarker(this.#marker, [parentPath]);
|
|
363
|
-
return remove(this.#state, marker, 'removeWhen', (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));
|
|
364
299
|
}
|
|
365
300
|
|
|
366
|
-
|
|
367
|
-
|
|
301
|
+
setAllWhere(key, matcher, testValue, newValue) {
|
|
302
|
+
const paths = this.findAllWhere(key, matcher, testValue);
|
|
368
303
|
return this.batch(() => {
|
|
369
|
-
const
|
|
370
|
-
const cb = (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query);
|
|
371
|
-
const paths = this.findAllWhen(key, matcher, testValue);
|
|
372
|
-
for (const targetPath of paths) {
|
|
373
|
-
const parentPath = targetPath.slice(0, -1);
|
|
374
|
-
const marker = createChildMarker(this.#marker, [parentPath]);
|
|
375
|
-
set(this.#state, marker, newValue, 'setAllWhen', cb);
|
|
376
|
-
}
|
|
304
|
+
for (const path of paths) this.set(path.slice(0, -1), newValue);
|
|
377
305
|
});
|
|
378
306
|
}
|
|
379
307
|
|
|
380
|
-
|
|
308
|
+
updateAllWhere(key, matcher, testValue, fn) {
|
|
309
|
+
const paths = this.findAllWhere(key, matcher, testValue);
|
|
381
310
|
return this.batch(() => {
|
|
382
|
-
const
|
|
383
|
-
const cb = (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query);
|
|
384
|
-
const paths = this.findAllWhen(key, matcher, testValue);
|
|
385
|
-
for (const targetPath of paths) {
|
|
386
|
-
const parentPath = targetPath.slice(0, -1);
|
|
387
|
-
const marker = createChildMarker(this.#marker, [parentPath]);
|
|
388
|
-
update(this.#state, marker, fn, 'updateAllWhen', cb);
|
|
389
|
-
}
|
|
311
|
+
for (const path of paths) this.update(path.slice(0, -1), fn);
|
|
390
312
|
});
|
|
391
313
|
}
|
|
392
314
|
|
|
393
|
-
|
|
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.
|
|
394
318
|
return this.batch(() => {
|
|
395
|
-
|
|
396
|
-
const cb = (marker, method, to, from) => this.#changeHandler(marker, method, to, from, query);
|
|
397
|
-
const paths = this.findAllWhen(key, matcher, testValue);
|
|
398
|
-
const results = [];
|
|
399
|
-
for (let i = paths.length - 1; i >= 0; i--) {
|
|
400
|
-
const parentPath = paths[i].slice(0, -1);
|
|
401
|
-
const marker = createChildMarker(this.#marker, [parentPath]);
|
|
402
|
-
results.push(remove(this.#state, marker, 'removeAllWhen', cb));
|
|
403
|
-
}
|
|
404
|
-
return results;
|
|
319
|
+
for (let i = paths.length - 1; i >= 0; i--) this.remove(paths[i].slice(0, -1));
|
|
405
320
|
});
|
|
406
321
|
}
|
|
407
322
|
|
|
@@ -446,14 +361,15 @@ export class State {
|
|
|
446
361
|
this.#batchDepth = 0;
|
|
447
362
|
this.#silentDepth = 0;
|
|
448
363
|
this.#quietDepth = 0;
|
|
449
|
-
this.#
|
|
364
|
+
this.#quietMarker = null;
|
|
365
|
+
this.#quietWrote = false;
|
|
450
366
|
}
|
|
451
367
|
|
|
452
368
|
|
|
453
369
|
// Reactivity Utilities
|
|
454
370
|
ref(...path) {
|
|
455
371
|
const marker = createChildMarker(this.#marker, path);
|
|
456
|
-
return new State(this.#state, marker, this.#changeHandler, this.#accessHandler);
|
|
372
|
+
return new State(this.#state, marker, this.#changeHandler, this.#accessHandler, this.#root);
|
|
457
373
|
}
|
|
458
374
|
|
|
459
375
|
batch(fn) {
|
|
@@ -488,42 +404,87 @@ export class State {
|
|
|
488
404
|
return this;
|
|
489
405
|
}
|
|
490
406
|
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
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.');
|
|
497
421
|
}
|
|
498
|
-
|
|
422
|
+
const marker = path.length === 0 ? this.#marker : createChildMarker(this.#marker, [path]);
|
|
423
|
+
this.#root.#beginQuiet(marker);
|
|
424
|
+
if (!fn) return () => this.endQuiet();
|
|
425
|
+
try { fn(this); } finally { this.endQuiet(); }
|
|
426
|
+
return this;
|
|
499
427
|
}
|
|
500
428
|
|
|
501
429
|
endQuiet() {
|
|
502
|
-
this.#
|
|
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.`);
|
|
437
|
+
}
|
|
503
438
|
if (this.#quietDepth === 0) {
|
|
504
|
-
|
|
505
|
-
this.#
|
|
506
|
-
if (paths && paths.length > 0) {
|
|
507
|
-
const coalescedPath = findCommonAncestor(paths);
|
|
508
|
-
const marker = createMarker(coalescedPath.length ? [coalescedPath] : []);
|
|
509
|
-
this.#notifyPlugins('onStateChange', marker, createChange(marker, 'quiet'), false);
|
|
510
|
-
}
|
|
439
|
+
this.#quietMarker = marker;
|
|
440
|
+
this.#quietWrote = false;
|
|
511
441
|
}
|
|
512
|
-
|
|
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;
|
|
513
474
|
}
|
|
514
475
|
|
|
515
476
|
// Private change handler
|
|
516
477
|
#createChangeHandler() {
|
|
517
478
|
return (marker, method, to = undefined, from = undefined, query) => {
|
|
518
479
|
if (this.#silentDepth > 0) return;
|
|
519
|
-
if (this.#
|
|
520
|
-
|
|
521
|
-
global: () => this.#quietPaths.push([]),
|
|
522
|
-
path: ({ path }) => this.#quietPaths.push(path),
|
|
523
|
-
});
|
|
480
|
+
if (this.#quietMarker && this.#isInsideQuiet(marker)) {
|
|
481
|
+
this.#quietWrote = true;
|
|
524
482
|
return;
|
|
525
483
|
}
|
|
526
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
|
+
}
|
|
527
488
|
}
|
|
528
489
|
}
|
|
529
490
|
|