Skip to content

Commit 0a42f1b

Browse files
committed
feat: add resetStore
1 parent eccf1c3 commit 0a42f1b

File tree

5 files changed

+56
-21
lines changed

5 files changed

+56
-21
lines changed

packages/store/src/core/functions.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Manager from './store';
2+
3+
import type { SetStateAction } from 'react';
4+
import type { Options, State } from '../@types';
5+
6+
export function createStore(options?: Options): void {
7+
return Manager.store(options);
8+
}
9+
10+
export function setStore<StateType extends State = State>(
11+
item: SetStateAction<StateType>
12+
) {
13+
return Manager.set(item);
14+
}
15+
16+
export function getStore<StateType = any>(item?: string): StateType {
17+
return Manager.get(item);
18+
}
19+
20+
export function resetStore() {
21+
return Manager.reset();
22+
}

packages/store/src/core/hooks.ts

+2-15
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import useForceUpdate from 'use-force-update';
2-
import { SetStateAction, useEffect } from 'react';
32

43
import Manager from './store';
54

6-
import { Options, State } from '../@types/core';
7-
import { SetStoreFn, Reducer } from '../@types/functions';
8-
9-
export function createStore(options?: Options): void {
10-
return Manager.store(options);
11-
}
5+
import type { State } from '../@types/core';
6+
import type { SetStoreFn, Reducer } from '../@types/functions';
127

138
export function useStore<StateType = any>(): [State, SetStoreFn<State>];
149
export function useStore<StateType = any>(
@@ -87,12 +82,4 @@ export function useStaticReducer<PayloadType = any>(
8782
return Manager.useReducer<PayloadType>(reducer);
8883
}
8984

90-
export function setStore<StateType extends State = State>(
91-
item: SetStateAction<StateType>
92-
) {
93-
return Manager.set(item);
94-
}
9585

96-
export function getStore<StateType = any>(item?: string): StateType {
97-
return Manager.get(item);
98-
}

packages/store/src/core/store.ts

+8
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,14 @@ export default class Manager {
182182
return runner<PayloadType>(reducerFunction(reducer.name));
183183
}
184184

185+
/**
186+
* Reset state to it's default value
187+
*/
188+
public static reset() {
189+
this.applyPluginHook('onReset', this._options.defaultState);
190+
this.set(this._options.defaultState);
191+
}
192+
185193
protected static _pluginInit(plugins: Plugin[]) {
186194
plugins.forEach((plugin: any) => {
187195
/**

packages/store/src/index.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ export * from './@types';
22
export * from './plugins/';
33

44
export {
5-
createStore,
6-
setStore,
7-
getStore,
85
useStore,
96
useStaticStore,
107
useReducer,
118
useStaticReducer,
129
} from './core/hooks';
10+
11+
export {
12+
createStore,
13+
setStore,
14+
getStore,
15+
resetStore,
16+
} from './core/functions';

pnpm-lock.yaml

+17-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)