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