@katn30/trakr 1.0.0
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 +21 -0
- package/README.md +1208 -0
- package/dist/dev/index.cjs +1273 -0
- package/dist/dev/index.cjs.map +1 -0
- package/dist/dev/index.d.cts +369 -0
- package/dist/dev/index.d.ts +369 -0
- package/dist/dev/index.js +1239 -0
- package/dist/dev/index.js.map +1 -0
- package/dist/prod/index.cjs +1273 -0
- package/dist/prod/index.cjs.map +1 -0
- package/dist/prod/index.js +1239 -0
- package/dist/prod/index.js.map +1 -0
- package/package.json +55 -0
|
@@ -0,0 +1,1239 @@
|
|
|
1
|
+
// src/TypedEvent.ts
|
|
2
|
+
var TypedEvent = class {
|
|
3
|
+
constructor() {
|
|
4
|
+
this._handlers = [];
|
|
5
|
+
}
|
|
6
|
+
subscribe(handler) {
|
|
7
|
+
this._handlers.push(handler);
|
|
8
|
+
return () => this.unsubscribe(handler);
|
|
9
|
+
}
|
|
10
|
+
unsubscribe(handler) {
|
|
11
|
+
const index = this._handlers.indexOf(handler);
|
|
12
|
+
if (index >= 0) {
|
|
13
|
+
this._handlers.splice(index, 1);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
emit(event) {
|
|
17
|
+
this._handlers.forEach((h) => h(event));
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// src/Change.ts
|
|
22
|
+
var Change = class {
|
|
23
|
+
constructor(number, redoAction, undoAction, properties) {
|
|
24
|
+
this.number = number;
|
|
25
|
+
this.redoAction = redoAction;
|
|
26
|
+
this.undoAction = undoAction;
|
|
27
|
+
this.properties = properties;
|
|
28
|
+
this.time = /* @__PURE__ */ new Date();
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// src/Operation.ts
|
|
33
|
+
var Operation = class {
|
|
34
|
+
constructor() {
|
|
35
|
+
this.time = /* @__PURE__ */ new Date();
|
|
36
|
+
this.actions = [];
|
|
37
|
+
this._hasActions = false;
|
|
38
|
+
}
|
|
39
|
+
get hasActions() {
|
|
40
|
+
return this._hasActions;
|
|
41
|
+
}
|
|
42
|
+
set hasActions(value) {
|
|
43
|
+
this._hasActions = value;
|
|
44
|
+
}
|
|
45
|
+
add(redoAction, undoAction, properties) {
|
|
46
|
+
const action = new Change(
|
|
47
|
+
this.actions.length,
|
|
48
|
+
redoAction,
|
|
49
|
+
undoAction,
|
|
50
|
+
properties
|
|
51
|
+
);
|
|
52
|
+
this.actions.push(action);
|
|
53
|
+
this.hasActions = true;
|
|
54
|
+
}
|
|
55
|
+
updateOrAdd(redoAction, undoAction, properties) {
|
|
56
|
+
const idx = this.actions.findLastIndex(
|
|
57
|
+
(c) => c.properties.trackedObject === properties.trackedObject && c.properties.property === properties.property
|
|
58
|
+
);
|
|
59
|
+
if (idx >= 0) {
|
|
60
|
+
this.actions[idx] = new Change(this.actions[idx].number, redoAction, undoAction, properties);
|
|
61
|
+
} else {
|
|
62
|
+
this.add(redoAction, undoAction, properties);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
redo() {
|
|
66
|
+
this.actions.reverse().forEach((x) => x.redoAction());
|
|
67
|
+
}
|
|
68
|
+
undo() {
|
|
69
|
+
this.actions.reverse().forEach((x) => x.undoAction());
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// src/CollectionUtilities.ts
|
|
74
|
+
var CollectionUtilities = class {
|
|
75
|
+
static getLast(array) {
|
|
76
|
+
return array.length > 0 ? array[array.length - 1] : void 0;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// src/State.ts
|
|
81
|
+
var State = /* @__PURE__ */ ((State2) => {
|
|
82
|
+
State2["Unchanged"] = "Unchanged";
|
|
83
|
+
State2["Insert"] = "Insert";
|
|
84
|
+
State2["Changed"] = "Changed";
|
|
85
|
+
State2["Deleted"] = "Deleted";
|
|
86
|
+
return State2;
|
|
87
|
+
})(State || {});
|
|
88
|
+
|
|
89
|
+
// src/DependencyTracker.ts
|
|
90
|
+
var COLLECTION_VERSION_KEY = "__version__";
|
|
91
|
+
var collector = null;
|
|
92
|
+
var forwardDeps = /* @__PURE__ */ new WeakMap();
|
|
93
|
+
var reverseDeps = /* @__PURE__ */ new Map();
|
|
94
|
+
var DependencyTracker = {
|
|
95
|
+
isActive() {
|
|
96
|
+
return collector !== null;
|
|
97
|
+
},
|
|
98
|
+
record(object, property) {
|
|
99
|
+
if (!collector) return;
|
|
100
|
+
let props = collector.get(object);
|
|
101
|
+
if (!props) {
|
|
102
|
+
props = /* @__PURE__ */ new Set();
|
|
103
|
+
collector.set(object, props);
|
|
104
|
+
}
|
|
105
|
+
props.add(property);
|
|
106
|
+
},
|
|
107
|
+
collect(fn) {
|
|
108
|
+
collector = /* @__PURE__ */ new Map();
|
|
109
|
+
fn();
|
|
110
|
+
const deps = collector;
|
|
111
|
+
collector = null;
|
|
112
|
+
return deps;
|
|
113
|
+
},
|
|
114
|
+
updateDeps(validatedObj, validatorProp, newDeps) {
|
|
115
|
+
const objForward = forwardDeps.get(validatedObj);
|
|
116
|
+
const oldDeps = objForward?.get(validatorProp);
|
|
117
|
+
if (oldDeps) {
|
|
118
|
+
oldDeps.forEach((props, depObj) => {
|
|
119
|
+
const propMap = reverseDeps.get(depObj);
|
|
120
|
+
if (!propMap) return;
|
|
121
|
+
props.forEach((prop) => {
|
|
122
|
+
const list = propMap.get(prop);
|
|
123
|
+
if (!list) return;
|
|
124
|
+
const idx = list.findIndex(
|
|
125
|
+
(x) => x.obj === validatedObj && x.prop === validatorProp
|
|
126
|
+
);
|
|
127
|
+
if (idx >= 0) list.splice(idx, 1);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
let fwd = forwardDeps.get(validatedObj);
|
|
132
|
+
if (!fwd) {
|
|
133
|
+
fwd = /* @__PURE__ */ new Map();
|
|
134
|
+
forwardDeps.set(validatedObj, fwd);
|
|
135
|
+
}
|
|
136
|
+
fwd.set(validatorProp, newDeps);
|
|
137
|
+
newDeps.forEach((props, depObj) => {
|
|
138
|
+
let propMap = reverseDeps.get(depObj);
|
|
139
|
+
if (!propMap) {
|
|
140
|
+
propMap = /* @__PURE__ */ new Map();
|
|
141
|
+
reverseDeps.set(depObj, propMap);
|
|
142
|
+
}
|
|
143
|
+
props.forEach((prop) => {
|
|
144
|
+
let list = propMap.get(prop);
|
|
145
|
+
if (!list) {
|
|
146
|
+
list = [];
|
|
147
|
+
propMap.set(prop, list);
|
|
148
|
+
}
|
|
149
|
+
if (!list.some((x) => x.obj === validatedObj && x.prop === validatorProp)) {
|
|
150
|
+
list.push({ obj: validatedObj, prop: validatorProp });
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
},
|
|
155
|
+
getDependents(depObj, depProp) {
|
|
156
|
+
return reverseDeps.get(depObj)?.get(depProp) ?? [];
|
|
157
|
+
},
|
|
158
|
+
clearDeps(validatedObj) {
|
|
159
|
+
const objForward = forwardDeps.get(validatedObj);
|
|
160
|
+
if (!objForward) return;
|
|
161
|
+
objForward.forEach((depMap) => {
|
|
162
|
+
depMap.forEach((props, depObj) => {
|
|
163
|
+
const propMap = reverseDeps.get(depObj);
|
|
164
|
+
if (!propMap) return;
|
|
165
|
+
props.forEach((prop) => {
|
|
166
|
+
const list = propMap.get(prop);
|
|
167
|
+
if (!list) return;
|
|
168
|
+
const idx = list.findIndex((x) => x.obj === validatedObj);
|
|
169
|
+
if (idx >= 0) list.splice(idx, 1);
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
forwardDeps.delete(validatedObj);
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
// src/Registry.ts
|
|
178
|
+
var VALIDATORS = /* @__PURE__ */ Symbol("validators");
|
|
179
|
+
function registerPropertyValidator(proto, property, validator) {
|
|
180
|
+
if (!(VALIDATORS in proto)) {
|
|
181
|
+
Object.defineProperty(proto, VALIDATORS, {
|
|
182
|
+
value: /* @__PURE__ */ new Map(),
|
|
183
|
+
configurable: true
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
const map = proto[VALIDATORS];
|
|
187
|
+
if (!map.has(property)) {
|
|
188
|
+
map.set(property, validator);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function validate(tracked) {
|
|
192
|
+
const proto = Object.getPrototypeOf(tracked);
|
|
193
|
+
if (!(VALIDATORS in proto)) return;
|
|
194
|
+
const validators = proto[VALIDATORS];
|
|
195
|
+
const messages = /* @__PURE__ */ new Map();
|
|
196
|
+
validators.forEach((validatorFn, property) => {
|
|
197
|
+
const deps = DependencyTracker.collect(() => {
|
|
198
|
+
const error = validatorFn(tracked);
|
|
199
|
+
if (error !== void 0) messages.set(property, error);
|
|
200
|
+
});
|
|
201
|
+
DependencyTracker.updateDeps(tracked, property, deps);
|
|
202
|
+
});
|
|
203
|
+
tracked._applyValidation(messages);
|
|
204
|
+
}
|
|
205
|
+
function validateSingleProperty(tracked, property) {
|
|
206
|
+
const proto = Object.getPrototypeOf(tracked);
|
|
207
|
+
if (!(VALIDATORS in proto)) return void 0;
|
|
208
|
+
const validators = proto[VALIDATORS];
|
|
209
|
+
const validatorFn = validators.get(property);
|
|
210
|
+
if (!validatorFn) return void 0;
|
|
211
|
+
let error;
|
|
212
|
+
const deps = DependencyTracker.collect(() => {
|
|
213
|
+
error = validatorFn(tracked);
|
|
214
|
+
});
|
|
215
|
+
DependencyTracker.updateDeps(tracked, property, deps);
|
|
216
|
+
return error;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// src/Tracker.ts
|
|
220
|
+
var Tracker = class {
|
|
221
|
+
constructor() {
|
|
222
|
+
this._suppressTrackingCounter = 0;
|
|
223
|
+
this._placeholderCounter = -1;
|
|
224
|
+
this._invalidCount = 0;
|
|
225
|
+
this._constructionDepth = 0;
|
|
226
|
+
this._version = 0;
|
|
227
|
+
this._isReplaying = false;
|
|
228
|
+
this.trackedObjects = [];
|
|
229
|
+
this.trackedCollections = [];
|
|
230
|
+
this.isDirtyChanged = new TypedEvent();
|
|
231
|
+
this.versionChanged = new TypedEvent();
|
|
232
|
+
this.isValidChanged = new TypedEvent();
|
|
233
|
+
this.canCommitChanged = new TypedEvent();
|
|
234
|
+
this._currentOperation = void 0;
|
|
235
|
+
this._redoOperations = [];
|
|
236
|
+
this._undoOperations = [];
|
|
237
|
+
this._commitStateOperation = void 0;
|
|
238
|
+
this._isDirty = false;
|
|
239
|
+
this._canUndo = false;
|
|
240
|
+
this._canRedo = false;
|
|
241
|
+
this._suppressTrackingCounter = 0;
|
|
242
|
+
this._currentOperationOwner = void 0;
|
|
243
|
+
this._currentOperationPropertyName = void 0;
|
|
244
|
+
this._isValid = true;
|
|
245
|
+
this._canCommit = false;
|
|
246
|
+
}
|
|
247
|
+
get deletedObjects() {
|
|
248
|
+
return this.trackedObjects.filter((obj) => obj.state === "Deleted" /* Deleted */);
|
|
249
|
+
}
|
|
250
|
+
get isDirty() {
|
|
251
|
+
return this._isDirty;
|
|
252
|
+
}
|
|
253
|
+
set isDirty(value) {
|
|
254
|
+
if (this._isDirty !== value) {
|
|
255
|
+
this._isDirty = value;
|
|
256
|
+
this.isDirtyChanged.emit(value);
|
|
257
|
+
this.updateCanCommit();
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
get version() {
|
|
261
|
+
return this._version;
|
|
262
|
+
}
|
|
263
|
+
get isValid() {
|
|
264
|
+
return this._isValid;
|
|
265
|
+
}
|
|
266
|
+
set isValid(value) {
|
|
267
|
+
if (this._isValid !== value) {
|
|
268
|
+
this._isValid = value;
|
|
269
|
+
this.isValidChanged.emit(value);
|
|
270
|
+
this.updateCanCommit();
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
get canCommit() {
|
|
274
|
+
return this._canCommit;
|
|
275
|
+
}
|
|
276
|
+
set canCommit(value) {
|
|
277
|
+
if (this._canCommit !== value) {
|
|
278
|
+
this._canCommit = value;
|
|
279
|
+
this.canCommitChanged.emit(value);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
updateCanCommit() {
|
|
283
|
+
this.canCommit = this._isDirty && this._isValid;
|
|
284
|
+
}
|
|
285
|
+
get canUndo() {
|
|
286
|
+
return this._canUndo;
|
|
287
|
+
}
|
|
288
|
+
set canUndo(value) {
|
|
289
|
+
this._canUndo = value;
|
|
290
|
+
}
|
|
291
|
+
get canRedo() {
|
|
292
|
+
return this._canRedo;
|
|
293
|
+
}
|
|
294
|
+
set canRedo(value) {
|
|
295
|
+
this._canRedo = value;
|
|
296
|
+
}
|
|
297
|
+
/** @internal */
|
|
298
|
+
get _isTrackingSuppressed() {
|
|
299
|
+
return this._suppressTrackingCounter > 0;
|
|
300
|
+
}
|
|
301
|
+
/** @internal */
|
|
302
|
+
get _isConstructing() {
|
|
303
|
+
return this._constructionDepth > 0;
|
|
304
|
+
}
|
|
305
|
+
/** @internal */
|
|
306
|
+
_trackObject(trackedObject) {
|
|
307
|
+
this.trackedObjects.push(trackedObject);
|
|
308
|
+
}
|
|
309
|
+
/** @internal */
|
|
310
|
+
_untrackObject(trackedObject) {
|
|
311
|
+
this.trackedObjects.splice(this.trackedObjects.indexOf(trackedObject), 1);
|
|
312
|
+
if (!trackedObject.isValid) this._invalidCount--;
|
|
313
|
+
this.isValid = this._invalidCount === 0;
|
|
314
|
+
}
|
|
315
|
+
/** @internal */
|
|
316
|
+
_trackCollection(trackedCollection) {
|
|
317
|
+
this.trackedCollections.push(trackedCollection);
|
|
318
|
+
}
|
|
319
|
+
/** @internal */
|
|
320
|
+
_untrackCollection(trackedCollection) {
|
|
321
|
+
this.trackedCollections.splice(
|
|
322
|
+
this.trackedCollections.indexOf(trackedCollection),
|
|
323
|
+
1
|
|
324
|
+
);
|
|
325
|
+
if (!trackedCollection.isValid) this._invalidCount--;
|
|
326
|
+
this.isValid = this._invalidCount === 0;
|
|
327
|
+
}
|
|
328
|
+
/** @internal */
|
|
329
|
+
_onValidityChanged(wasValid, isNowValid) {
|
|
330
|
+
if (wasValid && !isNowValid) this._invalidCount++;
|
|
331
|
+
else if (!wasValid && isNowValid) this._invalidCount--;
|
|
332
|
+
if (!this._isTrackingSuppressed) {
|
|
333
|
+
this.isValid = this._invalidCount === 0;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
construct(action) {
|
|
337
|
+
const objectsBefore = this.trackedObjects.length;
|
|
338
|
+
this._constructionDepth++;
|
|
339
|
+
this._suppressTrackingCounter++;
|
|
340
|
+
const result = action();
|
|
341
|
+
for (let i = objectsBefore; i < this.trackedObjects.length; i++) {
|
|
342
|
+
validate(this.trackedObjects[i]);
|
|
343
|
+
}
|
|
344
|
+
this._suppressTrackingCounter--;
|
|
345
|
+
this._constructionDepth--;
|
|
346
|
+
this.isValid = this._invalidCount === 0;
|
|
347
|
+
return result;
|
|
348
|
+
}
|
|
349
|
+
withTrackingSuppressed(action) {
|
|
350
|
+
this._suppressTrackingCounter++;
|
|
351
|
+
action();
|
|
352
|
+
this._suppressTrackingCounter--;
|
|
353
|
+
}
|
|
354
|
+
beginSuppressTracking() {
|
|
355
|
+
this._suppressTrackingCounter++;
|
|
356
|
+
}
|
|
357
|
+
endSuppressTracking() {
|
|
358
|
+
this._suppressTrackingCounter--;
|
|
359
|
+
}
|
|
360
|
+
/** @internal */
|
|
361
|
+
_doAndTrack(redoAction, undoAction, properties) {
|
|
362
|
+
if (this._isReplaying) return;
|
|
363
|
+
if (this._isTrackingSuppressed) {
|
|
364
|
+
redoAction();
|
|
365
|
+
if (!this._isConstructing) {
|
|
366
|
+
this.revalidateTargeted(properties.trackedObject, properties.property);
|
|
367
|
+
}
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
if (this.isStartingNewOperation()) {
|
|
371
|
+
this._currentOperationOwner = properties.trackedObject;
|
|
372
|
+
this._currentOperationPropertyName = properties.property;
|
|
373
|
+
if (this.shouldCoalesceChanges(properties)) {
|
|
374
|
+
this._currentOperation = CollectionUtilities.getLast(this._undoOperations);
|
|
375
|
+
this.versionChanged.emit(this._version);
|
|
376
|
+
} else {
|
|
377
|
+
this._currentOperation = new Operation();
|
|
378
|
+
this._undoOperations.push(this._currentOperation);
|
|
379
|
+
this._redoOperations.length = 0;
|
|
380
|
+
this.reset();
|
|
381
|
+
this._version++;
|
|
382
|
+
this.versionChanged.emit(this._version);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
this._currentOperation?.add(
|
|
386
|
+
() => redoAction(),
|
|
387
|
+
() => undoAction(),
|
|
388
|
+
properties
|
|
389
|
+
);
|
|
390
|
+
redoAction();
|
|
391
|
+
if (this.isEndingCurrentOperation(properties)) {
|
|
392
|
+
this._currentOperation = void 0;
|
|
393
|
+
this._currentOperationOwner = void 0;
|
|
394
|
+
this._currentOperationPropertyName = void 0;
|
|
395
|
+
this.revalidateTargeted(properties.trackedObject, properties.property);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
revalidateTargeted(changedObj, changedProp) {
|
|
399
|
+
const depKey = changedProp ?? COLLECTION_VERSION_KEY;
|
|
400
|
+
const dependents = [...DependencyTracker.getDependents(changedObj, depKey)];
|
|
401
|
+
for (const { obj, prop } of dependents) {
|
|
402
|
+
const error = validateSingleProperty(obj, prop);
|
|
403
|
+
obj._validate(prop, error);
|
|
404
|
+
}
|
|
405
|
+
if (changedProp === void 0) {
|
|
406
|
+
changedObj._validate();
|
|
407
|
+
}
|
|
408
|
+
this.isValid = this._invalidCount === 0;
|
|
409
|
+
}
|
|
410
|
+
isEndingCurrentOperation(properties) {
|
|
411
|
+
return this._currentOperationOwner === properties.trackedObject && this._currentOperationPropertyName === properties.property;
|
|
412
|
+
}
|
|
413
|
+
isStartingNewOperation() {
|
|
414
|
+
return this._currentOperationOwner === void 0 && this._currentOperationPropertyName === void 0;
|
|
415
|
+
}
|
|
416
|
+
shouldCoalesceChanges(properties) {
|
|
417
|
+
const lastOperation = CollectionUtilities.getLast(this._undoOperations);
|
|
418
|
+
return this.isCoalescibleType(properties) && this.hasLastOperation(lastOperation) && this.lastOperationTargetsSameProperty(lastOperation, properties) && this.lastActionIsRecent(lastOperation, properties.coalesceWithin);
|
|
419
|
+
}
|
|
420
|
+
isCoalescibleType(properties) {
|
|
421
|
+
return properties.coalesceWithin !== void 0 && (properties.type === 1 /* String */ || properties.type === 2 /* Number */);
|
|
422
|
+
}
|
|
423
|
+
hasLastOperation(lastOperation) {
|
|
424
|
+
return !!lastOperation;
|
|
425
|
+
}
|
|
426
|
+
lastOperationTargetsSameProperty(lastOperation, properties) {
|
|
427
|
+
return lastOperation.actions.every(
|
|
428
|
+
(x) => x.properties.trackedObject === properties.trackedObject && x.properties.property === properties.property
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
lastActionIsRecent(lastOperation, coalesceWithin) {
|
|
432
|
+
return (/* @__PURE__ */ new Date()).getTime() - CollectionUtilities.getLast(lastOperation.actions).time.getTime() < coalesceWithin;
|
|
433
|
+
}
|
|
434
|
+
/** @internal */
|
|
435
|
+
_nextPlaceholder() {
|
|
436
|
+
return this._placeholderCounter--;
|
|
437
|
+
}
|
|
438
|
+
onCommit(keys) {
|
|
439
|
+
const lastOp = CollectionUtilities.getLast(this._undoOperations);
|
|
440
|
+
this.trackedObjects.forEach((obj) => obj._onCommitted(lastOp, keys));
|
|
441
|
+
this._commitStateOperation = lastOp;
|
|
442
|
+
this.reset();
|
|
443
|
+
}
|
|
444
|
+
/** @internal */
|
|
445
|
+
_isInUndoStack(op) {
|
|
446
|
+
return this._undoOperations.includes(op);
|
|
447
|
+
}
|
|
448
|
+
reset() {
|
|
449
|
+
this.canUndo = this._undoOperations.length > 0;
|
|
450
|
+
this.canRedo = this._redoOperations.length > 0;
|
|
451
|
+
this.isDirty = CollectionUtilities.getLast(this._undoOperations) !== this._commitStateOperation;
|
|
452
|
+
}
|
|
453
|
+
startComposing() {
|
|
454
|
+
if (this._composingBaseIndex !== void 0) return;
|
|
455
|
+
this._composingBaseIndex = this._undoOperations.length;
|
|
456
|
+
this._composingRedoLength = this._redoOperations.length;
|
|
457
|
+
}
|
|
458
|
+
endComposing() {
|
|
459
|
+
if (this._composingBaseIndex === void 0) return;
|
|
460
|
+
const composed = this._undoOperations.splice(this._composingBaseIndex);
|
|
461
|
+
this._redoOperations.splice(this._composingRedoLength);
|
|
462
|
+
this._composingBaseIndex = void 0;
|
|
463
|
+
this._composingRedoLength = void 0;
|
|
464
|
+
if (composed.length === 0) {
|
|
465
|
+
this.reset();
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
if (composed.length === 1) {
|
|
469
|
+
this._undoOperations.push(composed[0]);
|
|
470
|
+
this.reset();
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
const merged = new Operation();
|
|
474
|
+
for (const op of composed) {
|
|
475
|
+
for (const action of op.actions) {
|
|
476
|
+
merged.add(action.redoAction, action.undoAction, action.properties);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
this._undoOperations.push(merged);
|
|
480
|
+
this.reset();
|
|
481
|
+
}
|
|
482
|
+
rollbackComposing() {
|
|
483
|
+
if (this._composingBaseIndex === void 0) return;
|
|
484
|
+
const toRevert = this._undoOperations.splice(this._composingBaseIndex);
|
|
485
|
+
this._redoOperations.splice(this._composingRedoLength);
|
|
486
|
+
this._composingBaseIndex = void 0;
|
|
487
|
+
this._composingRedoLength = void 0;
|
|
488
|
+
this.withTrackingSuppressed(() => {
|
|
489
|
+
for (let i = toRevert.length - 1; i >= 0; i--) {
|
|
490
|
+
toRevert[i].undo();
|
|
491
|
+
}
|
|
492
|
+
});
|
|
493
|
+
this.reset();
|
|
494
|
+
this.revalidate();
|
|
495
|
+
if (toRevert.length > 0) {
|
|
496
|
+
this._version -= toRevert.length;
|
|
497
|
+
this.versionChanged.emit(this._version);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
undo() {
|
|
501
|
+
if (!this.canUndo) {
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
const undoOperation = this._undoOperations.pop();
|
|
505
|
+
this._isReplaying = true;
|
|
506
|
+
this.withTrackingSuppressed(() => undoOperation.undo());
|
|
507
|
+
this._isReplaying = false;
|
|
508
|
+
this._redoOperations.push(undoOperation);
|
|
509
|
+
this.reset();
|
|
510
|
+
this.revalidate();
|
|
511
|
+
this._version--;
|
|
512
|
+
this.versionChanged.emit(this._version);
|
|
513
|
+
}
|
|
514
|
+
redo() {
|
|
515
|
+
if (!this.canRedo) {
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
const redoOperation = this._redoOperations.pop();
|
|
519
|
+
this._isReplaying = true;
|
|
520
|
+
this.withTrackingSuppressed(() => redoOperation.redo());
|
|
521
|
+
this._isReplaying = false;
|
|
522
|
+
this._undoOperations.push(redoOperation);
|
|
523
|
+
this.reset();
|
|
524
|
+
this.revalidate();
|
|
525
|
+
this._version++;
|
|
526
|
+
this.versionChanged.emit(this._version);
|
|
527
|
+
}
|
|
528
|
+
revalidate() {
|
|
529
|
+
this.trackedObjects.forEach((x) => validate(x));
|
|
530
|
+
this.trackedCollections.forEach((x) => x._validate());
|
|
531
|
+
this.isValid = this._invalidCount === 0;
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
|
|
535
|
+
// src/OperationProperties.ts
|
|
536
|
+
var OperationProperties = class {
|
|
537
|
+
constructor(trackedObject, property, type, validator, coalesceWithin) {
|
|
538
|
+
this.trackedObject = trackedObject;
|
|
539
|
+
this.property = property;
|
|
540
|
+
this.type = type;
|
|
541
|
+
this.validator = validator;
|
|
542
|
+
this.coalesceWithin = coalesceWithin;
|
|
543
|
+
}
|
|
544
|
+
};
|
|
545
|
+
|
|
546
|
+
// src/ExternallyAssigned.ts
|
|
547
|
+
var AUTO_ID = /* @__PURE__ */ Symbol("autoId");
|
|
548
|
+
function AutoId(_target, context) {
|
|
549
|
+
context.addInitializer(function() {
|
|
550
|
+
Object.defineProperty(Object.getPrototypeOf(this), AUTO_ID, {
|
|
551
|
+
value: String(context.name),
|
|
552
|
+
configurable: true
|
|
553
|
+
});
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
function getAutoIdProperty(proto) {
|
|
557
|
+
return AUTO_ID in proto ? proto[AUTO_ID] : void 0;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
// src/TrackedObjectStateMachine.ts
|
|
561
|
+
function applyStateTransition(obj, event, direction, context) {
|
|
562
|
+
if (event === "added") {
|
|
563
|
+
applyAdded(obj, direction);
|
|
564
|
+
} else if (event === "removed") {
|
|
565
|
+
applyRemoved(obj, direction, context);
|
|
566
|
+
} else {
|
|
567
|
+
applyCommitted(obj, direction, context);
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
function applyAdded(obj, direction) {
|
|
571
|
+
if (direction === "undo") {
|
|
572
|
+
obj._setState("Unchanged" /* Unchanged */);
|
|
573
|
+
obj.idPlaceholder = null;
|
|
574
|
+
} else {
|
|
575
|
+
obj._setState("Insert" /* Insert */);
|
|
576
|
+
obj.idPlaceholder = obj.tracker._nextPlaceholder();
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
function applyRemoved(obj, direction, context) {
|
|
580
|
+
if (direction === "undo") {
|
|
581
|
+
const prev = context?.prevState ?? "Unchanged" /* Unchanged */;
|
|
582
|
+
obj._setState(prev);
|
|
583
|
+
if (prev === "Insert" /* Insert */) {
|
|
584
|
+
obj.idPlaceholder = obj.tracker._nextPlaceholder();
|
|
585
|
+
if (context?.prevDirtyCounter !== void 0) {
|
|
586
|
+
obj._setDirtyCounter(context.prevDirtyCounter);
|
|
587
|
+
}
|
|
588
|
+
} else {
|
|
589
|
+
obj.idPlaceholder = null;
|
|
590
|
+
}
|
|
591
|
+
} else {
|
|
592
|
+
if (obj.state === "Insert" /* Insert */) {
|
|
593
|
+
obj._setState("Unchanged" /* Unchanged */);
|
|
594
|
+
obj.idPlaceholder = null;
|
|
595
|
+
obj._setDirtyCounter(0);
|
|
596
|
+
} else {
|
|
597
|
+
obj._setState("Deleted" /* Deleted */);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
function applyCommitted(obj, direction, context) {
|
|
602
|
+
if (direction === "undo") {
|
|
603
|
+
const prev = context?.prevState ?? "Unchanged" /* Unchanged */;
|
|
604
|
+
if (prev === "Insert" /* Insert */) {
|
|
605
|
+
obj._setState("Deleted" /* Deleted */);
|
|
606
|
+
obj.idPlaceholder = null;
|
|
607
|
+
} else if (prev === "Deleted" /* Deleted */) {
|
|
608
|
+
obj._setState("Insert" /* Insert */);
|
|
609
|
+
obj.idPlaceholder = obj.tracker._nextPlaceholder();
|
|
610
|
+
} else if (prev === "Changed" /* Changed */) {
|
|
611
|
+
obj._setState("Changed" /* Changed */);
|
|
612
|
+
}
|
|
613
|
+
} else {
|
|
614
|
+
if (context?.prevState === "Insert" /* Insert */ && context.autoIdProp && context.realId !== void 0) {
|
|
615
|
+
obj[context.autoIdProp] = context.realId;
|
|
616
|
+
}
|
|
617
|
+
obj.idPlaceholder = null;
|
|
618
|
+
obj._setState("Unchanged" /* Unchanged */);
|
|
619
|
+
obj._setDirtyCounter(0);
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
function buildCommittedContext(obj, autoIdProp, keys) {
|
|
623
|
+
const prevState = obj.state;
|
|
624
|
+
let realId;
|
|
625
|
+
if (prevState === "Insert" /* Insert */ && obj.idPlaceholder !== null && keys) {
|
|
626
|
+
realId = keys.find((k) => k.placeholder === obj.idPlaceholder)?.value;
|
|
627
|
+
}
|
|
628
|
+
return { prevState, autoIdProp, realId };
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// src/TrackedObject.ts
|
|
632
|
+
var TrackedObject = class {
|
|
633
|
+
constructor(tracker) {
|
|
634
|
+
this.tracker = tracker;
|
|
635
|
+
this._dirtyCounter = 0;
|
|
636
|
+
this._isValid = true;
|
|
637
|
+
this._state = "Unchanged" /* Unchanged */;
|
|
638
|
+
this.idPlaceholder = null;
|
|
639
|
+
this.changed = new TypedEvent();
|
|
640
|
+
this.trackedChanged = new TypedEvent();
|
|
641
|
+
if (false) {
|
|
642
|
+
throw new Error(`${this.constructor.name} must be created inside tracker.construct()`);
|
|
643
|
+
}
|
|
644
|
+
this.validationMessages = /* @__PURE__ */ new Map();
|
|
645
|
+
tracker._trackObject(this);
|
|
646
|
+
}
|
|
647
|
+
// ---- StateTarget interface (internal) ----
|
|
648
|
+
/** @internal */
|
|
649
|
+
get state() {
|
|
650
|
+
return this._state;
|
|
651
|
+
}
|
|
652
|
+
/** @internal */
|
|
653
|
+
_setState(value) {
|
|
654
|
+
this._state = value;
|
|
655
|
+
}
|
|
656
|
+
/** @internal */
|
|
657
|
+
_getDirtyCounter() {
|
|
658
|
+
return this._dirtyCounter;
|
|
659
|
+
}
|
|
660
|
+
/** @internal */
|
|
661
|
+
_setDirtyCounter(value) {
|
|
662
|
+
this._dirtyCounter = value;
|
|
663
|
+
}
|
|
664
|
+
// ---- Public API ----
|
|
665
|
+
get validationMessages() {
|
|
666
|
+
return this._validationMessages ?? /* @__PURE__ */ new Map();
|
|
667
|
+
}
|
|
668
|
+
set validationMessages(value) {
|
|
669
|
+
this._validationMessages = value;
|
|
670
|
+
}
|
|
671
|
+
get isValid() {
|
|
672
|
+
return this._isValid;
|
|
673
|
+
}
|
|
674
|
+
set isValid(value) {
|
|
675
|
+
const wasValid = this._isValid;
|
|
676
|
+
this._isValid = value;
|
|
677
|
+
if (wasValid !== value) {
|
|
678
|
+
this.tracker._onValidityChanged(wasValid, value);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
get isDirty() {
|
|
682
|
+
return this._dirtyCounter !== 0;
|
|
683
|
+
}
|
|
684
|
+
get dirtyCounter() {
|
|
685
|
+
return this._dirtyCounter;
|
|
686
|
+
}
|
|
687
|
+
set dirtyCounter(value) {
|
|
688
|
+
this._dirtyCounter = value;
|
|
689
|
+
}
|
|
690
|
+
/** @internal */
|
|
691
|
+
_onCommitted(lastOp, keys) {
|
|
692
|
+
const autoIdProp = getAutoIdProperty(Object.getPrototypeOf(this));
|
|
693
|
+
const context = buildCommittedContext(this, autoIdProp, keys);
|
|
694
|
+
const redoFn = () => applyStateTransition(this, "committed", "do", context);
|
|
695
|
+
const undoFn = () => applyStateTransition(this, "committed", "undo", context);
|
|
696
|
+
if (lastOp) {
|
|
697
|
+
lastOp.updateOrAdd(redoFn, undoFn, new OperationProperties(this, "__state__", 4 /* Object */));
|
|
698
|
+
}
|
|
699
|
+
redoFn();
|
|
700
|
+
}
|
|
701
|
+
/** @internal */
|
|
702
|
+
_markRemoved() {
|
|
703
|
+
const prevState = this._state;
|
|
704
|
+
const prevDirtyCounter = this._dirtyCounter;
|
|
705
|
+
this.tracker._doAndTrack(
|
|
706
|
+
() => applyStateTransition(this, "removed", "do"),
|
|
707
|
+
() => applyStateTransition(this, "removed", "undo", { prevState, prevDirtyCounter }),
|
|
708
|
+
new OperationProperties(this, "__state__", 4 /* Object */)
|
|
709
|
+
);
|
|
710
|
+
}
|
|
711
|
+
/** @internal */
|
|
712
|
+
_markAdded() {
|
|
713
|
+
if (this._state !== "Unchanged" /* Unchanged */) return;
|
|
714
|
+
if (this.tracker._isTrackingSuppressed) return;
|
|
715
|
+
this.tracker._doAndTrack(
|
|
716
|
+
() => applyStateTransition(this, "added", "do"),
|
|
717
|
+
() => applyStateTransition(this, "added", "undo"),
|
|
718
|
+
new OperationProperties(this, "__state__", 4 /* Object */)
|
|
719
|
+
);
|
|
720
|
+
}
|
|
721
|
+
/** @internal */
|
|
722
|
+
_validate(property, errorMessage) {
|
|
723
|
+
if (errorMessage) {
|
|
724
|
+
this.validationMessages.set(property, errorMessage);
|
|
725
|
+
} else {
|
|
726
|
+
this.validationMessages.delete(property);
|
|
727
|
+
}
|
|
728
|
+
this.validationMessages = new Map(this.validationMessages);
|
|
729
|
+
this.isValid = this.validationMessages.size === 0;
|
|
730
|
+
}
|
|
731
|
+
/** @internal */
|
|
732
|
+
_applyValidation(messages) {
|
|
733
|
+
this.validationMessages = messages;
|
|
734
|
+
this.isValid = messages.size === 0;
|
|
735
|
+
}
|
|
736
|
+
destroy() {
|
|
737
|
+
DependencyTracker.clearDeps(this);
|
|
738
|
+
this.tracker._untrackObject(this);
|
|
739
|
+
}
|
|
740
|
+
};
|
|
741
|
+
|
|
742
|
+
// src/Tracked.ts
|
|
743
|
+
function Tracked(validator, onChange, options) {
|
|
744
|
+
function decorator(target, context) {
|
|
745
|
+
const propertyName = String(context.name);
|
|
746
|
+
if (context.kind === "getter") {
|
|
747
|
+
const getterFn = target;
|
|
748
|
+
return function() {
|
|
749
|
+
DependencyTracker.record(this, propertyName);
|
|
750
|
+
return getterFn.call(this);
|
|
751
|
+
};
|
|
752
|
+
}
|
|
753
|
+
if (context.kind === "accessor") {
|
|
754
|
+
const accessorTarget = target;
|
|
755
|
+
if (validator) {
|
|
756
|
+
context.addInitializer(function() {
|
|
757
|
+
registerPropertyValidator(
|
|
758
|
+
Object.getPrototypeOf(this),
|
|
759
|
+
propertyName,
|
|
760
|
+
(model) => validator(model, model[propertyName])
|
|
761
|
+
);
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
return {
|
|
765
|
+
get() {
|
|
766
|
+
DependencyTracker.record(this, propertyName);
|
|
767
|
+
return accessorTarget.get.call(this);
|
|
768
|
+
},
|
|
769
|
+
set(newValue) {
|
|
770
|
+
const oldValue = accessorTarget.get.call(this);
|
|
771
|
+
if (isSameValue(oldValue, newValue)) {
|
|
772
|
+
return;
|
|
773
|
+
}
|
|
774
|
+
if (!this.tracker || this.tracker._isTrackingSuppressed) {
|
|
775
|
+
accessorTarget.set.call(this, newValue);
|
|
776
|
+
return;
|
|
777
|
+
}
|
|
778
|
+
const properties = new OperationProperties(
|
|
779
|
+
this,
|
|
780
|
+
propertyName,
|
|
781
|
+
getPropertyType(newValue, oldValue),
|
|
782
|
+
validator ? (model, v) => validator(model, v) : void 0,
|
|
783
|
+
options?.coalesceWithin
|
|
784
|
+
);
|
|
785
|
+
this.tracker._doAndTrack(
|
|
786
|
+
() => {
|
|
787
|
+
accessorTarget.set.call(this, newValue);
|
|
788
|
+
const tracked = this;
|
|
789
|
+
tracked.dirtyCounter++;
|
|
790
|
+
if (tracked.state === "Unchanged" /* Unchanged */) tracked._setState("Changed" /* Changed */);
|
|
791
|
+
if (oldValue instanceof TrackedObject) oldValue._markRemoved();
|
|
792
|
+
if (newValue instanceof TrackedObject) newValue._markAdded();
|
|
793
|
+
if (!this.tracker._isReplaying && onChange) {
|
|
794
|
+
onChange(this, newValue, oldValue);
|
|
795
|
+
}
|
|
796
|
+
this.changed.emit({ property: propertyName, oldValue, newValue });
|
|
797
|
+
if (!this.tracker._isReplaying) {
|
|
798
|
+
this.trackedChanged.emit({ property: propertyName, oldValue, newValue });
|
|
799
|
+
}
|
|
800
|
+
},
|
|
801
|
+
() => {
|
|
802
|
+
accessorTarget.set.call(this, oldValue);
|
|
803
|
+
const tracked = this;
|
|
804
|
+
tracked.dirtyCounter--;
|
|
805
|
+
if (tracked.dirtyCounter === 0 && tracked.state === "Changed" /* Changed */) tracked._setState("Unchanged" /* Unchanged */);
|
|
806
|
+
this.changed.emit({ property: propertyName, oldValue: newValue, newValue: oldValue });
|
|
807
|
+
},
|
|
808
|
+
properties
|
|
809
|
+
);
|
|
810
|
+
}
|
|
811
|
+
};
|
|
812
|
+
} else {
|
|
813
|
+
const setterFn = target;
|
|
814
|
+
if (validator) {
|
|
815
|
+
context.addInitializer(function() {
|
|
816
|
+
registerPropertyValidator(
|
|
817
|
+
Object.getPrototypeOf(this),
|
|
818
|
+
propertyName,
|
|
819
|
+
(model) => validator(model, model[propertyName])
|
|
820
|
+
);
|
|
821
|
+
});
|
|
822
|
+
}
|
|
823
|
+
return function(newValue) {
|
|
824
|
+
const oldValue = this[propertyName];
|
|
825
|
+
if (isSameValue(oldValue, newValue)) {
|
|
826
|
+
return;
|
|
827
|
+
}
|
|
828
|
+
if (!this.tracker || this.tracker._isTrackingSuppressed) {
|
|
829
|
+
setterFn.call(this, newValue);
|
|
830
|
+
return;
|
|
831
|
+
}
|
|
832
|
+
const properties = new OperationProperties(
|
|
833
|
+
this,
|
|
834
|
+
propertyName,
|
|
835
|
+
getPropertyType(newValue, oldValue),
|
|
836
|
+
validator ? (model, v) => validator(model, v) : void 0,
|
|
837
|
+
options?.coalesceWithin
|
|
838
|
+
);
|
|
839
|
+
this.tracker._doAndTrack(
|
|
840
|
+
() => {
|
|
841
|
+
setterFn.call(this, newValue);
|
|
842
|
+
const tracked = this;
|
|
843
|
+
tracked.dirtyCounter++;
|
|
844
|
+
if (tracked.state === "Unchanged" /* Unchanged */) tracked._setState("Changed" /* Changed */);
|
|
845
|
+
if (oldValue instanceof TrackedObject) oldValue._markRemoved();
|
|
846
|
+
if (newValue instanceof TrackedObject) newValue._markAdded();
|
|
847
|
+
this.changed.emit({ property: propertyName, oldValue, newValue });
|
|
848
|
+
if (!this.tracker._isReplaying) {
|
|
849
|
+
this.trackedChanged.emit({ property: propertyName, oldValue, newValue });
|
|
850
|
+
}
|
|
851
|
+
},
|
|
852
|
+
() => {
|
|
853
|
+
setterFn.call(this, oldValue);
|
|
854
|
+
const tracked = this;
|
|
855
|
+
tracked.dirtyCounter--;
|
|
856
|
+
if (tracked.dirtyCounter === 0 && tracked.state === "Changed" /* Changed */) tracked._setState("Unchanged" /* Unchanged */);
|
|
857
|
+
this.changed.emit({ property: propertyName, oldValue: newValue, newValue: oldValue });
|
|
858
|
+
},
|
|
859
|
+
properties
|
|
860
|
+
);
|
|
861
|
+
};
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
return decorator;
|
|
865
|
+
}
|
|
866
|
+
function isSameValue(value1, value2) {
|
|
867
|
+
return value1 === value2;
|
|
868
|
+
}
|
|
869
|
+
function getPropertyType(newValue, oldValue) {
|
|
870
|
+
const v = newValue ?? oldValue;
|
|
871
|
+
if (v instanceof Date) return 5 /* Date */;
|
|
872
|
+
switch (typeof v) {
|
|
873
|
+
case "string":
|
|
874
|
+
return 1 /* String */;
|
|
875
|
+
case "boolean":
|
|
876
|
+
return 0 /* Boolean */;
|
|
877
|
+
case "number":
|
|
878
|
+
return 2 /* Number */;
|
|
879
|
+
case "object":
|
|
880
|
+
return 4 /* Object */;
|
|
881
|
+
default:
|
|
882
|
+
throw new Error(`Property type '${typeof v}' not supported`);
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
// src/TrackedCollection.ts
|
|
887
|
+
var TrackedCollection = class {
|
|
888
|
+
constructor(tracker, items, _validator) {
|
|
889
|
+
this.tracker = tracker;
|
|
890
|
+
this._validator = _validator;
|
|
891
|
+
this._dirtyCounter = 0;
|
|
892
|
+
this.changed = new TypedEvent();
|
|
893
|
+
this.trackedChanged = new TypedEvent();
|
|
894
|
+
/** @internal */
|
|
895
|
+
this.state = "Unchanged" /* Unchanged */;
|
|
896
|
+
this._isValid = true;
|
|
897
|
+
this._isDirty = false;
|
|
898
|
+
this._collection = items ? [...items] : [];
|
|
899
|
+
this._validate();
|
|
900
|
+
this.tracker._trackCollection(this);
|
|
901
|
+
}
|
|
902
|
+
readAccess() {
|
|
903
|
+
DependencyTracker.record(this, COLLECTION_VERSION_KEY);
|
|
904
|
+
}
|
|
905
|
+
get dirtyCounter() {
|
|
906
|
+
return this._dirtyCounter;
|
|
907
|
+
}
|
|
908
|
+
set dirtyCounter(value) {
|
|
909
|
+
this._dirtyCounter = value;
|
|
910
|
+
}
|
|
911
|
+
get isDirty() {
|
|
912
|
+
return this._isDirty;
|
|
913
|
+
}
|
|
914
|
+
set isDirty(value) {
|
|
915
|
+
this._isDirty = value;
|
|
916
|
+
}
|
|
917
|
+
get isValid() {
|
|
918
|
+
return this._isValid;
|
|
919
|
+
}
|
|
920
|
+
set isValid(value) {
|
|
921
|
+
const wasValid = this._isValid;
|
|
922
|
+
this._isValid = value;
|
|
923
|
+
if (wasValid !== value) {
|
|
924
|
+
this.tracker._onValidityChanged(wasValid, value);
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
get length() {
|
|
928
|
+
this.readAccess();
|
|
929
|
+
return this._collection.length;
|
|
930
|
+
}
|
|
931
|
+
get lastItemIndex() {
|
|
932
|
+
this.readAccess();
|
|
933
|
+
return this._collection.length > 0 ? this._collection.length - 1 : void 0;
|
|
934
|
+
}
|
|
935
|
+
get collection() {
|
|
936
|
+
return this._collection;
|
|
937
|
+
}
|
|
938
|
+
set collection(value) {
|
|
939
|
+
this._collection = value;
|
|
940
|
+
}
|
|
941
|
+
get [Symbol.iterator]() {
|
|
942
|
+
this.readAccess();
|
|
943
|
+
return this.collection[Symbol.iterator].bind(this.collection);
|
|
944
|
+
}
|
|
945
|
+
get [Symbol.unscopables]() {
|
|
946
|
+
return this.collection[Symbol.unscopables];
|
|
947
|
+
}
|
|
948
|
+
get error() {
|
|
949
|
+
return this._error;
|
|
950
|
+
}
|
|
951
|
+
set error(value) {
|
|
952
|
+
this._error = value;
|
|
953
|
+
}
|
|
954
|
+
/** @internal */
|
|
955
|
+
_setState(_value) {
|
|
956
|
+
}
|
|
957
|
+
/** @internal */
|
|
958
|
+
_validate() {
|
|
959
|
+
this.error = this._validator ? this._validator(this.collection) : void 0;
|
|
960
|
+
this.isValid = this.error === void 0;
|
|
961
|
+
}
|
|
962
|
+
/** @internal */
|
|
963
|
+
_applyValidation(_messages) {
|
|
964
|
+
}
|
|
965
|
+
splice(start, deleteCount, ...items) {
|
|
966
|
+
let removed;
|
|
967
|
+
this.tracker._doAndTrack(
|
|
968
|
+
() => {
|
|
969
|
+
if (removed !== void 0) {
|
|
970
|
+
this.doSplice(start, deleteCount, items, removed);
|
|
971
|
+
} else {
|
|
972
|
+
removed = this.doSplice(start, deleteCount, items);
|
|
973
|
+
}
|
|
974
|
+
this.trackRemovedObjectDeletions(removed);
|
|
975
|
+
this.trackAddedObjectInsertions(items);
|
|
976
|
+
if (!this.tracker._isReplaying) {
|
|
977
|
+
this.trackedChanged.emit(new TrackedCollectionChanged(items, removed, this._collection));
|
|
978
|
+
}
|
|
979
|
+
},
|
|
980
|
+
() => this.undoSplice(start, items, removed),
|
|
981
|
+
new OperationProperties(this, void 0, 3 /* Collection */)
|
|
982
|
+
);
|
|
983
|
+
return removed;
|
|
984
|
+
}
|
|
985
|
+
doSplice(start, deleteCount, items, reusedRemoved) {
|
|
986
|
+
let removed;
|
|
987
|
+
let event;
|
|
988
|
+
this.tracker.withTrackingSuppressed(() => {
|
|
989
|
+
removed = this.collection.splice(start, deleteCount, ...items);
|
|
990
|
+
this.collection = [...this.collection];
|
|
991
|
+
event = new TrackedCollectionChanged(items, removed, this.collection);
|
|
992
|
+
});
|
|
993
|
+
this.changed.emit(event);
|
|
994
|
+
return reusedRemoved ?? removed;
|
|
995
|
+
}
|
|
996
|
+
trackRemovedObjectDeletions(removed) {
|
|
997
|
+
for (const item of removed) {
|
|
998
|
+
if (item instanceof TrackedObject) {
|
|
999
|
+
item._markRemoved();
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
trackAddedObjectInsertions(added) {
|
|
1004
|
+
for (const item of added) {
|
|
1005
|
+
if (item instanceof TrackedObject) {
|
|
1006
|
+
item._markAdded();
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
undoSplice(start, items, removed) {
|
|
1011
|
+
this.tracker.withTrackingSuppressed(() => {
|
|
1012
|
+
this.collection.splice(start, items?.length ?? 0, ...removed);
|
|
1013
|
+
this.collection = [...this.collection];
|
|
1014
|
+
const event = new TrackedCollectionChanged(
|
|
1015
|
+
removed,
|
|
1016
|
+
items,
|
|
1017
|
+
this.collection
|
|
1018
|
+
);
|
|
1019
|
+
this.changed.emit(event);
|
|
1020
|
+
});
|
|
1021
|
+
}
|
|
1022
|
+
reset(newItems) {
|
|
1023
|
+
this.splice(0, this.collection.length, ...newItems);
|
|
1024
|
+
}
|
|
1025
|
+
reverse() {
|
|
1026
|
+
this.collection.reverse();
|
|
1027
|
+
return this.collection;
|
|
1028
|
+
}
|
|
1029
|
+
sort(compareFn) {
|
|
1030
|
+
if (this.length === 0) {
|
|
1031
|
+
return this;
|
|
1032
|
+
}
|
|
1033
|
+
this.collection.sort(compareFn);
|
|
1034
|
+
return this;
|
|
1035
|
+
}
|
|
1036
|
+
clear() {
|
|
1037
|
+
if (this.length === 0) {
|
|
1038
|
+
return;
|
|
1039
|
+
}
|
|
1040
|
+
this.splice(0, this.length);
|
|
1041
|
+
}
|
|
1042
|
+
remove(item) {
|
|
1043
|
+
const itemIndex = this.collection.indexOf(item);
|
|
1044
|
+
if (itemIndex < 0) {
|
|
1045
|
+
return false;
|
|
1046
|
+
}
|
|
1047
|
+
this.splice(itemIndex, 1);
|
|
1048
|
+
return true;
|
|
1049
|
+
}
|
|
1050
|
+
replace(item, replace) {
|
|
1051
|
+
const itemIndex = this.collection.indexOf(item);
|
|
1052
|
+
if (itemIndex < 0) {
|
|
1053
|
+
return false;
|
|
1054
|
+
}
|
|
1055
|
+
this.splice(itemIndex, 1, replace);
|
|
1056
|
+
return true;
|
|
1057
|
+
}
|
|
1058
|
+
replaceAt(index, replace) {
|
|
1059
|
+
this.splice(index, 1, replace);
|
|
1060
|
+
}
|
|
1061
|
+
pop() {
|
|
1062
|
+
return this.length === 0 ? void 0 : this.splice(this.collection.length - 1, 1)[0];
|
|
1063
|
+
}
|
|
1064
|
+
push(...items) {
|
|
1065
|
+
if (!items || items.length === 0) {
|
|
1066
|
+
return this.length;
|
|
1067
|
+
}
|
|
1068
|
+
this.splice((this.lastItemIndex ?? -1) + 1, 0, ...items);
|
|
1069
|
+
return this.length;
|
|
1070
|
+
}
|
|
1071
|
+
concat(...items) {
|
|
1072
|
+
this.readAccess();
|
|
1073
|
+
return this.collection.concat(...items);
|
|
1074
|
+
}
|
|
1075
|
+
join(separator) {
|
|
1076
|
+
this.readAccess();
|
|
1077
|
+
return this.collection.join(separator);
|
|
1078
|
+
}
|
|
1079
|
+
shift() {
|
|
1080
|
+
return this.length === 0 ? void 0 : this.splice(0, 1)[0];
|
|
1081
|
+
}
|
|
1082
|
+
slice(start, end) {
|
|
1083
|
+
this.readAccess();
|
|
1084
|
+
return this.collection.slice(start, end);
|
|
1085
|
+
}
|
|
1086
|
+
unshift(...items) {
|
|
1087
|
+
this.splice(0, 0, ...items);
|
|
1088
|
+
return this.length;
|
|
1089
|
+
}
|
|
1090
|
+
indexOf(searchElement, fromIndex) {
|
|
1091
|
+
this.readAccess();
|
|
1092
|
+
return this.collection.indexOf(searchElement, fromIndex);
|
|
1093
|
+
}
|
|
1094
|
+
lastIndexOf(searchElement, fromIndex) {
|
|
1095
|
+
this.readAccess();
|
|
1096
|
+
return fromIndex !== void 0 ? this.collection.lastIndexOf(searchElement, fromIndex) : this.collection.lastIndexOf(searchElement);
|
|
1097
|
+
}
|
|
1098
|
+
every(predicate, thisArg) {
|
|
1099
|
+
this.readAccess();
|
|
1100
|
+
return this.collection.every(predicate, thisArg);
|
|
1101
|
+
}
|
|
1102
|
+
some(predicate, thisArg) {
|
|
1103
|
+
this.readAccess();
|
|
1104
|
+
return this.collection.some(predicate, thisArg);
|
|
1105
|
+
}
|
|
1106
|
+
forEach(callbackfn, thisArg) {
|
|
1107
|
+
this.readAccess();
|
|
1108
|
+
this.collection.forEach(callbackfn, thisArg);
|
|
1109
|
+
}
|
|
1110
|
+
map(callbackfn, thisArg) {
|
|
1111
|
+
this.readAccess();
|
|
1112
|
+
return this.collection.map(callbackfn, thisArg);
|
|
1113
|
+
}
|
|
1114
|
+
filter(predicate, thisArg) {
|
|
1115
|
+
this.readAccess();
|
|
1116
|
+
return this.collection.filter(predicate, thisArg);
|
|
1117
|
+
}
|
|
1118
|
+
find(predicate, thisArg) {
|
|
1119
|
+
this.readAccess();
|
|
1120
|
+
return this.collection.find(predicate, thisArg);
|
|
1121
|
+
}
|
|
1122
|
+
findIndex(predicate, thisArg) {
|
|
1123
|
+
this.readAccess();
|
|
1124
|
+
return this.collection.findIndex(predicate, thisArg);
|
|
1125
|
+
}
|
|
1126
|
+
flatMap(callback, thisArg) {
|
|
1127
|
+
this.readAccess();
|
|
1128
|
+
return this.collection.flatMap(callback, thisArg);
|
|
1129
|
+
}
|
|
1130
|
+
includes(searchElement, fromIndex) {
|
|
1131
|
+
this.readAccess();
|
|
1132
|
+
return this.collection.includes(searchElement, fromIndex);
|
|
1133
|
+
}
|
|
1134
|
+
toString() {
|
|
1135
|
+
this.readAccess();
|
|
1136
|
+
return this.collection.toString();
|
|
1137
|
+
}
|
|
1138
|
+
toLocaleString() {
|
|
1139
|
+
this.readAccess();
|
|
1140
|
+
return this.collection.toLocaleString();
|
|
1141
|
+
}
|
|
1142
|
+
entries() {
|
|
1143
|
+
this.readAccess();
|
|
1144
|
+
return this.collection.entries();
|
|
1145
|
+
}
|
|
1146
|
+
keys() {
|
|
1147
|
+
this.readAccess();
|
|
1148
|
+
return this.collection.keys();
|
|
1149
|
+
}
|
|
1150
|
+
values() {
|
|
1151
|
+
this.readAccess();
|
|
1152
|
+
return this.collection.values();
|
|
1153
|
+
}
|
|
1154
|
+
at(index) {
|
|
1155
|
+
this.readAccess();
|
|
1156
|
+
return this.collection.at(index);
|
|
1157
|
+
}
|
|
1158
|
+
fill(value, start, end) {
|
|
1159
|
+
const len = this.length;
|
|
1160
|
+
const s = start === void 0 ? 0 : start < 0 ? Math.max(len + start, 0) : Math.min(start, len);
|
|
1161
|
+
const e = end === void 0 ? len : end < 0 ? Math.max(len + end, 0) : Math.min(end, len);
|
|
1162
|
+
if (s >= e) return this;
|
|
1163
|
+
this.splice(s, e - s, ...new Array(e - s).fill(value));
|
|
1164
|
+
return this;
|
|
1165
|
+
}
|
|
1166
|
+
copyWithin(target, start, end) {
|
|
1167
|
+
const len = this.length;
|
|
1168
|
+
const t = target < 0 ? Math.max(len + target, 0) : Math.min(target, len);
|
|
1169
|
+
const s = start < 0 ? Math.max(len + start, 0) : Math.min(start, len);
|
|
1170
|
+
const e = end === void 0 ? len : end < 0 ? Math.max(len + end, 0) : Math.min(end, len);
|
|
1171
|
+
const itemsToCopy = this.collection.slice(s, e);
|
|
1172
|
+
const count = Math.min(itemsToCopy.length, len - t);
|
|
1173
|
+
if (count > 0) {
|
|
1174
|
+
this.splice(t, count, ...itemsToCopy.slice(0, count));
|
|
1175
|
+
}
|
|
1176
|
+
return this;
|
|
1177
|
+
}
|
|
1178
|
+
reduce(callbackfn, initialValue) {
|
|
1179
|
+
this.readAccess();
|
|
1180
|
+
return initialValue !== void 0 ? this.collection.reduce(callbackfn, initialValue) : this.collection.reduce(callbackfn);
|
|
1181
|
+
}
|
|
1182
|
+
reduceRight(callbackfn, initialValue) {
|
|
1183
|
+
this.readAccess();
|
|
1184
|
+
return initialValue !== void 0 ? this.collection.reduceRight(callbackfn, initialValue) : this.collection.reduceRight(callbackfn);
|
|
1185
|
+
}
|
|
1186
|
+
flat(depth) {
|
|
1187
|
+
DependencyTracker.record(this, COLLECTION_VERSION_KEY);
|
|
1188
|
+
return this._collection.flat(depth);
|
|
1189
|
+
}
|
|
1190
|
+
findLast(predicate, thisArg) {
|
|
1191
|
+
this.readAccess();
|
|
1192
|
+
return this.collection.findLast(predicate, thisArg);
|
|
1193
|
+
}
|
|
1194
|
+
findLastIndex(predicate, thisArg) {
|
|
1195
|
+
this.readAccess();
|
|
1196
|
+
return this.collection.findLastIndex(predicate, thisArg);
|
|
1197
|
+
}
|
|
1198
|
+
toReversed() {
|
|
1199
|
+
this.readAccess();
|
|
1200
|
+
return this.collection.toReversed();
|
|
1201
|
+
}
|
|
1202
|
+
toSorted(compareFn) {
|
|
1203
|
+
this.readAccess();
|
|
1204
|
+
return this.collection.toSorted(compareFn);
|
|
1205
|
+
}
|
|
1206
|
+
toSpliced(start, deleteCount, ...items) {
|
|
1207
|
+
this.readAccess();
|
|
1208
|
+
return this.collection.toSpliced(start, deleteCount, ...items);
|
|
1209
|
+
}
|
|
1210
|
+
with(index, value) {
|
|
1211
|
+
this.readAccess();
|
|
1212
|
+
return this.collection.with(index, value);
|
|
1213
|
+
}
|
|
1214
|
+
first() {
|
|
1215
|
+
this.readAccess();
|
|
1216
|
+
return this._collection.length > 0 ? this._collection[0] : void 0;
|
|
1217
|
+
}
|
|
1218
|
+
destroy() {
|
|
1219
|
+
this.tracker._untrackCollection(this);
|
|
1220
|
+
}
|
|
1221
|
+
};
|
|
1222
|
+
var TrackedCollectionChanged = class {
|
|
1223
|
+
constructor(added, removed, newCollection) {
|
|
1224
|
+
this.added = added;
|
|
1225
|
+
this.removed = removed;
|
|
1226
|
+
this.newCollection = newCollection;
|
|
1227
|
+
}
|
|
1228
|
+
};
|
|
1229
|
+
export {
|
|
1230
|
+
AutoId,
|
|
1231
|
+
State,
|
|
1232
|
+
Tracked,
|
|
1233
|
+
TrackedCollection,
|
|
1234
|
+
TrackedCollectionChanged,
|
|
1235
|
+
TrackedObject,
|
|
1236
|
+
Tracker,
|
|
1237
|
+
TypedEvent
|
|
1238
|
+
};
|
|
1239
|
+
//# sourceMappingURL=index.js.map
|