Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mzkmnk committed Jan 5, 2025
1 parent 1a7091e commit 5c2c2e9
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions projects/ngrx-extension/src/lib/with-history/with-history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { effect } from '@angular/core';
import {
getState,
signalStoreFeature,
withHooks,
withMethods,
} from '@ngrx/signals';

export const STATE_HISTORY = Symbol('STATE_HISTORY');

export type TStateHistory<State> = {
stateVersions: State[][];
currentVersionIndex: number;
currentVersion: number;
};

export function withHistory<State extends object>({
sync = true,
}: { sync: boolean }) {
/** このオブジェクトにstateの変更履歴を保存する */
const stateHistory: { [STATE_HISTORY]: TStateHistory<State> } = {
[STATE_HISTORY]: {
stateVersions: [],
currentVersion: 0,
currentVersionIndex: 0,
},
};

/** この関数内でstateを書き換え場合trueとする */
let dirty = false;

return signalStoreFeature(
withMethods((store) => ({
undo({ cnt = 1 }: { cnt: number }) {},

redo() {
// todo
},
})),
withHooks({
onInit(store) {
// initialize
stateHistory[STATE_HISTORY].currentVersion++;
stateHistory[STATE_HISTORY].stateVersions.push([]);

if (sync) {
effect(() =>
((state) => {
// todo
if (dirty) {
dirty = true;
return;
}
})(getState(store)),
);
}
},
}),
);
}

0 comments on commit 5c2c2e9

Please sign in to comment.