statewire 0.0.1 → 0.0.2
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 +14 -13
- package/dist/client/changes.d.ts +2 -2
- package/dist/client/changes.d.ts.map +1 -1
- package/dist/client/changes.js +3 -10
- package/dist/client/changes.js.map +1 -1
- package/dist/client/connection.d.ts +18 -39
- package/dist/client/connection.d.ts.map +1 -1
- package/dist/client/connection.js +61 -54
- package/dist/client/connection.js.map +1 -1
- package/dist/client/core.d.ts +7 -11
- package/dist/client/core.d.ts.map +1 -1
- package/dist/client/core.js +58 -55
- package/dist/client/core.js.map +1 -1
- package/dist/client/session.d.ts +8 -25
- package/dist/client/session.d.ts.map +1 -1
- package/dist/client/session.js +56 -36
- package/dist/client/session.js.map +1 -1
- package/dist/client/sessionStore.d.ts +1 -1
- package/dist/client/sessionStore.js.map +1 -1
- package/dist/client/timing.d.ts +2 -1
- package/dist/client/timing.d.ts.map +1 -1
- package/dist/client/timing.js +5 -1
- package/dist/client/timing.js.map +1 -1
- package/dist/host/adapter.d.ts +8 -0
- package/dist/host/adapter.d.ts.map +1 -0
- package/dist/host/adapter.js +23 -0
- package/dist/host/adapter.js.map +1 -0
- package/dist/host/draft.d.ts +21 -7
- package/dist/host/draft.d.ts.map +1 -1
- package/dist/host/draft.js +190 -103
- package/dist/host/draft.js.map +1 -1
- package/dist/host/host.d.ts +18 -35
- package/dist/host/host.d.ts.map +1 -1
- package/dist/host/host.js +81 -53
- package/dist/host/host.js.map +1 -1
- package/dist/host/socket.d.ts +14 -0
- package/dist/host/socket.d.ts.map +1 -0
- package/dist/host/socket.js +459 -0
- package/dist/host/socket.js.map +1 -0
- package/dist/host-internal.d.ts +27 -0
- package/dist/host-internal.d.ts.map +1 -0
- package/dist/host-internal.js +3 -0
- package/dist/host.d.ts +44 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +4 -0
- package/dist/index.d.ts +5 -28
- package/dist/index.js +2 -3
- package/dist/statewire.d.ts +94 -10
- package/dist/statewire.d.ts.map +1 -1
- package/dist/statewire.js +23 -35
- package/dist/statewire.js.map +1 -1
- package/dist/transports/fake-sse.d.ts +12 -0
- package/dist/transports/fake-sse.d.ts.map +1 -0
- package/dist/transports/fake-sse.js +39 -0
- package/dist/transports/fake-sse.js.map +1 -0
- package/dist/transports/fixtures.d.ts +33 -0
- package/dist/transports/fixtures.d.ts.map +1 -0
- package/dist/transports/fixtures.js +50 -0
- package/dist/transports/fixtures.js.map +1 -0
- package/dist/transports/remote.d.ts +6 -13
- package/dist/transports/remote.d.ts.map +1 -1
- package/dist/transports/remote.js +5 -24
- package/dist/transports/remote.js.map +1 -1
- package/dist/transports/sse.d.ts +10 -20
- package/dist/transports/sse.d.ts.map +1 -1
- package/dist/transports/sse.js +42 -41
- package/dist/transports/sse.js.map +1 -1
- package/dist/transports/testing.d.ts +4 -3
- package/dist/transports/testing.d.ts.map +1 -1
- package/dist/transports/testing.js +19 -46
- package/dist/transports/testing.js.map +1 -1
- package/dist/transports/ws.d.ts +4 -13
- package/dist/transports/ws.d.ts.map +1 -1
- package/dist/transports/ws.js +57 -60
- package/dist/transports/ws.js.map +1 -1
- package/dist/types.d.ts +53 -100
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/util/draftTraps.d.ts +6 -0
- package/dist/util/draftTraps.d.ts.map +1 -0
- package/dist/util/draftTraps.js +25 -0
- package/dist/util/draftTraps.js.map +1 -0
- package/dist/util/macrotask.d.ts +5 -0
- package/dist/util/macrotask.d.ts.map +1 -0
- package/dist/util/macrotask.js +25 -0
- package/dist/util/macrotask.js.map +1 -0
- package/dist/util/methodProxy.d.ts +5 -0
- package/dist/util/methodProxy.d.ts.map +1 -0
- package/dist/util/methodProxy.js +17 -0
- package/dist/util/methodProxy.js.map +1 -0
- package/dist/util/randomId.d.ts +5 -0
- package/dist/util/randomId.d.ts.map +1 -0
- package/dist/util/randomId.js +6 -0
- package/dist/util/randomId.js.map +1 -0
- package/dist/{client/optimistic.d.ts → util/recordMutations.d.ts} +2 -2
- package/dist/util/recordMutations.d.ts.map +1 -0
- package/dist/{client/optimistic.js → util/recordMutations.js} +5 -24
- package/dist/util/recordMutations.js.map +1 -0
- package/dist/util/statePath.d.ts +14 -0
- package/dist/util/statePath.d.ts.map +1 -0
- package/dist/util/statePath.js +31 -0
- package/dist/util/statePath.js.map +1 -0
- package/dist/wire/meta.d.ts +6 -1
- package/dist/wire/meta.d.ts.map +1 -1
- package/dist/wire/meta.js +30 -28
- package/dist/wire/meta.js.map +1 -1
- package/dist/wire/ops.d.ts +5 -26
- package/dist/wire/ops.d.ts.map +1 -1
- package/dist/wire/ops.js +67 -80
- package/dist/wire/ops.js.map +1 -1
- package/package.json +15 -11
- package/src/client/changes.ts +5 -15
- package/src/client/connection.ts +87 -113
- package/src/client/core.ts +57 -91
- package/src/client/session.ts +87 -70
- package/src/client/sessionStore.ts +1 -1
- package/src/client/timing.ts +6 -0
- package/src/host/adapter.ts +33 -0
- package/src/host/draft.ts +293 -125
- package/src/host/host.ts +107 -106
- package/src/host/socket.ts +542 -0
- package/src/host-internal.ts +28 -0
- package/src/host.ts +61 -0
- package/src/index.ts +2 -62
- package/src/statewire.ts +154 -61
- package/src/transports/fake-sse.ts +51 -0
- package/src/transports/fixtures.ts +60 -0
- package/src/transports/remote.ts +12 -54
- package/src/transports/sse.ts +68 -81
- package/src/transports/testing.ts +27 -59
- package/src/transports/ws.ts +76 -92
- package/src/types.ts +50 -131
- package/src/util/draftTraps.ts +32 -0
- package/src/util/macrotask.ts +25 -0
- package/src/util/methodProxy.ts +16 -0
- package/src/util/randomId.ts +4 -0
- package/src/{client/optimistic.ts → util/recordMutations.ts} +0 -0
- package/src/util/statePath.ts +61 -0
- package/src/wire/meta.ts +42 -47
- package/src/wire/ops.ts +107 -106
- package/LICENSE +0 -21
- package/dist/client/optimistic.d.ts.map +0 -1
- package/dist/client/optimistic.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/server.d.ts +0 -15
- package/dist/server.d.ts.map +0 -1
- package/dist/server.js +0 -3
- package/src/server.ts +0 -32
package/src/host/draft.ts
CHANGED
|
@@ -1,34 +1,28 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Statewire } from "../types";
|
|
2
|
+
import type { StatewirePath } from "../wire/ops";
|
|
3
|
+
import { sanitizeValue } from "../wire/ops";
|
|
4
|
+
import { isUnsafeKey } from "../util/statePath";
|
|
5
|
+
import { sharedDraftTraps } from "../util/draftTraps";
|
|
2
6
|
|
|
3
|
-
export type ServerOp = Exclude<
|
|
7
|
+
export type ServerOp = Exclude<Statewire.Op, { op: "event" }>;
|
|
4
8
|
|
|
5
9
|
export type DraftHost = {
|
|
6
|
-
|
|
7
|
-
record: (op: ServerOp) => void;
|
|
10
|
+
snapshot: () => unknown;
|
|
11
|
+
record: (op: ServerOp, changedPath: StatewirePath) => void;
|
|
8
12
|
};
|
|
9
13
|
|
|
10
|
-
export
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const out: Record<string, unknown> = {};
|
|
17
|
-
for (const key of Object.keys(value)) {
|
|
18
|
-
const item = frozen((value as Record<string, unknown>)[key]);
|
|
19
|
-
if (item !== undefined) out[key] = item;
|
|
20
|
-
}
|
|
21
|
-
return out;
|
|
22
|
-
}
|
|
23
|
-
return typeof value === "function" ? undefined : value;
|
|
14
|
+
export type StateDraft<T> = {
|
|
15
|
+
proxy: T;
|
|
16
|
+
/** Seal pending writes and return the committed state. */
|
|
17
|
+
flush: () => unknown;
|
|
18
|
+
/** Committed state was replaced externally; cached nodes re-resolve. */
|
|
19
|
+
invalidate: () => void;
|
|
24
20
|
};
|
|
25
21
|
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
return node;
|
|
22
|
+
const NODE = Symbol("statewire.draftNode");
|
|
23
|
+
|
|
24
|
+
type NodeHandle = {
|
|
25
|
+
append: (key: string | number, suffix: string) => void;
|
|
32
26
|
};
|
|
33
27
|
|
|
34
28
|
const isIndex = (key: string) => /^(0|[1-9]\d*)$/.test(key);
|
|
@@ -36,120 +30,294 @@ const isIndex = (key: string) => /^(0|[1-9]\d*)$/.test(key);
|
|
|
36
30
|
const invalid = (path: StatewirePath, message: string) =>
|
|
37
31
|
new Error(`statewire: ${message} at path: ${path.join(".")}`);
|
|
38
32
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
33
|
+
type Container = Record<string | number, unknown>;
|
|
34
|
+
|
|
35
|
+
type Node = {
|
|
36
|
+
parent: Node | null;
|
|
37
|
+
key: string | number;
|
|
38
|
+
path: StatewirePath;
|
|
39
|
+
gen: number;
|
|
40
|
+
base: unknown;
|
|
41
|
+
copy: Container | null;
|
|
42
|
+
children: Map<string, Node>;
|
|
43
|
+
isArray: boolean;
|
|
44
|
+
proxy: object;
|
|
45
|
+
handle: NodeHandle;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const createStateDraft = <T>(host: DraftHost): StateDraft<T> => {
|
|
49
|
+
let gen = 0;
|
|
50
|
+
const dirty: Node[] = [];
|
|
51
|
+
|
|
52
|
+
const latest = (node: Node): unknown => node.copy ?? node.base;
|
|
53
|
+
|
|
54
|
+
const resolve = (node: Node): Node => {
|
|
55
|
+
if (node.gen === gen) return node;
|
|
56
|
+
node.gen = gen;
|
|
57
|
+
if (node.parent === null) {
|
|
58
|
+
if (node.copy === null) node.base = host.snapshot();
|
|
59
|
+
return node;
|
|
60
|
+
}
|
|
61
|
+
const value = (latest(resolve(node.parent)) as Container)[node.key];
|
|
62
|
+
if (value !== latest(node)) {
|
|
63
|
+
node.base = value;
|
|
64
|
+
node.copy = null;
|
|
65
|
+
}
|
|
66
|
+
return node;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const prepareForWrite = (node: Node): Container => {
|
|
70
|
+
resolve(node);
|
|
71
|
+
if (node.copy !== null) return node.copy;
|
|
72
|
+
const base = node.base;
|
|
73
|
+
node.copy = Array.isArray(base)
|
|
74
|
+
? (base.slice() as unknown as Container)
|
|
75
|
+
: { ...(base as Container) };
|
|
76
|
+
dirty.push(node);
|
|
77
|
+
if (node.parent !== null) {
|
|
78
|
+
prepareForWrite(node.parent)[node.key] = node.copy;
|
|
63
79
|
}
|
|
64
|
-
|
|
80
|
+
return node.copy;
|
|
65
81
|
};
|
|
66
82
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
83
|
+
const changedPathFor = (node: Node, key: string | number): StatewirePath =>
|
|
84
|
+
Array.isArray(latest(node)) ? node.path : [...node.path, key];
|
|
85
|
+
|
|
86
|
+
const applyAppend = (node: Node, rawKey: string | number, suffix: string) => {
|
|
87
|
+
const container = latest(resolve(node)) as Container;
|
|
88
|
+
const key =
|
|
89
|
+
Array.isArray(container) && typeof rawKey === "string" && isIndex(rawKey)
|
|
90
|
+
? Number(rawKey)
|
|
91
|
+
: rawKey;
|
|
92
|
+
const existing = Object.hasOwn(container as object, key)
|
|
93
|
+
? container[key]
|
|
94
|
+
: undefined;
|
|
95
|
+
if (typeof existing !== "string") {
|
|
96
|
+
throw invalid([...node.path, key], "cannot append to a non-string");
|
|
97
|
+
}
|
|
98
|
+
if (suffix === "") return;
|
|
99
|
+
prepareForWrite(node)[key] = existing + suffix;
|
|
100
|
+
host.record(
|
|
101
|
+
{ op: "add", path: [...node.path, key, existing.length], value: suffix },
|
|
102
|
+
changedPathFor(node, key),
|
|
103
|
+
);
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const createNode = (parent: Node | null, key: string | number): Node => {
|
|
107
|
+
const seed =
|
|
108
|
+
parent === null ? host.snapshot() : (latest(parent) as Container)[key];
|
|
109
|
+
const node: Node = {
|
|
110
|
+
parent,
|
|
111
|
+
key,
|
|
112
|
+
path: parent === null ? [] : [...parent.path, key],
|
|
113
|
+
gen,
|
|
114
|
+
base: seed,
|
|
115
|
+
copy: null,
|
|
116
|
+
children: new Map(),
|
|
117
|
+
isArray: Array.isArray(seed),
|
|
118
|
+
proxy: null!,
|
|
119
|
+
handle: null!,
|
|
120
|
+
};
|
|
121
|
+
node.handle = {
|
|
122
|
+
append: (childKey, suffix) => applyAppend(node, childKey, suffix),
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const current = () => latest(resolve(node));
|
|
126
|
+
|
|
127
|
+
const childProxy = (
|
|
128
|
+
prop: string,
|
|
129
|
+
childKey: string | number,
|
|
130
|
+
value: object,
|
|
131
|
+
) => {
|
|
132
|
+
let child = node.children.get(prop);
|
|
133
|
+
if (child === undefined || child.isArray !== Array.isArray(value)) {
|
|
134
|
+
child = createNode(node, childKey);
|
|
135
|
+
node.children.set(prop, child);
|
|
72
136
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
137
|
+
return child.proxy;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const assign = (
|
|
141
|
+
container: Container,
|
|
142
|
+
key: string | number,
|
|
143
|
+
value: unknown,
|
|
144
|
+
) => {
|
|
145
|
+
const old = Object.hasOwn(container as object, key)
|
|
146
|
+
? container[key]
|
|
147
|
+
: undefined;
|
|
148
|
+
if (old === value) return;
|
|
149
|
+
const isObjectContainer = !Array.isArray(container);
|
|
150
|
+
if (
|
|
151
|
+
isObjectContainer &&
|
|
152
|
+
isUnsafeKey(key, node.path[node.path.length - 1])
|
|
153
|
+
) {
|
|
154
|
+
throw invalid([...node.path, key], `cannot set unsafe key "${key}"`);
|
|
77
155
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
if (typeof prop === "symbol") return false;
|
|
82
|
-
const current = node();
|
|
83
|
-
const value = frozen(rawValue);
|
|
84
|
-
if (Array.isArray(current)) {
|
|
85
|
-
if (prop === "length") {
|
|
86
|
-
const next = Number(value);
|
|
87
|
-
if (!Number.isInteger(next) || next < 0 || next > current.length) {
|
|
88
|
-
throw invalid(path, `cannot set list length to ${String(value)}`);
|
|
89
|
-
}
|
|
90
|
-
for (let i = current.length - 1; i >= next; i--) {
|
|
91
|
-
host.record({ op: "remove", path: [...path, i] });
|
|
92
|
-
}
|
|
93
|
-
return true;
|
|
156
|
+
if (value === undefined) {
|
|
157
|
+
if (!isObjectContainer) {
|
|
158
|
+
throw invalid([...node.path, key], "cannot serialize undefined");
|
|
94
159
|
}
|
|
95
|
-
|
|
96
|
-
|
|
160
|
+
delete prepareForWrite(node)[key];
|
|
161
|
+
host.record({ op: "remove", path: [...node.path, key] }, [
|
|
162
|
+
...node.path,
|
|
163
|
+
key,
|
|
164
|
+
]);
|
|
165
|
+
gen++;
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
prepareForWrite(node)[key] = isObjectContainer
|
|
169
|
+
? sanitizeValue(value, key as string)
|
|
170
|
+
: sanitizeValue(value);
|
|
171
|
+
host.record(
|
|
172
|
+
{ op: "replace", path: [...node.path, key], value },
|
|
173
|
+
changedPathFor(node, key),
|
|
174
|
+
);
|
|
175
|
+
if (
|
|
176
|
+
(typeof old === "object" && old !== null) ||
|
|
177
|
+
(typeof value === "object" && value !== null)
|
|
178
|
+
) {
|
|
179
|
+
gen++;
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
node.proxy = new Proxy(seed as object, {
|
|
184
|
+
get: (_target, prop) => {
|
|
185
|
+
if (prop === NODE) return node.handle;
|
|
186
|
+
const container = current();
|
|
187
|
+
if (typeof prop === "symbol") {
|
|
188
|
+
return (container as Record<symbol, unknown>)[prop];
|
|
97
189
|
}
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
190
|
+
const c = container as Container;
|
|
191
|
+
const key = Array.isArray(c) && isIndex(prop) ? Number(prop) : prop;
|
|
192
|
+
const value = c[key];
|
|
193
|
+
if (typeof value === "object" && value !== null) {
|
|
194
|
+
return childProxy(prop, key, value);
|
|
101
195
|
}
|
|
102
|
-
|
|
103
|
-
|
|
196
|
+
return value;
|
|
197
|
+
},
|
|
198
|
+
set: (_target, prop, value) => {
|
|
199
|
+
if (typeof prop === "symbol") return false;
|
|
200
|
+
const container = current();
|
|
201
|
+
if (Array.isArray(container)) {
|
|
202
|
+
if (prop === "length") {
|
|
203
|
+
const next = Number(value);
|
|
204
|
+
if (
|
|
205
|
+
!Number.isInteger(next) ||
|
|
206
|
+
next < 0 ||
|
|
207
|
+
next > container.length
|
|
208
|
+
) {
|
|
209
|
+
throw invalid(
|
|
210
|
+
node.path,
|
|
211
|
+
`cannot set list length to ${String(value)}`,
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
if (next === container.length) return true;
|
|
215
|
+
const copy = prepareForWrite(node) as unknown as unknown[];
|
|
216
|
+
for (let i = container.length - 1; i >= next; i--) {
|
|
217
|
+
copy.splice(i, 1);
|
|
218
|
+
host.record({ op: "remove", path: [...node.path, i] }, node.path);
|
|
219
|
+
}
|
|
220
|
+
gen++;
|
|
221
|
+
return true;
|
|
222
|
+
}
|
|
223
|
+
if (!isIndex(prop)) {
|
|
224
|
+
throw invalid(node.path, `cannot set key "${prop}" on a list`);
|
|
225
|
+
}
|
|
226
|
+
const index = Number(prop);
|
|
227
|
+
if (index > container.length) {
|
|
228
|
+
throw invalid(node.path, `list index ${index} out of range`);
|
|
229
|
+
}
|
|
230
|
+
if (index === container.length) {
|
|
231
|
+
if (value === undefined) {
|
|
232
|
+
throw invalid(
|
|
233
|
+
[...node.path, index],
|
|
234
|
+
"cannot serialize undefined",
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
prepareForWrite(node)[index] = sanitizeValue(value);
|
|
238
|
+
host.record(
|
|
239
|
+
{ op: "add", path: [...node.path, index], value },
|
|
240
|
+
node.path,
|
|
241
|
+
);
|
|
242
|
+
gen++;
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
assign(container as unknown as Container, index, value);
|
|
104
246
|
return true;
|
|
105
247
|
}
|
|
106
|
-
assign(
|
|
107
|
-
current as unknown as Record<string | number, unknown>,
|
|
108
|
-
index,
|
|
109
|
-
value,
|
|
110
|
-
);
|
|
248
|
+
assign(container as Container, prop, value);
|
|
111
249
|
return true;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
250
|
+
},
|
|
251
|
+
deleteProperty: (_target, prop) => {
|
|
252
|
+
if (typeof prop === "symbol") return false;
|
|
253
|
+
const container = current();
|
|
254
|
+
if (Array.isArray(container)) {
|
|
255
|
+
if (!isIndex(prop) || Number(prop) >= container.length) return true;
|
|
256
|
+
const index = Number(prop);
|
|
257
|
+
(prepareForWrite(node) as unknown as unknown[]).splice(index, 1);
|
|
258
|
+
host.record({ op: "remove", path: [...node.path, index] }, node.path);
|
|
259
|
+
gen++;
|
|
260
|
+
return true;
|
|
261
|
+
}
|
|
262
|
+
if (!Object.hasOwn(container as object, prop)) return true;
|
|
263
|
+
if (isUnsafeKey(prop, node.path[node.path.length - 1])) {
|
|
264
|
+
throw invalid(
|
|
265
|
+
[...node.path, prop],
|
|
266
|
+
`cannot remove unsafe key "${prop}"`,
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
delete prepareForWrite(node)[prop];
|
|
270
|
+
host.record({ op: "remove", path: [...node.path, prop] }, [
|
|
271
|
+
...node.path,
|
|
272
|
+
prop,
|
|
273
|
+
]);
|
|
274
|
+
gen++;
|
|
122
275
|
return true;
|
|
276
|
+
},
|
|
277
|
+
has: (_target, prop) => Reflect.has(current() as object, prop),
|
|
278
|
+
...sharedDraftTraps(() => current() as object, "on a state draft"),
|
|
279
|
+
});
|
|
280
|
+
return node;
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
const root = createNode(null, "");
|
|
284
|
+
|
|
285
|
+
return {
|
|
286
|
+
proxy: root.proxy as T,
|
|
287
|
+
flush: () => {
|
|
288
|
+
if (dirty.length === 0) return host.snapshot();
|
|
289
|
+
for (const node of dirty) {
|
|
290
|
+
if (node.copy !== null) {
|
|
291
|
+
node.base = node.copy;
|
|
292
|
+
node.copy = null;
|
|
293
|
+
}
|
|
123
294
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
return true;
|
|
127
|
-
},
|
|
128
|
-
defineProperty: () => {
|
|
129
|
-
throw new Error(
|
|
130
|
-
"statewire: defineProperty is not supported on a state draft",
|
|
131
|
-
);
|
|
295
|
+
dirty.length = 0;
|
|
296
|
+
return root.base;
|
|
132
297
|
},
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
"statewire: setPrototypeOf is not supported on a state draft",
|
|
136
|
-
);
|
|
137
|
-
},
|
|
138
|
-
preventExtensions: () => {
|
|
139
|
-
throw new Error(
|
|
140
|
-
"statewire: preventExtensions is not supported on a state draft",
|
|
141
|
-
);
|
|
298
|
+
invalidate: () => {
|
|
299
|
+
gen++;
|
|
142
300
|
},
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
301
|
+
};
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Append `suffix` to the string at `parent[key]` on a statewire state draft,
|
|
306
|
+
* recording a single end-offset text-insert `add` op instead of a full
|
|
307
|
+
* `replace` — use it on token-streaming hot paths. Throws when the current
|
|
308
|
+
* value is not a string.
|
|
309
|
+
*/
|
|
310
|
+
export const append = <TKey extends string | number>(
|
|
311
|
+
parent: { readonly [P in TKey]: string },
|
|
312
|
+
key: TKey,
|
|
313
|
+
suffix: string,
|
|
314
|
+
): void => {
|
|
315
|
+
const handle = (parent as { [NODE]?: NodeHandle })[NODE];
|
|
316
|
+
if (handle === undefined) {
|
|
317
|
+
throw new Error("statewire: append expects a state draft object");
|
|
318
|
+
}
|
|
319
|
+
if (typeof suffix !== "string") {
|
|
320
|
+
throw new Error("statewire: append suffix must be a string");
|
|
321
|
+
}
|
|
322
|
+
handle.append(key, suffix);
|
|
155
323
|
};
|