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