@rohal12/spindle 0.64.29 → 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.
@@ -1,6 +1,5 @@
1
1
  import { createContext } from 'preact';
2
- import { readState } from '../execute-mutation';
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
- /** Edits in the order made: paths below the namespaces (`['variables', 'a']`). */
26
- const log: { version: number; path: readonly string[] }[] = [];
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
- log.push({ version: ++version, path });
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
- /** Note an edit a control made to the story variable at `segments`. */
34
- export function noteControlEdit(segments: readonly string[]): void {
35
- note(['variables', ...segments]);
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
- * Whether mutation code runs inside a {for} iteration: {for} provides true
40
- * around its iterations, and the writes of such code are noted as edits of
41
- * the items they reach (see runAsItemEdit).
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 it changes as an edit. */
76
+ /** Run `run`, noting each variable path its own updates change as an edit. */
48
77
  export function runAsItemEdit(run: () => void): void {
49
- const before = readState();
50
- try {
51
- run();
52
- } finally {
53
- const after = readState();
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
- let node: unknown = state;
82
- path.forEach((seg, n) => {
83
- if (n === path.length - 1 && node !== null && typeof node === 'object') {
84
- const keys = ends.get(node) ?? new Set<string>();
85
- keys.add(seg);
86
- ends.set(node, keys);
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
- node =
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.has(item)
174
+ ? (through.get(item) ?? 0) > since
98
175
  : ends.get(list)?.has(String(index)) === true;
99
176
  }