From b8c04d85c04455ae7adff5944019d4bd4f6ed064 Mon Sep 17 00:00:00 2001 From: mzkmnk Date: Thu, 9 Jan 2025 08:57:58 +0900 Subject: [PATCH] chore: * --- .../src/lib/with-history/with-history.ts | 46 ++++++------------- .../with-history/with-history.component.ts | 7 +-- 2 files changed, 19 insertions(+), 34 deletions(-) diff --git a/projects/ngrx-extension/src/lib/with-history/with-history.ts b/projects/ngrx-extension/src/lib/with-history/with-history.ts index 5a344a8e..395f3791 100644 --- a/projects/ngrx-extension/src/lib/with-history/with-history.ts +++ b/projects/ngrx-extension/src/lib/with-history/with-history.ts @@ -1,13 +1,5 @@ import { effect } from '@angular/core'; -import { - type SignalStoreFeature, - type SignalStoreFeatureResult, - getState, - patchState, - signalStoreFeature, - withHooks, - withMethods, -} from '@ngrx/signals'; +import { getState, patchState, signalStoreFeature, withHooks, withMethods } from '@ngrx/signals'; export const STATE_HISTORY = Symbol('STATE_HISTORY'); @@ -16,23 +8,18 @@ export type TStateHistory = { currentVersionIndex: number; }; -export function withHistory({ - maxLength = 100, - sync = true, -}: { +export type Config = { maxLength?: number; sync?: boolean; -}): SignalStoreFeature< - SignalStoreFeatureResult, - { - state: {}; - props: {}; - methods: { - undo: () => void; - redo: () => void; - }; - } -> { +}; + +// todo next reference +// historiesChangeDetFn?: ( +// store: Prettify & Input['props'] & Input['methods'] & WritableSignal>, +// histories: State[], +// ) => void; + +export function withHistory({ maxLength = 100, sync = true }: Config) { /** このオブジェクトにstateの変更履歴を保存する */ const stateHistory: { [STATE_HISTORY]: TStateHistory } = { [STATE_HISTORY]: { @@ -81,15 +68,12 @@ export function withHistory({ patchState(store, stateVersions[currentVersionIndex - 1]); }, - /** 履歴全てを返却する */ - getHistories() { - return stateHistory[STATE_HISTORY].stateVersions; - }, - /** 履歴を削除する */ clearHistories() { - stateHistory[STATE_HISTORY].stateVersions = []; - stateHistory[STATE_HISTORY].currentVersionIndex = 0; + stateHistory[STATE_HISTORY].stateVersions = stateHistory[STATE_HISTORY].stateVersions.filter( + (_, index) => index + 1 === stateHistory[STATE_HISTORY].currentVersionIndex, + ); + stateHistory[STATE_HISTORY].currentVersionIndex = 1; }, })), diff --git a/src/app/pages/with-history/with-history.component.ts b/src/app/pages/with-history/with-history.component.ts index f69fe546..d6fb376a 100644 --- a/src/app/pages/with-history/with-history.component.ts +++ b/src/app/pages/with-history/with-history.component.ts @@ -11,12 +11,11 @@ export type TUser = { export type TUserState = { user: TUser; - histories: TUser[]; }; export const UserSignalStore = signalStore( - withState({ user: { name: 'John Doe', age: 30 }, histories: [] }), - withHistory({}), + withState({ user: { name: 'John Doe', age: 30 } }), + withHistory({}), withMethods((store) => ({ editName(name: string): void { patchStateWithImmer(store, (state) => { @@ -38,6 +37,8 @@ export const UserSignalStore = signalStore( + `,