@rohal12/spindle 0.64.28 → 0.64.30
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/pkg/format.js +1 -1
- package/dist/pkg/headless.js +173 -127
- package/dist/pkg/story-variables.js +50 -22
- package/package.json +1 -1
- package/src/class-registry.ts +1 -11
- package/src/code-check.ts +2 -2
- package/src/components/macros/Dialog.tsx +2 -4
- package/src/components/macros/arg-utils.ts +6 -0
- package/src/define-macro.ts +6 -5
- package/src/execute-mutation.ts +10 -0
- package/src/hooks/use-modal-focus.ts +7 -14
- package/src/js-bindings.ts +68 -20
- package/src/store.ts +5 -1
- package/src/story-api.ts +21 -16
- package/src/structural.ts +7 -9
- package/src/utils/control-edits.ts +113 -36
- package/src/utils/namespace.ts +15 -6
- package/src/utils/object-path.ts +2 -7
package/src/define-macro.ts
CHANGED
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
import { useInterpolate } from './hooks/use-interpolate';
|
|
15
15
|
import {
|
|
16
16
|
ItemEditContext,
|
|
17
|
-
|
|
17
|
+
runAsControlEdit,
|
|
18
18
|
runAsItemEdit,
|
|
19
19
|
} from './utils/control-edits';
|
|
20
20
|
import { useMergedLocals } from './hooks/use-merged-locals';
|
|
@@ -254,13 +254,14 @@ export function defineMacro<const P extends readonly ParameterDef[] = []>(
|
|
|
254
254
|
ctx.setValue = displayOnly
|
|
255
255
|
? refuse
|
|
256
256
|
: (value: unknown) => {
|
|
257
|
-
noteControlEdit(segments);
|
|
258
257
|
// The commit mutation code uses, which keeps the references among
|
|
259
258
|
// variables: a write to a draft copies only the path written, and
|
|
260
259
|
// other variables sharing the object would keep the old one.
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
260
|
+
runAsControlEdit(segments, () => {
|
|
261
|
+
mutateState((work, adopt) => {
|
|
262
|
+
setByPath(work.variables, segments, adopt(value), {
|
|
263
|
+
createMissing: true,
|
|
264
|
+
});
|
|
264
265
|
});
|
|
265
266
|
});
|
|
266
267
|
};
|
package/src/execute-mutation.ts
CHANGED
|
@@ -116,6 +116,16 @@ function clonerFor(
|
|
|
116
116
|
return (value) => deepClone(value, { keepUnregistered: true, seen, settle });
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
+
/** Adopt setter inputs into a draft, retaining references to owned state. */
|
|
120
|
+
export function adoptStateValues(target: VariableNamespaces): Cloner {
|
|
121
|
+
const held = locateObjects(namespacesOf(useStoryStore.getState()), []);
|
|
122
|
+
const scope = getActiveMutationScope();
|
|
123
|
+
if (scope) {
|
|
124
|
+
for (const [value, path] of locateObjects(scope, [])) held.set(value, path);
|
|
125
|
+
}
|
|
126
|
+
return clonerFor(target, held);
|
|
127
|
+
}
|
|
128
|
+
|
|
119
129
|
/** One cloner per target copy, made when first used. */
|
|
120
130
|
const clonersFor = (
|
|
121
131
|
paths?: (target: object) => ReadonlyMap<object, ObjectPath>,
|
|
@@ -28,21 +28,14 @@ const FOCUSABLE = [
|
|
|
28
28
|
*/
|
|
29
29
|
function isAvailable(el: HTMLElement): boolean {
|
|
30
30
|
if (!isRendered(el)) return false;
|
|
31
|
-
//
|
|
31
|
+
// Boxless containers can render focusable children, but cannot take focus
|
|
32
|
+
// themselves. Keep traversing their descendants in focusables().
|
|
33
|
+
const view = el.ownerDocument.defaultView ?? window;
|
|
34
|
+
if (view.getComputedStyle(el).display === 'contents') return false;
|
|
35
|
+
// Rendered visibility includes CSS overrides of hidden, until-found
|
|
36
|
+
// contents, closed details and shadow trees. Inertness is independent.
|
|
32
37
|
for (let at: Element | null = el; at; at = composedParent(at)) {
|
|
33
|
-
if (at.hasAttribute('
|
|
34
|
-
if (at.ownerDocument.defaultView?.getComputedStyle(at).display === 'none')
|
|
35
|
-
return false;
|
|
36
|
-
const parent = composedParent(at);
|
|
37
|
-
if (
|
|
38
|
-
parent?.localName === 'details' &&
|
|
39
|
-
!parent.hasAttribute('open') &&
|
|
40
|
-
at !==
|
|
41
|
-
Array.from(parent.children).find(
|
|
42
|
-
(child) => child.localName === 'summary',
|
|
43
|
-
)
|
|
44
|
-
)
|
|
45
|
-
return false;
|
|
38
|
+
if (at.hasAttribute('inert')) return false;
|
|
46
39
|
}
|
|
47
40
|
// Also covers controls disabled by a <fieldset disabled> ancestor
|
|
48
41
|
return !el.matches(':disabled');
|
package/src/js-bindings.ts
CHANGED
|
@@ -9,7 +9,6 @@
|
|
|
9
9
|
* cannot place, it counts as a declaration of unknown value: a parameter, a
|
|
10
10
|
* pattern, an assignment. Better no answer than another declaration's.
|
|
11
11
|
*/
|
|
12
|
-
import { escapeRegExp } from './components/macros/arg-utils';
|
|
13
12
|
|
|
14
13
|
type Kind = '{' | '(' | '[';
|
|
15
14
|
|
|
@@ -21,6 +20,44 @@ interface Group {
|
|
|
21
20
|
parent: Group | undefined;
|
|
22
21
|
}
|
|
23
22
|
|
|
23
|
+
/** Search arbitrary (also recovered, overlapping) bracket intervals. */
|
|
24
|
+
class GroupIndex {
|
|
25
|
+
private readonly size: number;
|
|
26
|
+
private readonly ends: number[];
|
|
27
|
+
|
|
28
|
+
constructor(private readonly groups: readonly Group[]) {
|
|
29
|
+
let size = 1;
|
|
30
|
+
while (size < groups.length) size *= 2;
|
|
31
|
+
this.size = size;
|
|
32
|
+
this.ends = new Array<number>(size * 2).fill(-1);
|
|
33
|
+
for (let i = 0; i < groups.length; i++)
|
|
34
|
+
this.ends[size + i] = groups[i]!.close;
|
|
35
|
+
for (let i = size - 1; i > 0; i--) {
|
|
36
|
+
this.ends[i] = Math.max(this.ends[i * 2]!, this.ends[i * 2 + 1]!);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Latest opener before `at` whose closer follows it, in logarithmic time. */
|
|
41
|
+
containing(at: number): Group | undefined {
|
|
42
|
+
const find = (
|
|
43
|
+
node: number,
|
|
44
|
+
start: number,
|
|
45
|
+
end: number,
|
|
46
|
+
): Group | undefined => {
|
|
47
|
+
if (
|
|
48
|
+
start >= this.groups.length ||
|
|
49
|
+
this.groups[start]!.open >= at ||
|
|
50
|
+
this.ends[node]! <= at
|
|
51
|
+
)
|
|
52
|
+
return undefined;
|
|
53
|
+
if (end - start === 1) return this.groups[start];
|
|
54
|
+
const mid = (start + end) >>> 1;
|
|
55
|
+
return find(node * 2 + 1, mid, end) ?? find(node * 2, start, mid);
|
|
56
|
+
};
|
|
57
|
+
return find(1, 0, this.size);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
24
61
|
/** The text a name is visible in: `[start, end)`. */
|
|
25
62
|
interface Scope {
|
|
26
63
|
start: number;
|
|
@@ -75,7 +112,9 @@ const isWordChar = (c: string | undefined) => !!c && /[\w$]/.test(c);
|
|
|
75
112
|
|
|
76
113
|
/** Resolve the declaration of variables in `code` (see blankedCode in macro-discovery). */
|
|
77
114
|
export class Bindings {
|
|
115
|
+
private readonly identifiers = new Map<string, number[]>();
|
|
78
116
|
private readonly groups: Group[] = [];
|
|
117
|
+
private readonly groupIndexes = new Map<Kind | undefined, GroupIndex>();
|
|
79
118
|
private readonly byOpen = new Map<number, Group>();
|
|
80
119
|
private readonly byClose = new Map<number, Group>();
|
|
81
120
|
private readonly functions = new Map<Group, boolean>();
|
|
@@ -88,6 +127,23 @@ export class Bindings {
|
|
|
88
127
|
|
|
89
128
|
constructor(private readonly code: string) {
|
|
90
129
|
this.readGroups();
|
|
130
|
+
for (const kind of [undefined, '{', '(', '['] as const) {
|
|
131
|
+
this.groupIndexes.set(
|
|
132
|
+
kind,
|
|
133
|
+
new GroupIndex(
|
|
134
|
+
kind === undefined
|
|
135
|
+
? this.groups
|
|
136
|
+
: this.groups.filter((g) => g.kind === kind),
|
|
137
|
+
),
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
// One source scan serves every config name, including distinct names.
|
|
141
|
+
// Keep the same word boundaries as the binding reader (also for `$`).
|
|
142
|
+
for (const match of code.matchAll(/[\w$]+/g)) {
|
|
143
|
+
const positions = this.identifiers.get(match[0]) ?? [];
|
|
144
|
+
positions.push(match.index);
|
|
145
|
+
this.identifiers.set(match[0], positions);
|
|
146
|
+
}
|
|
91
147
|
}
|
|
92
148
|
|
|
93
149
|
/**
|
|
@@ -119,13 +175,9 @@ export class Bindings {
|
|
|
119
175
|
name: string,
|
|
120
176
|
properties: ReadonlySet<string>,
|
|
121
177
|
): Binding[] {
|
|
122
|
-
const { code } = this;
|
|
123
|
-
const escaped = escapeRegExp(name);
|
|
124
178
|
const bindings: Binding[] = [];
|
|
125
|
-
for (const
|
|
126
|
-
|
|
127
|
-
)) {
|
|
128
|
-
const binding = this.bindingAt(m.index, name.length, properties);
|
|
179
|
+
for (const at of this.identifiers.get(name) ?? []) {
|
|
180
|
+
const binding = this.bindingAt(at, name.length, properties);
|
|
129
181
|
if (binding) bindings.push(binding);
|
|
130
182
|
}
|
|
131
183
|
// A write is to the declaration it sees: in the scope of that one
|
|
@@ -194,14 +246,7 @@ export class Bindings {
|
|
|
194
246
|
|
|
195
247
|
/** The innermost group that has `index` inside it, of this kind if given. */
|
|
196
248
|
private innermost(index: number, kind?: Kind): Group | undefined {
|
|
197
|
-
|
|
198
|
-
for (let k = groups.length - 1; k >= 0; k--) {
|
|
199
|
-
const g = groups[k]!;
|
|
200
|
-
if (g.open < index && index < g.close && (!kind || g.kind === kind)) {
|
|
201
|
-
return g;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
return undefined;
|
|
249
|
+
return this.groupIndexes.get(kind)!.containing(index);
|
|
205
250
|
}
|
|
206
251
|
|
|
207
252
|
/**
|
|
@@ -618,10 +663,13 @@ export class Bindings {
|
|
|
618
663
|
/** `Object.assign(x, …)` and the like: the name is the first argument. */
|
|
619
664
|
private isMutatedArgument(before: number, end: number): boolean {
|
|
620
665
|
const group = this.byOpen.get(before);
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
);
|
|
666
|
+
if (group?.kind !== '(' || !',)'.includes(this.code.charAt(end)))
|
|
667
|
+
return false;
|
|
668
|
+
// The regexp only needs the callee, not every preceding statement.
|
|
669
|
+
const method = this.wordBefore(this.backFrom(group.open) + 1);
|
|
670
|
+
const dot = this.backFrom(method.start);
|
|
671
|
+
if (this.code.charAt(dot) !== '.') return false;
|
|
672
|
+
const owner = this.wordBefore(this.backFrom(dot) + 1);
|
|
673
|
+
return MUTATING_CALL_RE.test(this.code.slice(owner.start, group.open));
|
|
626
674
|
}
|
|
627
675
|
}
|
package/src/store.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { immer } from 'zustand/middleware/immer';
|
|
|
4
4
|
import type { StateCreator } from 'zustand/vanilla';
|
|
5
5
|
import {
|
|
6
6
|
current,
|
|
7
|
+
produce,
|
|
7
8
|
enableMapSet,
|
|
8
9
|
enablePatches,
|
|
9
10
|
isDraft,
|
|
@@ -66,6 +67,7 @@ import {
|
|
|
66
67
|
type PathChange,
|
|
67
68
|
} from './structural';
|
|
68
69
|
import { getByPath, setByPath } from './utils/object-path';
|
|
70
|
+
import { notifyItemEdits } from './utils/control-edits';
|
|
69
71
|
import { isIndexKey } from './utils/value-kinds';
|
|
70
72
|
import {
|
|
71
73
|
noPassageError,
|
|
@@ -1297,10 +1299,12 @@ function storyStateGuard(
|
|
|
1297
1299
|
Object.assign(draft, updater);
|
|
1298
1300
|
};
|
|
1299
1301
|
const routed = routeStoreUpdate(recipe);
|
|
1300
|
-
|
|
1302
|
+
const before = get();
|
|
1303
|
+
const next = produce(before, (draft) => {
|
|
1301
1304
|
(routed ?? recipe)(draft);
|
|
1302
1305
|
keepNamespacesBare(draft);
|
|
1303
1306
|
});
|
|
1307
|
+
notifyItemEdits(before, next, () => set(next));
|
|
1304
1308
|
}) as typeof set;
|
|
1305
1309
|
api.setState = guarded;
|
|
1306
1310
|
const state = creator(guarded, get, api);
|
package/src/story-api.ts
CHANGED
|
@@ -26,6 +26,7 @@ import { registerClass } from './class-registry';
|
|
|
26
26
|
import {
|
|
27
27
|
frozenCopy,
|
|
28
28
|
readOnlyValue,
|
|
29
|
+
adoptStateValues,
|
|
29
30
|
getActiveMutationScope,
|
|
30
31
|
mutateState,
|
|
31
32
|
} from './execute-mutation';
|
|
@@ -282,12 +283,18 @@ function warnIfUndeclared(isTransient: boolean, key: string): void {
|
|
|
282
283
|
);
|
|
283
284
|
}
|
|
284
285
|
|
|
285
|
-
/**
|
|
286
|
-
function
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
286
|
+
/** Adopt the whole batch before writes can replace state-owned inputs. */
|
|
287
|
+
function setEntries(
|
|
288
|
+
draft: VariableNamespaces,
|
|
289
|
+
entries: readonly [string, unknown][],
|
|
290
|
+
adopt: (value: unknown) => unknown,
|
|
291
|
+
): void {
|
|
292
|
+
const adopted = entries.map(([name, value]) => [name, adopt(value)] as const);
|
|
293
|
+
for (const [name, value] of adopted) {
|
|
294
|
+
const { isTransient, key } = parseName(name);
|
|
295
|
+
const namespace = isTransient ? draft.transient : draft.variables;
|
|
296
|
+
setByPath(namespace, key.split('.'), value);
|
|
297
|
+
}
|
|
291
298
|
}
|
|
292
299
|
|
|
293
300
|
/**
|
|
@@ -345,22 +352,20 @@ function createStoryAPI(): StoryAPI {
|
|
|
345
352
|
warnIfUndeclared(isTransient, key);
|
|
346
353
|
if (!isTransient) recordHookWrite(key.split('.'));
|
|
347
354
|
}
|
|
348
|
-
// One store update for all keys,
|
|
349
|
-
//
|
|
350
|
-
// follows the code's own pending writes (program order, #215): see
|
|
351
|
-
// routeStoreUpdate.
|
|
355
|
+
// One store update for all keys, in program order with any running
|
|
356
|
+
// mutation. Nested writes use the mutation merge to preserve aliases.
|
|
352
357
|
if (entries.some(([k]) => k.includes('.'))) {
|
|
353
|
-
// A write below a variable goes through the commit mutation code
|
|
354
|
-
// uses (nested in the running code, if any: it continues from the
|
|
355
|
-
// code's pending writes, and they are committed first): an update of a draft would give the written path new
|
|
356
|
-
// objects and leave the other references to the old ones (#295).
|
|
357
358
|
mutateState((work, adopt) => {
|
|
358
|
-
|
|
359
|
+
setEntries(work, entries, adopt);
|
|
359
360
|
});
|
|
360
361
|
return;
|
|
361
362
|
}
|
|
362
363
|
useStoryStore.getState().updateVariables((draft) => {
|
|
363
|
-
for
|
|
364
|
+
// One adopter for the batch keeps shared references and cycles,
|
|
365
|
+
// including references to state already owned by the running code.
|
|
366
|
+
// Freezing caller inputs alone cannot protect mutable internal slots.
|
|
367
|
+
const adopt = adoptStateValues(draft);
|
|
368
|
+
setEntries(draft, entries, adopt);
|
|
364
369
|
});
|
|
365
370
|
},
|
|
366
371
|
|
package/src/structural.ts
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
// Temporal values, with shared references and cycles.
|
|
11
11
|
|
|
12
12
|
import { registeredClassName } from './class-registry';
|
|
13
|
-
import {
|
|
13
|
+
import { defineData, hasOwn } from './utils/namespace';
|
|
14
14
|
import { deleteByPath, getByPath, setByPath } from './utils/object-path';
|
|
15
15
|
import {
|
|
16
16
|
ERROR_HIDDEN_KEYS,
|
|
@@ -54,7 +54,7 @@ function builtinLike<C extends object>(
|
|
|
54
54
|
made(empty);
|
|
55
55
|
if (registeredClassName(obj) === undefined) return empty;
|
|
56
56
|
Object.setPrototypeOf(empty, Object.getPrototypeOf(obj) as object);
|
|
57
|
-
for (const key of extraKeys(obj))
|
|
57
|
+
for (const key of extraKeys(obj)) defineData(empty, key, value(key));
|
|
58
58
|
return empty;
|
|
59
59
|
}
|
|
60
60
|
|
|
@@ -102,13 +102,11 @@ export function deepClone<T>(value: T, options: DeepCloneOptions = {}): T {
|
|
|
102
102
|
*/
|
|
103
103
|
function copyKeys(obj: object, copy: object): object {
|
|
104
104
|
for (const key of Object.keys(obj)) {
|
|
105
|
-
|
|
105
|
+
defineData(copy, key, clone((obj as Record<string, unknown>)[key]));
|
|
106
106
|
}
|
|
107
107
|
for (const sym of Object.getOwnPropertySymbols(obj)) {
|
|
108
108
|
if (!Object.prototype.propertyIsEnumerable.call(obj, sym)) continue;
|
|
109
|
-
(copy as Record<symbol, unknown>)[sym]
|
|
110
|
-
(obj as Record<symbol, unknown>)[sym],
|
|
111
|
-
);
|
|
109
|
+
defineData(copy, sym, clone((obj as Record<symbol, unknown>)[sym]));
|
|
112
110
|
}
|
|
113
111
|
return copy;
|
|
114
112
|
}
|
|
@@ -184,7 +182,7 @@ export function deepClone<T>(value: T, options: DeepCloneOptions = {}): T {
|
|
|
184
182
|
// Holes stay holes
|
|
185
183
|
const arr = builtin(obj, new Array(val.length) as unknown[]);
|
|
186
184
|
for (const key of arrayIndices(val))
|
|
187
|
-
arr
|
|
185
|
+
defineData(arr, key, clone(val[Number(key)]));
|
|
188
186
|
return arr;
|
|
189
187
|
}
|
|
190
188
|
|
|
@@ -360,13 +358,13 @@ export function shareEqual<T>(prev: unknown, curr: T): T {
|
|
|
360
358
|
}
|
|
361
359
|
if (Array.isArray(c) && Array.isArray(p)) {
|
|
362
360
|
const out = collection(new Array(c.length) as unknown[]);
|
|
363
|
-
for (const key of arrayIndices(c)) out
|
|
361
|
+
for (const key of arrayIndices(c)) defineData(out, key, shareKey(key));
|
|
364
362
|
return out;
|
|
365
363
|
}
|
|
366
364
|
if (mergesWith(p, c, false)) {
|
|
367
365
|
const out = Object.create(Object.getPrototypeOf(c) as object | null);
|
|
368
366
|
done.set(c, out);
|
|
369
|
-
for (const key of Object.keys(cr))
|
|
367
|
+
for (const key of Object.keys(cr)) defineData(out, key, shareKey(key));
|
|
370
368
|
return out;
|
|
371
369
|
}
|
|
372
370
|
return fresh(c);
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { createContext } from 'preact';
|
|
2
|
-
import {
|
|
3
|
-
import { diffPaths } from '../structural';
|
|
2
|
+
import { deepEqualStrict, diffPaths } from '../structural';
|
|
4
3
|
import { getByPath } from './object-path';
|
|
5
4
|
import type { VariableNamespaces } from '../store';
|
|
6
5
|
|
|
@@ -22,42 +21,119 @@ import type { VariableNamespaces } from '../store';
|
|
|
22
21
|
*/
|
|
23
22
|
let version = 0;
|
|
24
23
|
const MAX_LOG = 4096;
|
|
25
|
-
/**
|
|
26
|
-
const
|
|
24
|
+
/** Object identities at their own commit; old story graphs stay collectible. */
|
|
25
|
+
const through = new WeakMap<object, number>();
|
|
26
|
+
/** Paths, not story graphs: revisit only the prefixes an edit actually used. */
|
|
27
|
+
const tracked = new Map<string, readonly string[]>();
|
|
28
|
+
/** Primitive slot edits also retain their committed value. */
|
|
29
|
+
const log: { version: number; path: readonly string[]; value: unknown }[] = [];
|
|
27
30
|
|
|
28
|
-
function note(path: readonly string[]): void {
|
|
29
|
-
|
|
31
|
+
function note(path: readonly string[], state: VariableNamespaces): void {
|
|
32
|
+
const at = ++version;
|
|
33
|
+
const key = JSON.stringify(path);
|
|
34
|
+
tracked.delete(key);
|
|
35
|
+
tracked.set(key, path);
|
|
36
|
+
if (tracked.size > MAX_LOG) tracked.delete(tracked.keys().next().value!);
|
|
37
|
+
let node: unknown = state;
|
|
38
|
+
for (const segment of path) {
|
|
39
|
+
node =
|
|
40
|
+
node !== null && typeof node === 'object'
|
|
41
|
+
? getByPath(node, [segment])
|
|
42
|
+
: undefined;
|
|
43
|
+
if (node !== null && typeof node === 'object') through.set(node, at);
|
|
44
|
+
}
|
|
45
|
+
if (node !== null && typeof node === 'object') return;
|
|
46
|
+
log.push({ version: at, path, value: node });
|
|
30
47
|
if (log.length > MAX_LOG) log.shift();
|
|
31
48
|
}
|
|
32
49
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
50
|
+
type Edits = readonly (readonly string[])[] | 'changed' | undefined;
|
|
51
|
+
let activeEdits: Edits;
|
|
52
|
+
|
|
53
|
+
function withEdits(edits: Edits, run: () => void): void {
|
|
54
|
+
const previous = activeEdits;
|
|
55
|
+
activeEdits = edits;
|
|
56
|
+
try {
|
|
57
|
+
run();
|
|
58
|
+
} finally {
|
|
59
|
+
activeEdits = previous;
|
|
60
|
+
}
|
|
36
61
|
}
|
|
37
62
|
|
|
38
|
-
/**
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
63
|
+
/** Attribute only the control's own writes, before subscribers can replace them. */
|
|
64
|
+
export function runAsControlEdit(
|
|
65
|
+
segments: readonly string[],
|
|
66
|
+
run: () => void,
|
|
67
|
+
): void {
|
|
68
|
+
withEdits([['variables', ...segments]], run);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Whether mutation code runs inside a {for} iteration. */
|
|
43
72
|
export const ItemEditContext = createContext(false);
|
|
44
73
|
|
|
45
74
|
const NAMESPACES = ['variables', 'temporary', 'transient'] as const;
|
|
46
75
|
|
|
47
|
-
/** Run `run`, noting each variable path
|
|
76
|
+
/** Run `run`, noting each variable path its own updates change as an edit. */
|
|
48
77
|
export function runAsItemEdit(run: () => void): void {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
78
|
+
withEdits('changed', run);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Called with finalized state before the store notifies its subscribers. */
|
|
82
|
+
export function notifyItemEdits(
|
|
83
|
+
before: VariableNamespaces,
|
|
84
|
+
after: VariableNamespaces,
|
|
85
|
+
notify: () => void,
|
|
86
|
+
): void {
|
|
87
|
+
// Commits can copy whole arrays, including unchanged rows. Carry an
|
|
88
|
+
// earlier edit to an equal copy even when a watcher changed a sibling;
|
|
89
|
+
// a replacement with different content cannot inherit the mark.
|
|
90
|
+
// Skip the namespace itself so unrelated state is never walked.
|
|
91
|
+
const compared = new WeakMap<object, WeakSet<object>>();
|
|
92
|
+
for (const path of tracked.values()) {
|
|
93
|
+
const ns = path[0] as keyof VariableNamespaces;
|
|
94
|
+
if (before[ns] === after[ns]) continue;
|
|
95
|
+
let previous: unknown = before[ns];
|
|
96
|
+
let next: unknown = after[ns];
|
|
97
|
+
for (const segment of path.slice(1)) {
|
|
98
|
+
previous =
|
|
99
|
+
previous !== null && typeof previous === 'object'
|
|
100
|
+
? getByPath(previous, [segment])
|
|
101
|
+
: undefined;
|
|
102
|
+
next =
|
|
103
|
+
next !== null && typeof next === 'object'
|
|
104
|
+
? getByPath(next, [segment])
|
|
105
|
+
: undefined;
|
|
106
|
+
if (
|
|
107
|
+
previous === null ||
|
|
108
|
+
typeof previous !== 'object' ||
|
|
109
|
+
next === null ||
|
|
110
|
+
typeof next !== 'object' ||
|
|
111
|
+
previous === next
|
|
112
|
+
)
|
|
113
|
+
continue;
|
|
114
|
+
const edited = through.get(previous);
|
|
115
|
+
if (edited === undefined) continue;
|
|
116
|
+
const copies = compared.get(previous) ?? new WeakSet<object>();
|
|
117
|
+
if (copies.has(next)) continue;
|
|
118
|
+
copies.add(next);
|
|
119
|
+
compared.set(previous, copies);
|
|
120
|
+
if (deepEqualStrict(previous, next)) {
|
|
121
|
+
through.set(next, Math.max(through.get(next) ?? 0, edited));
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (activeEdits === 'changed') {
|
|
54
126
|
for (const ns of NAMESPACES) {
|
|
55
127
|
if (before[ns] === after[ns]) continue;
|
|
56
128
|
for (const { path } of diffPaths(before[ns], after[ns], true)) {
|
|
57
|
-
note([ns, ...path]);
|
|
129
|
+
note([ns, ...path], after);
|
|
58
130
|
}
|
|
59
131
|
}
|
|
132
|
+
} else {
|
|
133
|
+
for (const path of activeEdits ?? []) note(path, after);
|
|
60
134
|
}
|
|
135
|
+
// Watcher writes are independent of the edit that triggered them.
|
|
136
|
+
withEdits(undefined, notify);
|
|
61
137
|
}
|
|
62
138
|
|
|
63
139
|
export function controlEditVersion(): number {
|
|
@@ -74,26 +150,27 @@ export function editedItems(
|
|
|
74
150
|
state: VariableNamespaces,
|
|
75
151
|
): (list: readonly unknown[], item: unknown, index: number) => boolean {
|
|
76
152
|
// The objects the paths lead through, and the keys they end at
|
|
77
|
-
const through = new Set<unknown>();
|
|
78
153
|
const ends = new Map<unknown, Set<string>>();
|
|
79
154
|
for (let i = log.length - 1; i >= 0 && log[i]!.version > since; i--) {
|
|
80
|
-
const { path } = log[i]!;
|
|
81
|
-
|
|
82
|
-
path.
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
155
|
+
const { path, value } = log[i]!;
|
|
156
|
+
const parent = getByPath(state, path.slice(0, -1));
|
|
157
|
+
const key = path[path.length - 1]!;
|
|
158
|
+
if (parent !== null && typeof parent === 'object') {
|
|
159
|
+
const current = getByPath(parent, [key]);
|
|
160
|
+
// A primitive edit survives later edits of other slots, but a watcher
|
|
161
|
+
// assigning a different value at the very same path is a replacement.
|
|
162
|
+
if (
|
|
163
|
+
(current === null || typeof current !== 'object') &&
|
|
164
|
+
Object.is(current, value)
|
|
165
|
+
) {
|
|
166
|
+
const keys = ends.get(parent) ?? new Set<string>();
|
|
167
|
+
keys.add(key);
|
|
168
|
+
ends.set(parent, keys);
|
|
87
169
|
}
|
|
88
|
-
|
|
89
|
-
node !== null && typeof node === 'object'
|
|
90
|
-
? getByPath(node, [seg])
|
|
91
|
-
: undefined;
|
|
92
|
-
if (node !== null && typeof node === 'object') through.add(node);
|
|
93
|
-
});
|
|
170
|
+
}
|
|
94
171
|
}
|
|
95
172
|
return (list, item, index) =>
|
|
96
173
|
typeof item === 'object' && item !== null
|
|
97
|
-
? through.
|
|
174
|
+
? (through.get(item) ?? 0) > since
|
|
98
175
|
: ends.get(list)?.has(String(index)) === true;
|
|
99
176
|
}
|
package/src/utils/namespace.ts
CHANGED
|
@@ -52,18 +52,27 @@ export const ownValue = (
|
|
|
52
52
|
key: string,
|
|
53
53
|
): unknown => (ns && hasOwn(ns, key) ? (ns as Namespace)[key] : undefined);
|
|
54
54
|
|
|
55
|
+
/** Define an own data field without invoking an inherited accessor. */
|
|
56
|
+
export function defineData(
|
|
57
|
+
target: object,
|
|
58
|
+
key: PropertyKey,
|
|
59
|
+
value: unknown,
|
|
60
|
+
): void {
|
|
61
|
+
Object.defineProperty(target, key, {
|
|
62
|
+
value,
|
|
63
|
+
enumerable: true,
|
|
64
|
+
writable: true,
|
|
65
|
+
configurable: true,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
55
69
|
/**
|
|
56
70
|
* Set an own enumerable property. A "__proto__" key is defined as an own
|
|
57
71
|
* property (as JSON.parse does) instead of replacing the prototype.
|
|
58
72
|
*/
|
|
59
73
|
export function setOwn(target: object, key: string, value: unknown): void {
|
|
60
74
|
if (key === '__proto__') {
|
|
61
|
-
|
|
62
|
-
value,
|
|
63
|
-
enumerable: true,
|
|
64
|
-
writable: true,
|
|
65
|
-
configurable: true,
|
|
66
|
-
});
|
|
75
|
+
defineData(target, key, value);
|
|
67
76
|
} else {
|
|
68
77
|
(target as Record<string, unknown>)[key] = value;
|
|
69
78
|
}
|
package/src/utils/object-path.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { current as draftState, isDraft, original } from 'immer';
|
|
2
2
|
import { registeredClassName } from '../class-registry';
|
|
3
|
-
import { hasOwn, isArrayIndex } from './namespace';
|
|
3
|
+
import { defineData, hasOwn, isArrayIndex } from './namespace';
|
|
4
4
|
import { atomicName } from './value-kinds';
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -56,12 +56,7 @@ export function shallowCopy(value: object): Record<string, unknown> {
|
|
|
56
56
|
// Define rather than assign (Object.assign), so that a "__proto__" key
|
|
57
57
|
// stays a key instead of replacing the copy's prototype
|
|
58
58
|
for (const key of Object.keys(value)) {
|
|
59
|
-
|
|
60
|
-
value: (value as Record<string, unknown>)[key],
|
|
61
|
-
enumerable: true,
|
|
62
|
-
writable: true,
|
|
63
|
-
configurable: true,
|
|
64
|
-
});
|
|
59
|
+
defineData(copy, key, (value as Record<string, unknown>)[key]);
|
|
65
60
|
}
|
|
66
61
|
return copy as Record<string, unknown>;
|
|
67
62
|
}
|