@foblex/mutator
Version:
Angular Signals state engine with deep patch + undo/redo and notifier.
61 lines (54 loc) • 1.88 kB
TypeScript
import { Signal, InjectionToken, Provider } from '@angular/core';
declare function deepDelete<T>(target: T, patch: DeepDelete<T>): T;
type DeepDelete<T> = {
[K in keyof T]?: T[K] extends object ? DeepDelete<T[K]> | null : null;
};
declare function deepMerge<T>(target: T, patch: DeepPartial<T>): T;
type DeepPartial<T> = {
[K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K];
};
interface IMutatorChange {
version: number;
notifier: string | null;
}
type Changes<T> = {
value: DeepPartial<T>;
action: 'create' | 'update';
} | {
value: DeepDelete<T>;
action: 'delete';
};
declare abstract class Mutator<T extends object> {
private _base;
private readonly _undoStack;
private readonly _redoStack;
private readonly _config;
private readonly _limit;
private readonly _change;
private readonly _canUndo;
private readonly _canRedo;
get changes(): Signal<IMutatorChange>;
get canUndo(): Signal<boolean>;
get canRedo(): Signal<boolean>;
get base(): T;
initialize(value: T): void;
create(patch: DeepPartial<T>, notifier?: string): void;
update(patch: DeepPartial<T>, notifier?: string): void;
delete(patch: DeepDelete<T>, notifier?: string): void;
_addChanges(changes: Changes<T>, notifier: string | null): void;
private _setChangeObject;
getSnapshot(): T;
private _applyChange;
undo(): void;
redo(): void;
patchBase(patch: DeepPartial<T>): void;
deleteFromBase(patch: DeepDelete<T>): void;
private _updateSignals;
}
interface MutatorConfig {
limit?: number;
}
declare const MUTATOR_CONFIG: InjectionToken<MutatorConfig>;
declare function provideMutator(config?: MutatorConfig): Provider[];
export { MUTATOR_CONFIG, Mutator, deepDelete, deepMerge, provideMutator };
export type { DeepDelete, DeepPartial, IMutatorChange, MutatorConfig };