Skip to content

Commit

Permalink
chore: update store
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Nov 19, 2023
1 parent eafc6d4 commit c1fceee
Show file tree
Hide file tree
Showing 4 changed files with 1,061 additions and 15 deletions.
6 changes: 6 additions & 0 deletions packages/store/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ createStore({
},
},
});
```

In your components:

```js
import { useStore, useReducer } from '@hanabira/store';

/**
* Use the store in your components.
Expand Down
5 changes: 4 additions & 1 deletion packages/store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
},
"devDependencies": {
"@hanabira/config": "workspace:*",
"@types/react": "^18.2.0",
"@types/react-dom": "^18.2.0",
"eslint": "^7.32.0",
"@hanabira/config": "workspace:*",
"react": "^17.0.2",
"tsup": "^6.0.1",
"typescript": "^5.2.2"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"use-force-update": "^1.0.11"
}
}
28 changes: 26 additions & 2 deletions packages/store/src/core/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { SetStateAction } from 'react';
import useForceUpdate from 'use-force-update';

import Manager from './store';

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

export function createStore(options?: Options): void {
return Manager.store(options);
Expand All @@ -18,7 +20,29 @@ export function useStore<StateType = any>(
item?: string,
callback?: (value: StateType) => any
): [StateType, SetStoreFn<StateType>] {
Manager
const forceUpdate = useForceUpdate();
const removeForceUpdateListener = (): void => {
// Manager.removePropertyListener(forceUpdate);
};

const state = Manager.get(item);

const stateSetter: SetStoreFn<StateType> = (value) => {
if (typeof value === 'function') {
const callableState = value as (prevState: StateType) => State;
Manager.set(callableState(state));
} else {
Manager.set(value);
}

Manager.applyPluginHook('onSave', Manager.get());
};

// If this component ever updates or unmounts, remove the force update
// listener.
// useEffect((): VoidFunction => removeForceUpdateListener, []);

return [state, stateSetter];
}

export function useStaticStore<StateType = any>(): [State, SetStoreFn<State>];
Expand Down
Loading

0 comments on commit c1fceee

Please sign in to comment.