Skip to content

Commit

Permalink
Update doc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mayank1513 committed Dec 18, 2024
1 parent dae7253 commit 3ea6073
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
19 changes: 11 additions & 8 deletions lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ import type { RGS, SetStateAction, ValueType } from "./utils";
export type { SetterArgType, SetStateAction, Plugin } from "./utils";

/**
* Use this hook similar to `useState` hook.
* The difference is that you need to pass a
* unique key - unique across the app to make
* this state accessible to all client components.
* A React hook for managing shared global state, similar to the `useState` hook.
* This hook requires a unique key, which identifies the global store and allows state sharing across all client components.
*
* @example
* ```tsx
* const [state, setState] = useRGS<number>("counter", 1);
* ```
*
* @param key - Unique key to identify the store.
* @param value - Initial value of the store.
* @returns - A tuple (Ordered sequance of values) containing the state and a function to set the state.
* @param key - A unique key to identify the global store.
* @param value - The initial value of the global state. Can be a value or a function returning a value.
* @param includeRegExp - (Optional) A regular expression to specify which fields trigger updates.
* @param excludeRegExp - (Optional) A regular expression to specify which fields should be excluded from updates.
* @returns A tuple containing the current state and a function to update the state.
*
* @see [Learn More](https://r18gs.vercel.app/)
*/
const useRGS = <T>(
key: string,
Expand All @@ -27,7 +29,7 @@ const useRGS = <T>(
excludeRegExp?: RegExp,
): [T, SetStateAction<T>] => {
/** Initialize the named store when invoked for the first time. */
if (!globalRGS[key])
if (!globalRGS[key]) {
globalRGS[key] = {
v: value instanceof Function ? value() : value,
l: [],
Expand All @@ -38,6 +40,7 @@ const useRGS = <T>(
triggerListeners(rgs, oldV, rgs.v);
},
};
}

return createHook<T>(key, includeRegExp, excludeRegExp);
};
Expand Down
5 changes: 5 additions & 0 deletions lib/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ export const useRGSWithPlugins = <T>(
return createHook<T>(key, includeRegExp, excludeRegExp);
};

/**
* Converts a list of selectors into a regular expression.
* @param list - An array of strings representing the fields to match.
* @returns A regular expression that matches any field from the provided list.
*/
export const listToRegExp = (list: string[]) => {
const escapedList = list.map(s => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
return new RegExp(`^(${escapedList.join("|")})$`);
Expand Down

0 comments on commit 3ea6073

Please sign in to comment.