Skip to content

Commit

Permalink
Fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
mayank1513 committed Apr 30, 2024
1 parent 206ffe4 commit 7b71f71
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
22 changes: 15 additions & 7 deletions lib/r18gs/src/with-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { Plugin, SetStateAction } from "./utils";
* @param key - Unique key to identify the store.
* @param value - Initial value of the store.
* @param plugins - Plugins to be applied to the store.
* @returns - A hook funciton that returns a tuple (Ordered sequance of values) containing the state and a function to set the state.
* @returns - A hook function that returns a tuple (Ordered sequence of values) containing the state and a function to set the state.
*/
export function create<T>(
key: string,
Expand All @@ -35,16 +35,24 @@ export function create<T>(
* @returns A hook that automatically initializes the store (if not already initialized) with the given plugins.
*/
export function withPlugins<T>(
plugins?: Plugin<T>[],
): (key: string, value?: T, doNotInit?: boolean) => [T, SetStateAction<T>] {
plugins: Plugin<T>[],
): <U = T>(key: string, value?: U, doNotInit?: boolean) => [U, SetStateAction<U>] {
/**
* todo - this typedoc comments are not visible in IDE suggestions - fix this
* Creates a hook similar to useRGS, with plugins applied on first invocation.
*
* @param key - Unique key to identify the store.
* @param value - Initial value of the store.
* @param doNotInit - @defaultValue false - Do not initialize the store. Useful when you want to initialize the store later. Note that the setter function is not available until the store is initialized.
* @param doNotInit - If true, the store won't be initialized immediately. Defaults to false. Useful when you want to initialize the store later. Note that the setter function is not available until the store is initialized.
* @returns A tuple containing the state value and its setter function.
*/
return (key: string, value?: T, doNotInit = false) =>
useRGSWithPlugins(key, value, plugins, doNotInit);
const hookWithPlugins = <U = T>(
key: string,
value?: U,
doNotInit = false,
): [U, SetStateAction<U>] =>
useRGSWithPlugins(key, value, plugins as unknown as Plugin<U>[], doNotInit);

return hookWithPlugins;
}

export { useRGSWithPlugins };
2 changes: 1 addition & 1 deletion lib/r18gs/tests/create.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const TESTID_DISPLAY2 = "d2";

const useMyRGS = create(COUNTER_RGS_KEY, 0, [persist({ storage: "cookie" })]);

const useMyRGS2 = withPlugins<number>([persist()]);
const useMyRGS2 = withPlugins([persist()]);

function Component1() {
const [count, setCount] = useMyRGS();
Expand Down

0 comments on commit 7b71f71

Please sign in to comment.