Skip to content

Commit f934667

Browse files
committed
Use in place of selector list
This will help create compact selectors
1 parent 1481a7d commit f934667

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

lib/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export type { SetterArgType, SetStateAction, Plugin } from "./utils";
2323
const useRGS = <T>(
2424
key: string,
2525
value?: ValueType<T>,
26-
...fields: (keyof T)[]
26+
includeRegExp?: RegExp | null,
27+
excludeRegExp?: RegExp,
2728
): [T, SetStateAction<T>] => {
2829
/** Initialize the named store when invoked for the first time. */
2930
if (!globalRGS[key])
@@ -38,7 +39,7 @@ const useRGS = <T>(
3839
},
3940
};
4041

41-
return createHook<T>(key, fields);
42+
return createHook<T>(key, includeRegExp, excludeRegExp);
4243
};
4344

4445
export { useRGS };

lib/src/utils.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { useSyncExternalStore } from "react";
22

3-
export type Selector = string | number | Symbol;
43
type Listener = () => void;
5-
type ListenerWithSelectors = { l: Listener; s: Selector[] };
4+
type ListenerWithSelectors = {
5+
l: Listener;
6+
s: [includeRegExp?: RegExp | null, excludeRegExp?: RegExp];
7+
};
68

79
export type SetterArgType<T> = T | ((prevState: T) => T);
810
export type SetStateAction<T> = (value: SetterArgType<T>) => void;
@@ -29,21 +31,35 @@ export const globalRGS = globalThisForBetterMinification.rgs;
2931

3032
/** trigger all listeners */
3133
export const triggerListeners = <T>(rgs: RGS, oldV: T, newV: T) => {
32-
const updatedFiels: Selector[] = [];
34+
const updatedFiels: string[] = [];
3335
// no need to test this --- it will automatically fail
3436
// if (typeof oldV === "object" && typeof rgs.v === "object")
3537
for (const key in oldV) if (oldV[key] !== newV[key]) updatedFiels.push(key);
36-
rgs.l.forEach(({ l, s }) => (!s.length || s.some(filed => updatedFiels.includes(filed))) && l());
38+
// const testStr = updatedFiels.join("; ");
39+
rgs.l.forEach(
40+
({ l, s: [includeRegExp, excludeRegExp] }) =>
41+
updatedFiels.filter(
42+
s =>
43+
(!includeRegExp || includeRegExp.test(s)) && (!excludeRegExp || !excludeRegExp.test(s)),
44+
).length && l(),
45+
);
3746
};
3847

3948
/** Extract coomon create hook logic to utils */
40-
export const createHook = <T>(key: string, fields: (keyof T)[]): [T, SetStateAction<T>] => {
49+
export const createHook = <T>(
50+
key: string,
51+
includeRegExp?: RegExp | null,
52+
excludeRegExp?: RegExp,
53+
): [T, SetStateAction<T>] => {
4154
const rgs = globalRGS[key] as RGS;
4255
/** This function is called by react to get the current stored value. */
4356
const getSnapshot = () => rgs.v as T;
4457
const val = useSyncExternalStore<T>(
4558
listener => {
46-
const listenerWithSelectors = { l: listener, s: fields };
59+
const listenerWithSelectors = {
60+
l: listener,
61+
s: [includeRegExp, excludeRegExp],
62+
} as ListenerWithSelectors;
4763
rgs.l.push(listenerWithSelectors);
4864
return () => {
4965
rgs.l = rgs.l.filter(l => l !== listenerWithSelectors);
@@ -133,8 +149,9 @@ export const useRGSWithPlugins = <T>(
133149
value?: ValueType<T>,
134150
plugins?: Plugin<T>[],
135151
doNotInit = false,
136-
...fields: (keyof T)[]
152+
includeRegExp?: RegExp | null,
153+
excludeRegExp?: RegExp,
137154
): [T, SetStateAction<T>] => {
138155
if (!globalRGS[key]?.s) initWithPlugins(key, value, plugins, doNotInit);
139-
return createHook<T>(key, fields);
156+
return createHook<T>(key, includeRegExp, excludeRegExp);
140157
};

0 commit comments

Comments
 (0)