Skip to content

Commit

Permalink
feat: add resetStore
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Nov 20, 2023
1 parent eccf1c3 commit 0a42f1b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 21 deletions.
22 changes: 22 additions & 0 deletions packages/store/src/core/functions.ts
@@ -0,0 +1,22 @@
import Manager from './store';

import type { SetStateAction } from 'react';
import type { Options, State } from '../@types';

export function createStore(options?: Options): void {
return Manager.store(options);
}

export function setStore<StateType extends State = State>(
item: SetStateAction<StateType>
) {
return Manager.set(item);
}

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

export function resetStore() {
return Manager.reset();
}
17 changes: 2 additions & 15 deletions packages/store/src/core/hooks.ts
@@ -1,14 +1,9 @@
import useForceUpdate from 'use-force-update';
import { SetStateAction, useEffect } from 'react';

import Manager from './store';

import { Options, State } from '../@types/core';
import { SetStoreFn, Reducer } from '../@types/functions';

export function createStore(options?: Options): void {
return Manager.store(options);
}
import type { State } from '../@types/core';
import type { SetStoreFn, Reducer } from '../@types/functions';

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

export function setStore<StateType extends State = State>(
item: SetStateAction<StateType>
) {
return Manager.set(item);
}

export function getStore<StateType = any>(item?: string): StateType {
return Manager.get(item);
}
8 changes: 8 additions & 0 deletions packages/store/src/core/store.ts
Expand Up @@ -182,6 +182,14 @@ export default class Manager {
return runner<PayloadType>(reducerFunction(reducer.name));
}

/**
* Reset state to it's default value
*/
public static reset() {
this.applyPluginHook('onReset', this._options.defaultState);
this.set(this._options.defaultState);
}

protected static _pluginInit(plugins: Plugin[]) {
plugins.forEach((plugin: any) => {
/**
Expand Down
10 changes: 7 additions & 3 deletions packages/store/src/index.ts
Expand Up @@ -2,11 +2,15 @@ export * from './@types';
export * from './plugins/';

export {
createStore,
setStore,
getStore,
useStore,
useStaticStore,
useReducer,
useStaticReducer,
} from './core/hooks';

export {
createStore,
setStore,
getStore,
resetStore,
} from './core/functions';
20 changes: 17 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0a42f1b

Please sign in to comment.