Skip to content

Commit

Permalink
chore: *
Browse files Browse the repository at this point in the history
  • Loading branch information
mzkmnk committed Jan 8, 2025
1 parent 2540fbe commit b8c04d8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 34 deletions.
46 changes: 15 additions & 31 deletions projects/ngrx-extension/src/lib/with-history/with-history.ts
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -16,23 +8,18 @@ export type TStateHistory<State> = {
currentVersionIndex: number;
};

export function withHistory<State extends object>({
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<StateSignals<Input['state']> & Input['props'] & Input['methods'] & WritableSignal<Input['state']>>,
// histories: State[],
// ) => void;

export function withHistory<State extends object>({ maxLength = 100, sync = true }: Config) {
/** このオブジェクトにstateの変更履歴を保存する */
const stateHistory: { [STATE_HISTORY]: TStateHistory<State> } = {
[STATE_HISTORY]: {
Expand Down Expand Up @@ -81,15 +68,12 @@ export function withHistory<State extends object>({
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;
},
})),

Expand Down
7 changes: 4 additions & 3 deletions src/app/pages/with-history/with-history.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ export type TUser = {

export type TUserState = {
user: TUser;
histories: TUser[];
};

export const UserSignalStore = signalStore(
withState<TUserState>({ user: { name: 'John Doe', age: 30 }, histories: [] }),
withHistory<TUserState>({}),
withState<TUserState>({ user: { name: 'John Doe', age: 30 } }),
withHistory({}),
withMethods((store) => ({
editName(name: string): void {
patchStateWithImmer(store, (state) => {
Expand All @@ -38,6 +37,8 @@ export const UserSignalStore = signalStore(
<button (click)="editName()" class="bg-green-500 rounded-lg p-2 text-white">change name</button>
<button (click)="userSignalStore.undo()" class="bg-red-500 rounded-lg p-2 text-white">undo</button>
<button (click)="userSignalStore.redo()" class="bg-blue-500 rounded-lg p-2 text-white">redo</button>
<button (click)="userSignalStore.clearHistories()" class="bg-purple-500 rounded-lg p-2 text-white">clear
</button>
</div>
</div>
`,
Expand Down

0 comments on commit b8c04d8

Please sign in to comment.