@katn30/trakr 2.2.1 → 4.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/README.md +294 -0
- package/dist/dev/index.cjs +764 -6
- package/dist/dev/index.cjs.map +1 -1
- package/dist/dev/index.d.cts +92 -1
- package/dist/dev/index.d.ts +92 -1
- package/dist/dev/index.js +756 -5
- package/dist/dev/index.js.map +1 -1
- package/dist/prod/index.cjs +764 -6
- package/dist/prod/index.cjs.map +1 -1
- package/dist/prod/index.js +756 -5
- package/dist/prod/index.js.map +1 -1
- package/package.json +1 -1
package/dist/prod/index.js
CHANGED
|
@@ -257,17 +257,73 @@ var OperationProperties = class {
|
|
|
257
257
|
|
|
258
258
|
// src/ExternallyAssigned.ts
|
|
259
259
|
var AUTO_ID = /* @__PURE__ */ Symbol("autoId");
|
|
260
|
+
var ID_PROPS = /* @__PURE__ */ Symbol("idProps");
|
|
260
261
|
function AutoId(_target, context) {
|
|
261
262
|
context.addInitializer(function() {
|
|
262
|
-
Object.
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
263
|
+
const proto = Object.getPrototypeOf(this);
|
|
264
|
+
if (!Object.prototype.hasOwnProperty.call(proto, AUTO_ID)) {
|
|
265
|
+
Object.defineProperty(proto, AUTO_ID, {
|
|
266
|
+
value: String(context.name),
|
|
267
|
+
configurable: true
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
addIdentityProp(proto, String(context.name));
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
function Id(_target, context) {
|
|
274
|
+
context.addInitializer(function() {
|
|
275
|
+
const proto = Object.getPrototypeOf(this);
|
|
276
|
+
addIdentityProp(proto, String(context.name));
|
|
266
277
|
});
|
|
267
278
|
}
|
|
279
|
+
function addIdentityProp(proto, propertyName) {
|
|
280
|
+
if (!Object.prototype.hasOwnProperty.call(proto, ID_PROPS)) {
|
|
281
|
+
Object.defineProperty(proto, ID_PROPS, {
|
|
282
|
+
value: [],
|
|
283
|
+
configurable: true,
|
|
284
|
+
writable: true
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
const list = proto[ID_PROPS];
|
|
288
|
+
if (!list.includes(propertyName)) list.push(propertyName);
|
|
289
|
+
}
|
|
268
290
|
function getAutoIdProperty(proto) {
|
|
269
291
|
return AUTO_ID in proto ? proto[AUTO_ID] : void 0;
|
|
270
292
|
}
|
|
293
|
+
function getIdentityProperties(proto) {
|
|
294
|
+
const chain = [];
|
|
295
|
+
let current = proto;
|
|
296
|
+
while (current) {
|
|
297
|
+
chain.push(current);
|
|
298
|
+
current = Object.getPrototypeOf(current);
|
|
299
|
+
}
|
|
300
|
+
const merged = [];
|
|
301
|
+
for (let i = chain.length - 1; i >= 0; i--) {
|
|
302
|
+
const proto2 = chain[i];
|
|
303
|
+
if (!Object.prototype.hasOwnProperty.call(proto2, ID_PROPS)) continue;
|
|
304
|
+
const list = proto2[ID_PROPS];
|
|
305
|
+
for (const name of list) {
|
|
306
|
+
if (!merged.includes(name)) merged.push(name);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return merged;
|
|
310
|
+
}
|
|
311
|
+
function getIdentity(obj) {
|
|
312
|
+
const props = getIdentityProperties(Object.getPrototypeOf(obj));
|
|
313
|
+
if (props.length === 0) return void 0;
|
|
314
|
+
if (props.length === 1) {
|
|
315
|
+
return obj[props[0]];
|
|
316
|
+
}
|
|
317
|
+
const result = {};
|
|
318
|
+
for (const p of props) result[p] = obj[p];
|
|
319
|
+
return result;
|
|
320
|
+
}
|
|
321
|
+
function getIdentityObject(obj) {
|
|
322
|
+
const props = getIdentityProperties(Object.getPrototypeOf(obj));
|
|
323
|
+
const result = {};
|
|
324
|
+
for (const p of props) result[p] = obj[p];
|
|
325
|
+
return result;
|
|
326
|
+
}
|
|
271
327
|
|
|
272
328
|
// src/TrackedObjectStateMachine.ts
|
|
273
329
|
function applyStateTransition(obj, event, direction, context) {
|
|
@@ -1328,8 +1384,700 @@ var TrackedCollectionChanged = class {
|
|
|
1328
1384
|
this.newCollection = newCollection;
|
|
1329
1385
|
}
|
|
1330
1386
|
};
|
|
1387
|
+
|
|
1388
|
+
// src/EventRegistry.ts
|
|
1389
|
+
var EVENT_METADATA = /* @__PURE__ */ Symbol("eventMetadata");
|
|
1390
|
+
var instanceState = /* @__PURE__ */ new WeakMap();
|
|
1391
|
+
var instanceHistory = /* @__PURE__ */ new WeakMap();
|
|
1392
|
+
var instanceHistoryTimes = /* @__PURE__ */ new WeakMap();
|
|
1393
|
+
var subscribedInstances = /* @__PURE__ */ new WeakSet();
|
|
1394
|
+
var itemOriginCollection = /* @__PURE__ */ new WeakMap();
|
|
1395
|
+
var DEFAULT_GROUP = "";
|
|
1396
|
+
function hasOwnEventMetadata(proto) {
|
|
1397
|
+
return Object.prototype.hasOwnProperty.call(proto, EVENT_METADATA);
|
|
1398
|
+
}
|
|
1399
|
+
function ownEventMetadata(proto) {
|
|
1400
|
+
return hasOwnEventMetadata(proto) ? proto[EVENT_METADATA] : void 0;
|
|
1401
|
+
}
|
|
1402
|
+
function registerEventProperty(proto, property, options) {
|
|
1403
|
+
if (!hasOwnEventMetadata(proto)) {
|
|
1404
|
+
Object.defineProperty(proto, EVENT_METADATA, {
|
|
1405
|
+
value: /* @__PURE__ */ new Map(),
|
|
1406
|
+
configurable: true
|
|
1407
|
+
});
|
|
1408
|
+
}
|
|
1409
|
+
const map = proto[EVENT_METADATA];
|
|
1410
|
+
if (!map.has(property)) {
|
|
1411
|
+
map.set(property, options);
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1414
|
+
function getEventMetadata(proto) {
|
|
1415
|
+
const chain = [];
|
|
1416
|
+
let current = proto;
|
|
1417
|
+
while (current) {
|
|
1418
|
+
chain.push(current);
|
|
1419
|
+
current = Object.getPrototypeOf(current);
|
|
1420
|
+
}
|
|
1421
|
+
const merged = /* @__PURE__ */ new Map();
|
|
1422
|
+
for (let i = chain.length - 1; i >= 0; i--) {
|
|
1423
|
+
const own = ownEventMetadata(chain[i]);
|
|
1424
|
+
if (!own) continue;
|
|
1425
|
+
own.forEach((options, property) => {
|
|
1426
|
+
if (!merged.has(property)) merged.set(property, options);
|
|
1427
|
+
});
|
|
1428
|
+
}
|
|
1429
|
+
return merged;
|
|
1430
|
+
}
|
|
1431
|
+
var contextStacks = /* @__PURE__ */ new WeakMap();
|
|
1432
|
+
function pushContext(tracker, ctx) {
|
|
1433
|
+
let stack = contextStacks.get(tracker);
|
|
1434
|
+
if (!stack) {
|
|
1435
|
+
stack = [];
|
|
1436
|
+
contextStacks.set(tracker, stack);
|
|
1437
|
+
}
|
|
1438
|
+
stack.push(ctx);
|
|
1439
|
+
}
|
|
1440
|
+
function popContext(tracker) {
|
|
1441
|
+
const stack = contextStacks.get(tracker);
|
|
1442
|
+
if (stack && stack.length > 0) stack.pop();
|
|
1443
|
+
}
|
|
1444
|
+
function peekContext(tracker) {
|
|
1445
|
+
const stack = contextStacks.get(tracker);
|
|
1446
|
+
return stack && stack.length > 0 ? stack[stack.length - 1] : void 0;
|
|
1447
|
+
}
|
|
1448
|
+
function ensureEventStateSubscription(target) {
|
|
1449
|
+
if (subscribedInstances.has(target)) return;
|
|
1450
|
+
subscribedInstances.add(target);
|
|
1451
|
+
target.changed.subscribe((evt) => {
|
|
1452
|
+
const meta = getEventMetadata(Object.getPrototypeOf(target));
|
|
1453
|
+
const propMeta = meta.get(evt.property);
|
|
1454
|
+
if (!propMeta) return;
|
|
1455
|
+
if (propMeta.history) {
|
|
1456
|
+
appendHistoryEntry(
|
|
1457
|
+
target,
|
|
1458
|
+
evt.property,
|
|
1459
|
+
evt.newValue,
|
|
1460
|
+
evt.oldValue,
|
|
1461
|
+
propMeta.history,
|
|
1462
|
+
propMeta.coalesceWithin
|
|
1463
|
+
);
|
|
1464
|
+
} else {
|
|
1465
|
+
updateFinalValue(target, evt.property, evt.newValue, evt.oldValue);
|
|
1466
|
+
}
|
|
1467
|
+
});
|
|
1468
|
+
}
|
|
1469
|
+
function updateFinalValue(target, property, newValue, oldValue) {
|
|
1470
|
+
const state = getOrCreateEventState(target);
|
|
1471
|
+
let entry = state.get(property);
|
|
1472
|
+
if (!entry) {
|
|
1473
|
+
entry = { originalValue: oldValue, currentValue: newValue };
|
|
1474
|
+
state.set(property, entry);
|
|
1475
|
+
} else {
|
|
1476
|
+
entry.currentValue = newValue;
|
|
1477
|
+
}
|
|
1478
|
+
if (entry.currentValue === entry.originalValue) {
|
|
1479
|
+
state.delete(property);
|
|
1480
|
+
}
|
|
1481
|
+
}
|
|
1482
|
+
function appendHistoryEntry(target, property, newValue, oldValue, historyConfig, coalesceWithin) {
|
|
1483
|
+
const chains = getOrCreateHistoryState(target);
|
|
1484
|
+
let chain = chains.get(property);
|
|
1485
|
+
if (!chain) {
|
|
1486
|
+
chain = [];
|
|
1487
|
+
chains.set(property, chain);
|
|
1488
|
+
}
|
|
1489
|
+
const times = getOrCreateHistoryTimes(target);
|
|
1490
|
+
const now = Date.now();
|
|
1491
|
+
const lastTime = times.get(property);
|
|
1492
|
+
const shouldReplace = coalesceWithin !== void 0 && lastTime !== void 0 && now - lastTime < coalesceWithin && chain.length > 0;
|
|
1493
|
+
let entry;
|
|
1494
|
+
if (typeof historyConfig === "object" && historyConfig.entryFactory) {
|
|
1495
|
+
const ctx = peekContext(target.tracker);
|
|
1496
|
+
const change = { time: /* @__PURE__ */ new Date() };
|
|
1497
|
+
entry = historyConfig.entryFactory(target, newValue, oldValue, change, ctx);
|
|
1498
|
+
} else {
|
|
1499
|
+
entry = { property, value: newValue };
|
|
1500
|
+
}
|
|
1501
|
+
if (shouldReplace) {
|
|
1502
|
+
chain[chain.length - 1] = entry;
|
|
1503
|
+
} else {
|
|
1504
|
+
chain.push(entry);
|
|
1505
|
+
}
|
|
1506
|
+
times.set(property, now);
|
|
1507
|
+
}
|
|
1508
|
+
function getOrCreateHistoryTimes(target) {
|
|
1509
|
+
let times = instanceHistoryTimes.get(target);
|
|
1510
|
+
if (!times) {
|
|
1511
|
+
times = /* @__PURE__ */ new Map();
|
|
1512
|
+
instanceHistoryTimes.set(target, times);
|
|
1513
|
+
}
|
|
1514
|
+
return times;
|
|
1515
|
+
}
|
|
1516
|
+
function getOrCreateEventState(target) {
|
|
1517
|
+
let state = instanceState.get(target);
|
|
1518
|
+
if (!state) {
|
|
1519
|
+
state = /* @__PURE__ */ new Map();
|
|
1520
|
+
instanceState.set(target, state);
|
|
1521
|
+
}
|
|
1522
|
+
return state;
|
|
1523
|
+
}
|
|
1524
|
+
function getOrCreateHistoryState(target) {
|
|
1525
|
+
let state = instanceHistory.get(target);
|
|
1526
|
+
if (!state) {
|
|
1527
|
+
state = /* @__PURE__ */ new Map();
|
|
1528
|
+
instanceHistory.set(target, state);
|
|
1529
|
+
}
|
|
1530
|
+
return state;
|
|
1531
|
+
}
|
|
1532
|
+
function getEventState(target) {
|
|
1533
|
+
return instanceState.get(target) ?? /* @__PURE__ */ new Map();
|
|
1534
|
+
}
|
|
1535
|
+
function getHistoryState(target) {
|
|
1536
|
+
return instanceHistory.get(target) ?? /* @__PURE__ */ new Map();
|
|
1537
|
+
}
|
|
1538
|
+
function clearEventState(target) {
|
|
1539
|
+
instanceState.delete(target);
|
|
1540
|
+
instanceHistory.delete(target);
|
|
1541
|
+
instanceHistoryTimes.delete(target);
|
|
1542
|
+
}
|
|
1543
|
+
function recordItemOrigin(item, collection) {
|
|
1544
|
+
itemOriginCollection.set(item, collection);
|
|
1545
|
+
}
|
|
1546
|
+
function getItemOrigin(item) {
|
|
1547
|
+
return itemOriginCollection.get(item);
|
|
1548
|
+
}
|
|
1549
|
+
|
|
1550
|
+
// src/EventTrackedCollection.ts
|
|
1551
|
+
var EventTrackedCollection = class extends TrackedCollection {
|
|
1552
|
+
constructor(tracker, items, validator, options) {
|
|
1553
|
+
super(tracker, items, validator);
|
|
1554
|
+
this._historyOps = [];
|
|
1555
|
+
this._baselineItems = /* @__PURE__ */ new Set();
|
|
1556
|
+
this._eventOptions = options;
|
|
1557
|
+
for (const item of this.collection) {
|
|
1558
|
+
this._baselineItems.add(item);
|
|
1559
|
+
if (item instanceof TrackedObject) {
|
|
1560
|
+
recordItemOrigin(item, this);
|
|
1561
|
+
this._validateItemHasIdentity(item);
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
this.changed.subscribe((evt) => {
|
|
1565
|
+
for (const item of evt.added) {
|
|
1566
|
+
if (item instanceof TrackedObject) {
|
|
1567
|
+
recordItemOrigin(item, this);
|
|
1568
|
+
this._validateItemHasIdentity(item);
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
if (this._isAggregateMode() && !this.tracker._isReplaying) {
|
|
1572
|
+
for (const item of evt.added) {
|
|
1573
|
+
if (item instanceof TrackedObject) {
|
|
1574
|
+
this._recordAddOp(item);
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
for (const item of evt.removed) {
|
|
1578
|
+
if (item instanceof TrackedObject) {
|
|
1579
|
+
this._recordRemoveOp(item);
|
|
1580
|
+
}
|
|
1581
|
+
}
|
|
1582
|
+
}
|
|
1583
|
+
});
|
|
1584
|
+
if (this._isHistoryMode()) {
|
|
1585
|
+
this._attachItemChangeWatchers();
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
/** @internal */
|
|
1589
|
+
get _lifecycleOptions() {
|
|
1590
|
+
if (!this._eventOptions) return void 0;
|
|
1591
|
+
if (this._isAggregateMode()) return void 0;
|
|
1592
|
+
return {
|
|
1593
|
+
itemAdded: this._eventOptions.itemAdded,
|
|
1594
|
+
itemRemoved: this._eventOptions.itemRemoved
|
|
1595
|
+
};
|
|
1596
|
+
}
|
|
1597
|
+
/** @internal */
|
|
1598
|
+
get _aggregateOptions() {
|
|
1599
|
+
return this._isAggregateMode() ? this._eventOptions : void 0;
|
|
1600
|
+
}
|
|
1601
|
+
/** @internal */
|
|
1602
|
+
get _historyOpsList() {
|
|
1603
|
+
return this._historyOps;
|
|
1604
|
+
}
|
|
1605
|
+
/** @internal */
|
|
1606
|
+
_clearHistoryOps() {
|
|
1607
|
+
this._historyOps.length = 0;
|
|
1608
|
+
this._baselineItems.clear();
|
|
1609
|
+
for (const item of this.collection) this._baselineItems.add(item);
|
|
1610
|
+
}
|
|
1611
|
+
/** @internal */
|
|
1612
|
+
_isInBaseline(item) {
|
|
1613
|
+
return this._baselineItems.has(item);
|
|
1614
|
+
}
|
|
1615
|
+
/** @internal */
|
|
1616
|
+
_baselineListSnapshot() {
|
|
1617
|
+
return [...this._baselineItems];
|
|
1618
|
+
}
|
|
1619
|
+
/** @internal */
|
|
1620
|
+
_rebaseline() {
|
|
1621
|
+
this._baselineItems.clear();
|
|
1622
|
+
for (const item of this.collection) this._baselineItems.add(item);
|
|
1623
|
+
}
|
|
1624
|
+
_isAggregateMode() {
|
|
1625
|
+
if (!this._eventOptions) return false;
|
|
1626
|
+
return this._eventOptions.eventType !== void 0 || this._eventOptions.history !== void 0;
|
|
1627
|
+
}
|
|
1628
|
+
_isHistoryMode() {
|
|
1629
|
+
return !!this._eventOptions?.history;
|
|
1630
|
+
}
|
|
1631
|
+
_validateItemHasIdentity(item) {
|
|
1632
|
+
const props = getIdentityProperties(Object.getPrototypeOf(item));
|
|
1633
|
+
if (props.length === 0) {
|
|
1634
|
+
throw new Error(
|
|
1635
|
+
`EventTrackedCollection item type ${item.constructor.name} must declare at least one @Id or @AutoId property when used with eventType or history mode`
|
|
1636
|
+
);
|
|
1637
|
+
}
|
|
1638
|
+
}
|
|
1639
|
+
_attachItemChangeWatchers() {
|
|
1640
|
+
const watchItem = (item) => {
|
|
1641
|
+
item.changed.subscribe((_evt) => {
|
|
1642
|
+
if (this.tracker._isReplaying) return;
|
|
1643
|
+
if (!this.collection.includes(item)) return;
|
|
1644
|
+
this._recordChangeOp(item);
|
|
1645
|
+
});
|
|
1646
|
+
};
|
|
1647
|
+
for (const item of this.collection) {
|
|
1648
|
+
if (item instanceof TrackedObject) watchItem(item);
|
|
1649
|
+
}
|
|
1650
|
+
this.changed.subscribe((evt) => {
|
|
1651
|
+
for (const item of evt.added) {
|
|
1652
|
+
if (item instanceof TrackedObject) watchItem(item);
|
|
1653
|
+
}
|
|
1654
|
+
});
|
|
1655
|
+
}
|
|
1656
|
+
_recordAddOp(item) {
|
|
1657
|
+
const opts = this._eventOptions;
|
|
1658
|
+
if (typeof opts?.history === "object" && opts.history.entryFactory) {
|
|
1659
|
+
const ctx = peekContext(this.tracker);
|
|
1660
|
+
const change = { time: /* @__PURE__ */ new Date() };
|
|
1661
|
+
this._historyOps.push({
|
|
1662
|
+
op: "add",
|
|
1663
|
+
raw: opts.history.entryFactory(item, change, ctx)
|
|
1664
|
+
});
|
|
1665
|
+
} else if (opts?.history === true) {
|
|
1666
|
+
this._historyOps.push({ op: "add", item, snapshot: snapshotItemAtRecordTime(item) });
|
|
1667
|
+
}
|
|
1668
|
+
}
|
|
1669
|
+
_recordRemoveOp(item) {
|
|
1670
|
+
const opts = this._eventOptions;
|
|
1671
|
+
const identity = getIdentity(item);
|
|
1672
|
+
if (typeof opts?.history === "object" && opts.history.entryFactory) {
|
|
1673
|
+
const ctx = peekContext(this.tracker);
|
|
1674
|
+
const change = { time: /* @__PURE__ */ new Date() };
|
|
1675
|
+
this._historyOps.push({
|
|
1676
|
+
op: "remove",
|
|
1677
|
+
raw: opts.history.entryFactory(item, change, ctx)
|
|
1678
|
+
});
|
|
1679
|
+
} else if (opts?.history === true) {
|
|
1680
|
+
this._historyOps.push({ op: "remove", identity });
|
|
1681
|
+
}
|
|
1682
|
+
}
|
|
1683
|
+
_recordChangeOpDiff(item) {
|
|
1684
|
+
const proto = Object.getPrototypeOf(item);
|
|
1685
|
+
const idProps = getIdentityProperties(proto);
|
|
1686
|
+
const meta = getEventMetadata(proto);
|
|
1687
|
+
const rec = item;
|
|
1688
|
+
const diff = {};
|
|
1689
|
+
for (const [propName] of meta) {
|
|
1690
|
+
if (idProps.includes(propName)) continue;
|
|
1691
|
+
const v = rec[propName];
|
|
1692
|
+
diff[propName] = v === void 0 ? null : v;
|
|
1693
|
+
}
|
|
1694
|
+
return diff;
|
|
1695
|
+
}
|
|
1696
|
+
_recordChangeOp(item) {
|
|
1697
|
+
const opts = this._eventOptions;
|
|
1698
|
+
if (typeof opts?.history === "object" && opts.history.entryFactory) {
|
|
1699
|
+
const ctx = peekContext(this.tracker);
|
|
1700
|
+
const change = { time: /* @__PURE__ */ new Date() };
|
|
1701
|
+
this._historyOps.push({
|
|
1702
|
+
op: "change",
|
|
1703
|
+
raw: opts.history.entryFactory(item, change, ctx)
|
|
1704
|
+
});
|
|
1705
|
+
} else if (opts?.history === true) {
|
|
1706
|
+
const identity = getIdentity(item);
|
|
1707
|
+
const diff = this._recordChangeOpDiff(item);
|
|
1708
|
+
this._historyOps.push({ op: "change", identity, item, diff });
|
|
1709
|
+
}
|
|
1710
|
+
}
|
|
1711
|
+
};
|
|
1712
|
+
function snapshotItemAtRecordTime(item) {
|
|
1713
|
+
const proto = Object.getPrototypeOf(item);
|
|
1714
|
+
const meta = getEventMetadata(proto);
|
|
1715
|
+
const idProps = getIdentityProperties(proto);
|
|
1716
|
+
const autoIdProp = getAutoIdProperty(proto);
|
|
1717
|
+
const rec = item;
|
|
1718
|
+
const snap = {};
|
|
1719
|
+
for (const idProp of idProps) {
|
|
1720
|
+
if (idProp === autoIdProp) snap[idProp] = null;
|
|
1721
|
+
else snap[idProp] = rec[idProp];
|
|
1722
|
+
}
|
|
1723
|
+
for (const [propName] of meta) {
|
|
1724
|
+
if (idProps.includes(propName)) continue;
|
|
1725
|
+
const v = rec[propName];
|
|
1726
|
+
snap[propName] = v === void 0 ? null : v;
|
|
1727
|
+
}
|
|
1728
|
+
return snap;
|
|
1729
|
+
}
|
|
1730
|
+
|
|
1731
|
+
// src/EventTracker.ts
|
|
1732
|
+
var EventTracker = class extends Tracker {
|
|
1733
|
+
withContext(ctx, action) {
|
|
1734
|
+
pushContext(this, ctx);
|
|
1735
|
+
try {
|
|
1736
|
+
return action();
|
|
1737
|
+
} finally {
|
|
1738
|
+
popContext(this);
|
|
1739
|
+
}
|
|
1740
|
+
}
|
|
1741
|
+
onCommit(keys) {
|
|
1742
|
+
super.onCommit(keys);
|
|
1743
|
+
for (const obj of this.trackedObjects) {
|
|
1744
|
+
clearEventState(obj);
|
|
1745
|
+
}
|
|
1746
|
+
for (const col of this.trackedCollections) {
|
|
1747
|
+
if (col instanceof EventTrackedCollection) {
|
|
1748
|
+
col._clearHistoryOps();
|
|
1749
|
+
}
|
|
1750
|
+
}
|
|
1751
|
+
}
|
|
1752
|
+
generateEvents() {
|
|
1753
|
+
const events = [];
|
|
1754
|
+
for (const obj of this.trackedObjects) {
|
|
1755
|
+
const origin = getItemOrigin(obj);
|
|
1756
|
+
if (!origin) continue;
|
|
1757
|
+
const legacy = origin._lifecycleOptions;
|
|
1758
|
+
if (!legacy) continue;
|
|
1759
|
+
const meta = getEventMetadata(Object.getPrototypeOf(obj));
|
|
1760
|
+
switch (obj.state) {
|
|
1761
|
+
case "Insert" /* Insert */:
|
|
1762
|
+
if (legacy.itemAdded) {
|
|
1763
|
+
events.push(buildItemAddedEvent(obj, meta, legacy.itemAdded));
|
|
1764
|
+
}
|
|
1765
|
+
break;
|
|
1766
|
+
case "Deleted" /* Deleted */:
|
|
1767
|
+
if (legacy.itemRemoved) {
|
|
1768
|
+
events.push(buildItemRemovedEvent(obj, legacy.itemRemoved));
|
|
1769
|
+
}
|
|
1770
|
+
break;
|
|
1771
|
+
case "Changed" /* Changed */:
|
|
1772
|
+
pushFieldClusterEvents(obj, meta, events);
|
|
1773
|
+
break;
|
|
1774
|
+
}
|
|
1775
|
+
}
|
|
1776
|
+
const ownedCollections = /* @__PURE__ */ new Map();
|
|
1777
|
+
for (const col of this.trackedCollections) {
|
|
1778
|
+
if (!(col instanceof EventTrackedCollection)) continue;
|
|
1779
|
+
const agg = col._aggregateOptions;
|
|
1780
|
+
if (!agg?.owner) continue;
|
|
1781
|
+
const owner = agg.owner.object;
|
|
1782
|
+
let list = ownedCollections.get(owner);
|
|
1783
|
+
if (!list) {
|
|
1784
|
+
list = [];
|
|
1785
|
+
ownedCollections.set(owner, list);
|
|
1786
|
+
}
|
|
1787
|
+
list.push(col);
|
|
1788
|
+
}
|
|
1789
|
+
for (const obj of this.trackedObjects) {
|
|
1790
|
+
const origin = getItemOrigin(obj);
|
|
1791
|
+
if (origin) continue;
|
|
1792
|
+
const groups = /* @__PURE__ */ new Map();
|
|
1793
|
+
const meta = getEventMetadata(Object.getPrototypeOf(obj));
|
|
1794
|
+
const stateMap = getEventState(obj);
|
|
1795
|
+
const historyMap = getHistoryState(obj);
|
|
1796
|
+
for (const [propName, propMeta] of meta) {
|
|
1797
|
+
const group = propMeta.eventType ?? DEFAULT_GROUP;
|
|
1798
|
+
if (propMeta.history) {
|
|
1799
|
+
const chain = historyMap.get(propName);
|
|
1800
|
+
if (chain && chain.length > 0) {
|
|
1801
|
+
addToGroup(groups, group, propName, [...chain]);
|
|
1802
|
+
}
|
|
1803
|
+
} else {
|
|
1804
|
+
const entry = stateMap.get(propName);
|
|
1805
|
+
if (entry) {
|
|
1806
|
+
addToGroup(groups, group, propName, normalizeValue(entry.currentValue));
|
|
1807
|
+
}
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1810
|
+
const owned = ownedCollections.get(obj) ?? [];
|
|
1811
|
+
for (const col of owned) {
|
|
1812
|
+
const slot = computeCollectionSlot(col, this);
|
|
1813
|
+
if (slot === void 0) continue;
|
|
1814
|
+
const agg = col._aggregateOptions;
|
|
1815
|
+
const group = agg.eventType ?? DEFAULT_GROUP;
|
|
1816
|
+
const propertyName = agg.owner.property;
|
|
1817
|
+
addToGroup(groups, group, propertyName, slot);
|
|
1818
|
+
}
|
|
1819
|
+
if (groups.size === 0) continue;
|
|
1820
|
+
const identity = getIdentity(obj);
|
|
1821
|
+
for (const [group, payload] of groups) {
|
|
1822
|
+
const event = {
|
|
1823
|
+
eventType: group,
|
|
1824
|
+
payload,
|
|
1825
|
+
trackingId: obj.trackingId
|
|
1826
|
+
};
|
|
1827
|
+
if (identity !== void 0) {
|
|
1828
|
+
event.targetId = identity;
|
|
1829
|
+
}
|
|
1830
|
+
events.push(event);
|
|
1831
|
+
}
|
|
1832
|
+
}
|
|
1833
|
+
for (const col of this.trackedCollections) {
|
|
1834
|
+
if (!(col instanceof EventTrackedCollection)) continue;
|
|
1835
|
+
const agg = col._aggregateOptions;
|
|
1836
|
+
if (!agg || agg.owner) continue;
|
|
1837
|
+
const slot = computeCollectionSlot(col, this);
|
|
1838
|
+
if (slot === void 0) continue;
|
|
1839
|
+
const eventType = agg.eventType ?? DEFAULT_GROUP;
|
|
1840
|
+
events.push({
|
|
1841
|
+
eventType,
|
|
1842
|
+
payload: slot
|
|
1843
|
+
});
|
|
1844
|
+
}
|
|
1845
|
+
return events;
|
|
1846
|
+
}
|
|
1847
|
+
};
|
|
1848
|
+
function addToGroup(groups, group, key, value) {
|
|
1849
|
+
let payload = groups.get(group);
|
|
1850
|
+
if (!payload) {
|
|
1851
|
+
payload = {};
|
|
1852
|
+
groups.set(group, payload);
|
|
1853
|
+
}
|
|
1854
|
+
payload[key] = value;
|
|
1855
|
+
}
|
|
1856
|
+
function buildItemAddedEvent(obj, meta, eventType) {
|
|
1857
|
+
const payload = {};
|
|
1858
|
+
for (const [propertyName] of meta) {
|
|
1859
|
+
const value = obj[propertyName];
|
|
1860
|
+
payload[propertyName] = normalizeValue(value);
|
|
1861
|
+
}
|
|
1862
|
+
return {
|
|
1863
|
+
eventType,
|
|
1864
|
+
payload,
|
|
1865
|
+
trackingId: obj.trackingId
|
|
1866
|
+
};
|
|
1867
|
+
}
|
|
1868
|
+
function buildItemRemovedEvent(obj, eventType) {
|
|
1869
|
+
const autoIdProp = getAutoIdProperty(Object.getPrototypeOf(obj));
|
|
1870
|
+
const targetId = autoIdProp ? obj[autoIdProp] : void 0;
|
|
1871
|
+
const event = {
|
|
1872
|
+
eventType,
|
|
1873
|
+
payload: {}
|
|
1874
|
+
};
|
|
1875
|
+
if (typeof targetId === "number") {
|
|
1876
|
+
event.targetId = targetId;
|
|
1877
|
+
}
|
|
1878
|
+
return event;
|
|
1879
|
+
}
|
|
1880
|
+
function pushFieldClusterEvents(obj, meta, events) {
|
|
1881
|
+
const state = getEventState(obj);
|
|
1882
|
+
if (state.size === 0) return;
|
|
1883
|
+
const grouped = /* @__PURE__ */ new Map();
|
|
1884
|
+
for (const [propertyName, propMeta] of meta) {
|
|
1885
|
+
if (!state.has(propertyName)) continue;
|
|
1886
|
+
if (!propMeta.eventType) continue;
|
|
1887
|
+
let bucket = grouped.get(propMeta.eventType);
|
|
1888
|
+
if (!bucket) {
|
|
1889
|
+
bucket = [];
|
|
1890
|
+
grouped.set(propMeta.eventType, bucket);
|
|
1891
|
+
}
|
|
1892
|
+
bucket.push(propertyName);
|
|
1893
|
+
}
|
|
1894
|
+
const autoIdProp = getAutoIdProperty(Object.getPrototypeOf(obj));
|
|
1895
|
+
const targetIdRaw = autoIdProp ? obj[autoIdProp] : void 0;
|
|
1896
|
+
for (const [eventType, propertyNames] of grouped) {
|
|
1897
|
+
const payload = {};
|
|
1898
|
+
for (const propertyName of propertyNames) {
|
|
1899
|
+
payload[propertyName] = normalizeValue(state.get(propertyName).currentValue);
|
|
1900
|
+
}
|
|
1901
|
+
const event = {
|
|
1902
|
+
eventType,
|
|
1903
|
+
payload,
|
|
1904
|
+
trackingId: obj.trackingId
|
|
1905
|
+
};
|
|
1906
|
+
if (typeof targetIdRaw === "number") {
|
|
1907
|
+
event.targetId = targetIdRaw;
|
|
1908
|
+
}
|
|
1909
|
+
events.push(event);
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1912
|
+
function computeCollectionSlot(col, tracker) {
|
|
1913
|
+
const agg = col._aggregateOptions;
|
|
1914
|
+
const isHistory = agg.history !== void 0 && agg.history !== false;
|
|
1915
|
+
if (isHistory) return computeHistoryOps(col, agg);
|
|
1916
|
+
return computeBucketedSlot(col, tracker);
|
|
1917
|
+
}
|
|
1918
|
+
function computeHistoryOps(col, agg) {
|
|
1919
|
+
const opsList = col._historyOpsList;
|
|
1920
|
+
const hasFactory = typeof agg.history === "object" && agg.history.entryFactory;
|
|
1921
|
+
const ops = [];
|
|
1922
|
+
for (const op of opsList) {
|
|
1923
|
+
if (hasFactory) {
|
|
1924
|
+
ops.push(op.raw);
|
|
1925
|
+
continue;
|
|
1926
|
+
}
|
|
1927
|
+
if (op.op === "add") {
|
|
1928
|
+
ops.push({ op: "add", item: op.snapshot ?? (op.item ? snapshotItemForAdded(op.item) : {}) });
|
|
1929
|
+
} else if (op.op === "remove") {
|
|
1930
|
+
const idObj = op.identity && typeof op.identity === "object" ? op.identity : identityToSingleKeyObject(op.identity, col);
|
|
1931
|
+
ops.push({ op: "remove", ...idObj });
|
|
1932
|
+
} else if (op.op === "change" && op.item) {
|
|
1933
|
+
const idObj = getIdentityObject(op.item);
|
|
1934
|
+
const diff = op.diff ?? computeItemDiff(op.item);
|
|
1935
|
+
ops.push({ op: "change", ...idObj, ...diff });
|
|
1936
|
+
}
|
|
1937
|
+
}
|
|
1938
|
+
if (ops.length === 0) return void 0;
|
|
1939
|
+
return { ops };
|
|
1940
|
+
}
|
|
1941
|
+
function identityToSingleKeyObject(identity, col) {
|
|
1942
|
+
const baseline = col._baselineListSnapshot();
|
|
1943
|
+
const sample = baseline.find((i) => i instanceof TrackedObject);
|
|
1944
|
+
const currentSample = col.collection.find(
|
|
1945
|
+
(i) => i instanceof TrackedObject
|
|
1946
|
+
);
|
|
1947
|
+
const proto = sample ? Object.getPrototypeOf(sample) : currentSample ? Object.getPrototypeOf(currentSample) : void 0;
|
|
1948
|
+
if (!proto) return {};
|
|
1949
|
+
const props = getIdentityProperties(proto);
|
|
1950
|
+
if (props.length === 1) return { [props[0]]: identity };
|
|
1951
|
+
return identity && typeof identity === "object" ? identity : {};
|
|
1952
|
+
}
|
|
1953
|
+
function computeBucketedSlot(col, tracker) {
|
|
1954
|
+
const added = [];
|
|
1955
|
+
const changed = [];
|
|
1956
|
+
const removed = [];
|
|
1957
|
+
const isPrimitive = !hasTrackedObjectItems(col);
|
|
1958
|
+
for (const item of col.collection) {
|
|
1959
|
+
if (item instanceof TrackedObject) {
|
|
1960
|
+
if (item.state === "Insert" /* Insert */) {
|
|
1961
|
+
added.push(snapshotItemForAdded(item));
|
|
1962
|
+
} else if (item.state === "Changed" /* Changed */) {
|
|
1963
|
+
const entry = buildChangedEntry(item);
|
|
1964
|
+
if (entry) changed.push(entry);
|
|
1965
|
+
}
|
|
1966
|
+
}
|
|
1967
|
+
}
|
|
1968
|
+
if (isPrimitive) {
|
|
1969
|
+
for (const item of col.collection) {
|
|
1970
|
+
if (!col._isInBaseline(item)) added.push(item);
|
|
1971
|
+
}
|
|
1972
|
+
for (const baselineItem of col._baselineListSnapshot()) {
|
|
1973
|
+
if (!col.collection.includes(baselineItem)) removed.push(baselineItem);
|
|
1974
|
+
}
|
|
1975
|
+
} else {
|
|
1976
|
+
for (const obj of tracker.trackedObjects) {
|
|
1977
|
+
if (obj.state !== "Deleted" /* Deleted */) continue;
|
|
1978
|
+
if (getItemOrigin(obj) !== col) continue;
|
|
1979
|
+
const identity = getIdentity(obj);
|
|
1980
|
+
removed.push(identity);
|
|
1981
|
+
}
|
|
1982
|
+
}
|
|
1983
|
+
if (added.length === 0 && changed.length === 0 && removed.length === 0) {
|
|
1984
|
+
return void 0;
|
|
1985
|
+
}
|
|
1986
|
+
const slot = { added, removed };
|
|
1987
|
+
if (!isPrimitive) slot.changed = changed;
|
|
1988
|
+
return slot;
|
|
1989
|
+
}
|
|
1990
|
+
function hasTrackedObjectItems(col) {
|
|
1991
|
+
for (const item of col.collection) {
|
|
1992
|
+
if (item instanceof TrackedObject) return true;
|
|
1993
|
+
}
|
|
1994
|
+
for (const item of col._baselineListSnapshot()) {
|
|
1995
|
+
if (item instanceof TrackedObject) return true;
|
|
1996
|
+
}
|
|
1997
|
+
return false;
|
|
1998
|
+
}
|
|
1999
|
+
function snapshotItemForAdded(item) {
|
|
2000
|
+
const proto = Object.getPrototypeOf(item);
|
|
2001
|
+
const meta = getEventMetadata(proto);
|
|
2002
|
+
const idProps = getIdentityProperties(proto);
|
|
2003
|
+
const autoIdProp = getAutoIdProperty(proto);
|
|
2004
|
+
const snapshot = {};
|
|
2005
|
+
const rec = item;
|
|
2006
|
+
for (const idProp of idProps) {
|
|
2007
|
+
if (idProp === autoIdProp) {
|
|
2008
|
+
snapshot[idProp] = null;
|
|
2009
|
+
} else {
|
|
2010
|
+
snapshot[idProp] = rec[idProp];
|
|
2011
|
+
}
|
|
2012
|
+
}
|
|
2013
|
+
for (const [propName] of meta) {
|
|
2014
|
+
if (idProps.includes(propName)) continue;
|
|
2015
|
+
snapshot[propName] = normalizeValue(rec[propName]);
|
|
2016
|
+
}
|
|
2017
|
+
return snapshot;
|
|
2018
|
+
}
|
|
2019
|
+
function buildChangedEntry(item) {
|
|
2020
|
+
const state = getEventState(item);
|
|
2021
|
+
const history = getHistoryState(item);
|
|
2022
|
+
if (state.size === 0 && history.size === 0) return void 0;
|
|
2023
|
+
const proto = Object.getPrototypeOf(item);
|
|
2024
|
+
const idProps = getIdentityProperties(proto);
|
|
2025
|
+
const rec = item;
|
|
2026
|
+
const entry = {};
|
|
2027
|
+
for (const idProp of idProps) {
|
|
2028
|
+
entry[idProp] = rec[idProp];
|
|
2029
|
+
}
|
|
2030
|
+
for (const [propName, e] of state) {
|
|
2031
|
+
if (idProps.includes(propName)) continue;
|
|
2032
|
+
entry[propName] = normalizeValue(e.currentValue);
|
|
2033
|
+
}
|
|
2034
|
+
for (const [propName, chain] of history) {
|
|
2035
|
+
if (idProps.includes(propName)) continue;
|
|
2036
|
+
entry[propName] = [...chain];
|
|
2037
|
+
}
|
|
2038
|
+
return entry;
|
|
2039
|
+
}
|
|
2040
|
+
function computeItemDiff(item) {
|
|
2041
|
+
const state = getEventState(item);
|
|
2042
|
+
const proto = Object.getPrototypeOf(item);
|
|
2043
|
+
const idProps = getIdentityProperties(proto);
|
|
2044
|
+
const diff = {};
|
|
2045
|
+
for (const [propName, e] of state) {
|
|
2046
|
+
if (idProps.includes(propName)) continue;
|
|
2047
|
+
diff[propName] = normalizeValue(e.currentValue);
|
|
2048
|
+
}
|
|
2049
|
+
return diff;
|
|
2050
|
+
}
|
|
2051
|
+
function normalizeValue(value) {
|
|
2052
|
+
return value === void 0 ? null : value;
|
|
2053
|
+
}
|
|
2054
|
+
|
|
2055
|
+
// src/EventTracked.ts
|
|
2056
|
+
function EventTracked(validator, onChange, options) {
|
|
2057
|
+
const trackedDecorator = Tracked(validator, onChange, {
|
|
2058
|
+
coalesceWithin: options?.coalesceWithin
|
|
2059
|
+
});
|
|
2060
|
+
function decorator(target, context) {
|
|
2061
|
+
const result = trackedDecorator(target, context);
|
|
2062
|
+
const propertyName = String(context.name);
|
|
2063
|
+
context.addInitializer(function() {
|
|
2064
|
+
registerEventProperty(Object.getPrototypeOf(this), propertyName, {
|
|
2065
|
+
eventType: options?.eventType,
|
|
2066
|
+
history: options?.history,
|
|
2067
|
+
coalesceWithin: options?.coalesceWithin
|
|
2068
|
+
});
|
|
2069
|
+
ensureEventStateSubscription(this);
|
|
2070
|
+
});
|
|
2071
|
+
return result;
|
|
2072
|
+
}
|
|
2073
|
+
return decorator;
|
|
2074
|
+
}
|
|
1331
2075
|
export {
|
|
1332
2076
|
AutoId,
|
|
2077
|
+
EventTracked,
|
|
2078
|
+
EventTrackedCollection,
|
|
2079
|
+
EventTracker,
|
|
2080
|
+
Id,
|
|
1333
2081
|
State,
|
|
1334
2082
|
Tracked,
|
|
1335
2083
|
TrackedCollection,
|
|
@@ -1337,6 +2085,9 @@ export {
|
|
|
1337
2085
|
TrackedObject,
|
|
1338
2086
|
Tracker,
|
|
1339
2087
|
TrackerSession,
|
|
1340
|
-
TypedEvent
|
|
2088
|
+
TypedEvent,
|
|
2089
|
+
getIdentity,
|
|
2090
|
+
getIdentityObject,
|
|
2091
|
+
getIdentityProperties
|
|
1341
2092
|
};
|
|
1342
2093
|
//# sourceMappingURL=index.js.map
|