@katn30/trakr 3.0.0 → 4.1.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.
@@ -216,11 +216,15 @@ declare global {
216
216
  }
217
217
  }
218
218
 
219
- interface IdAssignment {
219
+ interface IdAssignment<V = number> {
220
220
  trackingId: number;
221
- value: number;
221
+ value: V;
222
222
  }
223
223
  declare function AutoId<This extends object, Value>(_target: undefined, context: ClassFieldDecoratorContext<This, Value>): void;
224
+ declare function Id<This extends object, Value>(_target: undefined, context: ClassFieldDecoratorContext<This, Value>): void;
225
+ declare function getIdentityProperties(proto: object): string[];
226
+ declare function getIdentity(obj: object): unknown;
227
+ declare function getIdentityObject(obj: object): Record<string, unknown>;
224
228
 
225
229
  interface StateTarget {
226
230
  trackingId: number;
@@ -261,7 +265,7 @@ declare abstract class TrackedObject implements ITracked, StateTarget {
261
265
  protected set dirtyCounter(value: number);
262
266
  constructor(tracker: Tracker);
263
267
  /** @internal */
264
- _onCommitted(lastOp?: Operation, keys?: IdAssignment[]): void;
268
+ _onCommitted(lastOp?: Operation, keys?: IdAssignment<unknown>[]): void;
265
269
  /** @internal */
266
270
  _markRemoved(): void;
267
271
  /** @internal */
@@ -394,7 +398,8 @@ declare class Tracker implements ITrackerContext {
394
398
  private lastActionIsRecent;
395
399
  /** @internal */
396
400
  _nextTrackingId(): number;
397
- onCommit(keys?: IdAssignment[]): void;
401
+ onCommit<V = number>(keys?: IdAssignment<V>[]): void;
402
+ getByTrackingId(trackingId: number): TrackedObject | undefined;
398
403
  /** @internal */
399
404
  _isInUndoStack(op: Operation): boolean;
400
405
  private reset;
@@ -418,7 +423,7 @@ interface GeneratedEvent<TEventType extends string = string, TPayload = Record<s
418
423
  eventType: TEventType;
419
424
  payload: TPayload;
420
425
  trackingId?: number;
421
- targetId?: number;
426
+ targetId?: unknown;
422
427
  }
423
428
  interface EventLifecycleOptions<TEventType extends string = string> {
424
429
  itemAdded?: TEventType;
@@ -426,23 +431,79 @@ interface EventLifecycleOptions<TEventType extends string = string> {
426
431
  }
427
432
 
428
433
  declare class EventTracker extends Tracker {
429
- onCommit(keys?: IdAssignment[]): void;
434
+ withContext<T>(ctx: unknown, action: () => T): T;
435
+ onCommit<V = number>(keys?: IdAssignment<V>[]): void;
430
436
  generateEvents<TEventType extends string = string>(): GeneratedEvent<TEventType>[];
431
437
  }
432
438
 
433
- declare function EventTracked(eventType: string, validator?: (self: any, newValue: any) => string | undefined, onChange?: (self: any, newValue: any, oldValue: any) => void, options?: {
439
+ interface HistoryOptions<TSelf = any, TValue = any> {
440
+ entryFactory: (self: TSelf, newValue: TValue, oldValue: TValue, change: Change, ctx?: unknown) => unknown;
441
+ }
442
+ type HistoryConfig = boolean | HistoryOptions;
443
+ interface EventPropertyOptions {
444
+ eventType?: string;
445
+ history?: HistoryConfig;
434
446
  coalesceWithin?: number;
435
- }): {
447
+ }
448
+
449
+ interface EventTrackedOptions {
450
+ coalesceWithin?: number;
451
+ eventType?: string;
452
+ history?: HistoryConfig;
453
+ }
454
+ declare function EventTracked(validator?: (self: any, newValue: any) => string | undefined, onChange?: (self: any, newValue: any, oldValue: any) => void, options?: EventTrackedOptions): {
436
455
  <T extends TrackedObject, V>(target: ClassAccessorDecoratorTarget<T, V>, context: ClassAccessorDecoratorContext<T, V>): ClassAccessorDecoratorResult<T, V>;
437
456
  <T extends TrackedObject, V>(target: (this: T, value: V) => void, context: ClassSetterDecoratorContext<T, V>): (this: T, value: V) => void;
438
457
  <T extends TrackedObject, V>(target: (this: T) => V, context: ClassGetterDecoratorContext<T, V>): (this: T) => V;
439
458
  };
440
459
 
460
+ interface CollectionHistoryOptions<T = any> {
461
+ entryFactory: (self: T, change: Change, ctx?: unknown) => unknown;
462
+ }
463
+ type CollectionHistoryConfig = boolean | CollectionHistoryOptions;
464
+ interface EventTrackedCollectionOptions<T = any, TEventType extends string = string> extends EventLifecycleOptions<TEventType> {
465
+ eventType?: TEventType;
466
+ history?: CollectionHistoryConfig;
467
+ owner?: {
468
+ object: TrackedObject;
469
+ property: string;
470
+ };
471
+ }
472
+ interface CollectionOp {
473
+ op: "add" | "remove" | "change";
474
+ item?: TrackedObject;
475
+ identity?: unknown;
476
+ diff?: Record<string, unknown>;
477
+ raw?: unknown;
478
+ snapshot?: Record<string, unknown>;
479
+ }
441
480
  declare class EventTrackedCollection<T, TEventType extends string = string> extends TrackedCollection<T> {
442
481
  private readonly _eventOptions;
443
- constructor(tracker: Tracker, items?: T[], validator?: (value: T[]) => string | undefined, eventOptions?: EventLifecycleOptions<TEventType>);
482
+ private readonly _historyOps;
483
+ private readonly _baselineItems;
484
+ constructor(tracker: Tracker, items?: T[], validator?: (value: T[]) => string | undefined, options?: EventTrackedCollectionOptions<T, TEventType>);
444
485
  /** @internal */
445
486
  get _lifecycleOptions(): EventLifecycleOptions<TEventType> | undefined;
487
+ /** @internal */
488
+ get _aggregateOptions(): EventTrackedCollectionOptions<T, TEventType> | undefined;
489
+ /** @internal */
490
+ get _historyOpsList(): CollectionOp[];
491
+ /** @internal */
492
+ _clearHistoryOps(): void;
493
+ /** @internal */
494
+ _isInBaseline(item: T): boolean;
495
+ /** @internal */
496
+ _baselineListSnapshot(): T[];
497
+ /** @internal */
498
+ _rebaseline(): void;
499
+ private _isAggregateMode;
500
+ private _isHistoryMode;
501
+ private _validateItemHasIdentity;
502
+ private _attachItemChangeWatchers;
503
+ private _recordAddOp;
504
+ private _recordRemoveOp;
505
+ private _recordChangeOpDiff;
506
+ private _recordChangeOp;
446
507
  }
447
508
 
448
- export { AutoId, type EventLifecycleOptions, EventTracked, EventTrackedCollection, EventTracker, type GeneratedEvent, type ITracked, type IdAssignment, type PropertyScope, State, Tracked, TrackedCollection, TrackedCollectionChanged, TrackedObject, type TrackedPropertyChanged, Tracker, TrackerSession, TypedEvent };
509
+ export { AutoId, type CollectionHistoryConfig, type CollectionHistoryOptions, type EventLifecycleOptions, type EventPropertyOptions, EventTracked, EventTrackedCollection, type EventTrackedCollectionOptions, type EventTrackedOptions, EventTracker, type GeneratedEvent, type HistoryConfig, type HistoryOptions, type ITracked, Id, type IdAssignment, type PropertyScope, State, Tracked, TrackedCollection, TrackedCollectionChanged, TrackedObject, type TrackedPropertyChanged, Tracker, TrackerSession, TypedEvent, getIdentity, getIdentityObject, getIdentityProperties };
@@ -216,11 +216,15 @@ declare global {
216
216
  }
217
217
  }
218
218
 
219
- interface IdAssignment {
219
+ interface IdAssignment<V = number> {
220
220
  trackingId: number;
221
- value: number;
221
+ value: V;
222
222
  }
223
223
  declare function AutoId<This extends object, Value>(_target: undefined, context: ClassFieldDecoratorContext<This, Value>): void;
224
+ declare function Id<This extends object, Value>(_target: undefined, context: ClassFieldDecoratorContext<This, Value>): void;
225
+ declare function getIdentityProperties(proto: object): string[];
226
+ declare function getIdentity(obj: object): unknown;
227
+ declare function getIdentityObject(obj: object): Record<string, unknown>;
224
228
 
225
229
  interface StateTarget {
226
230
  trackingId: number;
@@ -261,7 +265,7 @@ declare abstract class TrackedObject implements ITracked, StateTarget {
261
265
  protected set dirtyCounter(value: number);
262
266
  constructor(tracker: Tracker);
263
267
  /** @internal */
264
- _onCommitted(lastOp?: Operation, keys?: IdAssignment[]): void;
268
+ _onCommitted(lastOp?: Operation, keys?: IdAssignment<unknown>[]): void;
265
269
  /** @internal */
266
270
  _markRemoved(): void;
267
271
  /** @internal */
@@ -394,7 +398,8 @@ declare class Tracker implements ITrackerContext {
394
398
  private lastActionIsRecent;
395
399
  /** @internal */
396
400
  _nextTrackingId(): number;
397
- onCommit(keys?: IdAssignment[]): void;
401
+ onCommit<V = number>(keys?: IdAssignment<V>[]): void;
402
+ getByTrackingId(trackingId: number): TrackedObject | undefined;
398
403
  /** @internal */
399
404
  _isInUndoStack(op: Operation): boolean;
400
405
  private reset;
@@ -418,7 +423,7 @@ interface GeneratedEvent<TEventType extends string = string, TPayload = Record<s
418
423
  eventType: TEventType;
419
424
  payload: TPayload;
420
425
  trackingId?: number;
421
- targetId?: number;
426
+ targetId?: unknown;
422
427
  }
423
428
  interface EventLifecycleOptions<TEventType extends string = string> {
424
429
  itemAdded?: TEventType;
@@ -426,23 +431,79 @@ interface EventLifecycleOptions<TEventType extends string = string> {
426
431
  }
427
432
 
428
433
  declare class EventTracker extends Tracker {
429
- onCommit(keys?: IdAssignment[]): void;
434
+ withContext<T>(ctx: unknown, action: () => T): T;
435
+ onCommit<V = number>(keys?: IdAssignment<V>[]): void;
430
436
  generateEvents<TEventType extends string = string>(): GeneratedEvent<TEventType>[];
431
437
  }
432
438
 
433
- declare function EventTracked(eventType: string, validator?: (self: any, newValue: any) => string | undefined, onChange?: (self: any, newValue: any, oldValue: any) => void, options?: {
439
+ interface HistoryOptions<TSelf = any, TValue = any> {
440
+ entryFactory: (self: TSelf, newValue: TValue, oldValue: TValue, change: Change, ctx?: unknown) => unknown;
441
+ }
442
+ type HistoryConfig = boolean | HistoryOptions;
443
+ interface EventPropertyOptions {
444
+ eventType?: string;
445
+ history?: HistoryConfig;
434
446
  coalesceWithin?: number;
435
- }): {
447
+ }
448
+
449
+ interface EventTrackedOptions {
450
+ coalesceWithin?: number;
451
+ eventType?: string;
452
+ history?: HistoryConfig;
453
+ }
454
+ declare function EventTracked(validator?: (self: any, newValue: any) => string | undefined, onChange?: (self: any, newValue: any, oldValue: any) => void, options?: EventTrackedOptions): {
436
455
  <T extends TrackedObject, V>(target: ClassAccessorDecoratorTarget<T, V>, context: ClassAccessorDecoratorContext<T, V>): ClassAccessorDecoratorResult<T, V>;
437
456
  <T extends TrackedObject, V>(target: (this: T, value: V) => void, context: ClassSetterDecoratorContext<T, V>): (this: T, value: V) => void;
438
457
  <T extends TrackedObject, V>(target: (this: T) => V, context: ClassGetterDecoratorContext<T, V>): (this: T) => V;
439
458
  };
440
459
 
460
+ interface CollectionHistoryOptions<T = any> {
461
+ entryFactory: (self: T, change: Change, ctx?: unknown) => unknown;
462
+ }
463
+ type CollectionHistoryConfig = boolean | CollectionHistoryOptions;
464
+ interface EventTrackedCollectionOptions<T = any, TEventType extends string = string> extends EventLifecycleOptions<TEventType> {
465
+ eventType?: TEventType;
466
+ history?: CollectionHistoryConfig;
467
+ owner?: {
468
+ object: TrackedObject;
469
+ property: string;
470
+ };
471
+ }
472
+ interface CollectionOp {
473
+ op: "add" | "remove" | "change";
474
+ item?: TrackedObject;
475
+ identity?: unknown;
476
+ diff?: Record<string, unknown>;
477
+ raw?: unknown;
478
+ snapshot?: Record<string, unknown>;
479
+ }
441
480
  declare class EventTrackedCollection<T, TEventType extends string = string> extends TrackedCollection<T> {
442
481
  private readonly _eventOptions;
443
- constructor(tracker: Tracker, items?: T[], validator?: (value: T[]) => string | undefined, eventOptions?: EventLifecycleOptions<TEventType>);
482
+ private readonly _historyOps;
483
+ private readonly _baselineItems;
484
+ constructor(tracker: Tracker, items?: T[], validator?: (value: T[]) => string | undefined, options?: EventTrackedCollectionOptions<T, TEventType>);
444
485
  /** @internal */
445
486
  get _lifecycleOptions(): EventLifecycleOptions<TEventType> | undefined;
487
+ /** @internal */
488
+ get _aggregateOptions(): EventTrackedCollectionOptions<T, TEventType> | undefined;
489
+ /** @internal */
490
+ get _historyOpsList(): CollectionOp[];
491
+ /** @internal */
492
+ _clearHistoryOps(): void;
493
+ /** @internal */
494
+ _isInBaseline(item: T): boolean;
495
+ /** @internal */
496
+ _baselineListSnapshot(): T[];
497
+ /** @internal */
498
+ _rebaseline(): void;
499
+ private _isAggregateMode;
500
+ private _isHistoryMode;
501
+ private _validateItemHasIdentity;
502
+ private _attachItemChangeWatchers;
503
+ private _recordAddOp;
504
+ private _recordRemoveOp;
505
+ private _recordChangeOpDiff;
506
+ private _recordChangeOp;
446
507
  }
447
508
 
448
- export { AutoId, type EventLifecycleOptions, EventTracked, EventTrackedCollection, EventTracker, type GeneratedEvent, type ITracked, type IdAssignment, type PropertyScope, State, Tracked, TrackedCollection, TrackedCollectionChanged, TrackedObject, type TrackedPropertyChanged, Tracker, TrackerSession, TypedEvent };
509
+ export { AutoId, type CollectionHistoryConfig, type CollectionHistoryOptions, type EventLifecycleOptions, type EventPropertyOptions, EventTracked, EventTrackedCollection, type EventTrackedCollectionOptions, type EventTrackedOptions, EventTracker, type GeneratedEvent, type HistoryConfig, type HistoryOptions, type ITracked, Id, type IdAssignment, type PropertyScope, State, Tracked, TrackedCollection, TrackedCollectionChanged, TrackedObject, type TrackedPropertyChanged, Tracker, TrackerSession, TypedEvent, getIdentity, getIdentityObject, getIdentityProperties };