-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular.ts
More file actions
196 lines (168 loc) · 6.45 KB
/
Copy pathangular.ts
File metadata and controls
196 lines (168 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { computed, signal, type Signal, type WritableSignal } from '@angular/core';
import type { JsonValue } from 'jsonpath-rfc9535';
import { Engine, NodeEngine, type DiffOp } from './engine.js';
// The store mutates engine.draft / engine.base in place and re-exposes the
// same references. Angular's default reference equality would skip propagation
// — so every read computed declares this equality to force propagation.
const neverEqual = () => false;
// Angular Signals adapter for the patchwork Engine.
//
// The store wraps an Engine (or NodeEngine via scope) and exposes reactive
// reads as Signals. All engine mutations are forwarded; after each, the
// internal "tick" signals are fired so any dependent computeds re-evaluate.
//
// Two pieces of state per store tree (root + scopes share them):
// _draftTick — bumped after every mutation that affects draft
// _baseTick — bumped after every accept (or undo/redo that moves base)
//
// The ticks are WritableSignal<unknown> with equal: () => false, so even
// re-setting the same reference fires the change. This avoids structuredClone
// on every mutation while keeping reactivity correct.
export interface PatchworkStore<T extends JsonValue = JsonValue> {
readonly draft: Signal<T>;
readonly base: Signal<T>;
readonly engine: Engine<T> | NodeEngine<T>;
get<U = JsonValue>(path: string): Signal<Array<{ path: string; value: U }>>;
getBase<U = JsonValue>(path: string): Signal<Array<{ path: string; value: U }>>;
getValue<U = JsonValue>(path: string): Signal<U>;
getValueBase<U = JsonValue>(path: string): Signal<U>;
diff(path?: string, options?: { key?: string; includeUnchanged?: boolean; cascade?: boolean }): Signal<DiffOp[]>;
add(path: string, value: any): void;
replace(path: string, value: any): void;
delete(path: string): void;
move(from: string, to: string): void;
copy(from: string, to: string): void;
revert(path: string): void;
restore(op: DiffOp): void;
undo(): void;
redo(): void;
accept(): void;
decline(): void;
beginEphemeral(): void;
commitEphemeral(): void;
discardEphemeral(): void;
scope<U extends JsonValue = JsonValue>(path: string): PatchworkStore<U>;
}
class PatchworkStoreImpl<T extends JsonValue> implements PatchworkStore<T> {
readonly engine: Engine<T> | NodeEngine<T>;
private readonly _draftTick: WritableSignal<unknown>;
private readonly _baseTick: WritableSignal<unknown>;
readonly draft: Signal<T>;
readonly base: Signal<T>;
constructor(
engine: Engine<JsonValue> | NodeEngine<JsonValue>,
sharedDraftTick?: WritableSignal<unknown>,
sharedBaseTick?: WritableSignal<unknown>,
) {
this.engine = engine as Engine<T> | NodeEngine<T>;
this._draftTick = sharedDraftTick ?? signal<unknown>(null, { equal: () => false });
this._baseTick = sharedBaseTick ?? signal<unknown>(null, { equal: () => false });
this.draft = computed(() => {
this._draftTick();
return this.engine.draft as T;
}, { equal: neverEqual });
this.base = computed(() => {
this._baseTick();
return this.engine.base as T;
}, { equal: neverEqual });
}
private fireDraft() { this._draftTick.set(null); }
private fireBase() { this._baseTick.set(null); }
get<U = JsonValue>(path: string): Signal<Array<{ path: string; value: U }>> {
return computed(() => {
this._draftTick();
return this.engine.get(path) as Array<{ path: string; value: U }>;
}, { equal: neverEqual });
}
getBase<U = JsonValue>(path: string): Signal<Array<{ path: string; value: U }>> {
return computed(() => {
this._baseTick();
return this.engine.getBase(path) as Array<{ path: string; value: U }>;
}, { equal: neverEqual });
}
getValue<U = JsonValue>(path: string): Signal<U> {
return computed(() => {
this._draftTick();
return this.engine.getValue(path) as U;
}, { equal: neverEqual });
}
getValueBase<U = JsonValue>(path: string): Signal<U> {
return computed(() => {
this._baseTick();
return this.engine.getValueBase(path) as U;
}, { equal: neverEqual });
}
diff(path?: string, options?: { key?: string; includeUnchanged?: boolean; cascade?: boolean }): Signal<DiffOp[]> {
return computed(() => {
this._draftTick();
this._baseTick();
return this.engine.diff(path, options);
}, { equal: neverEqual });
}
add(path: string, value: any): void { this.engine.add(path, value); this.fireDraft(); }
replace(path: string, value: any): void { this.engine.replace(path, value); this.fireDraft(); }
delete(path: string): void { this.engine.delete(path); this.fireDraft(); }
move(from: string, to: string): void { this.engine.move(from, to); this.fireDraft(); }
copy(from: string, to: string): void { this.engine.copy(from, to); this.fireDraft(); }
revert(path: string): void { this.engine.revert(path); this.fireDraft(); }
restore(op: DiffOp): void { this.engine.restore(op); this.fireDraft(); }
undo(): void {
this.engine.undo();
// Either side could have moved (accept undo touches base, etc.) — fire both.
this.fireDraft();
this.fireBase();
}
redo(): void {
this.engine.redo();
this.fireDraft();
this.fireBase();
}
accept(): void {
this.engine.accept();
this.fireBase();
}
decline(): void {
this.engine.decline();
this.fireDraft();
}
beginEphemeral(): void {
if (!('beginEphemeral' in this.engine)) {
throw new Error('beginEphemeral: not available on scoped stores (NodeEngine)');
}
(this.engine as Engine<T>).beginEphemeral();
}
commitEphemeral(): void {
if (!('commitEphemeral' in this.engine)) {
throw new Error('commitEphemeral: not available on scoped stores (NodeEngine)');
}
(this.engine as Engine<T>).commitEphemeral();
this.fireDraft();
}
discardEphemeral(): void {
if (!('discardEphemeral' in this.engine)) {
throw new Error('discardEphemeral: not available on scoped stores (NodeEngine)');
}
(this.engine as Engine<T>).discardEphemeral();
this.fireDraft();
}
scope<U extends JsonValue = JsonValue>(path: string): PatchworkStore<U> {
const child = this.engine.getNodeEngine<U>(path);
return new PatchworkStoreImpl<U>(
child as NodeEngine<JsonValue>,
this._draftTick,
this._baseTick,
);
}
}
export function createPatchworkStore<T extends JsonValue = JsonValue>(
base: T,
options?: { schema?: Record<string, any> },
): PatchworkStore<T> {
const engine = new Engine<T>(base, options);
return new PatchworkStoreImpl<T>(engine as Engine<JsonValue>);
}
export function fromEngine<T extends JsonValue = JsonValue>(
engine: Engine<T>,
): PatchworkStore<T> {
return new PatchworkStoreImpl<T>(engine as Engine<JsonValue>);
}