@@ -2,7 +2,6 @@ import { useSyncExternalStore } from "react";
2
2
3
3
export type Selector = string | number | Symbol ;
4
4
type Listener = ( ) => void ;
5
- type Subscriber = ( l : Listener ) => ( ) => void ;
6
5
type ListenerWithSelectors = { l : Listener ; s : Selector [ ] } ;
7
6
8
7
export type SetterArgType < T > = T | ( ( prevState : T ) => T ) ;
@@ -12,7 +11,7 @@ export type ValueType<T> = T | (() => T);
12
11
/**
13
12
* This is a hack to reduce lib size + readability + not encouraging direct access to globalThis
14
13
*/
15
- type RGS = {
14
+ export type RGS = {
16
15
v : unknown ;
17
16
l : ListenerWithSelectors [ ] ;
18
17
s : SetStateAction < unknown > | null ;
@@ -29,42 +28,30 @@ if (!globalThisForBetterMinification.rgs) globalThisForBetterMinification.rgs =
29
28
export const globalRGS = globalThisForBetterMinification . rgs ;
30
29
31
30
/** trigger all listeners */
32
- const triggerListeners = < T > ( rgs : RGS , oldV : T , newV : T ) => {
31
+ export const triggerListeners = < T > ( rgs : RGS , oldV : T , newV : T ) => {
33
32
const updatedFiels : Selector [ ] = [ ] ;
34
33
// no need to test this --- it will automatically fail
35
34
// if (typeof oldV === "object" && typeof rgs.v === "object")
36
35
for ( const key in oldV ) if ( oldV [ key ] !== newV [ key ] ) updatedFiels . push ( key ) ;
37
36
rgs . l . forEach ( ( { l, s } ) => ( ! s . length || s . some ( filed => updatedFiels . includes ( filed ) ) ) && l ( ) ) ;
38
37
} ;
39
- /** craete subscriber function to subscribe to the store. */
40
- export const createSubcriber = ( key : string , fields : Selector [ ] ) : Subscriber => {
41
- return listener => {
42
- const rgs = globalRGS [ key ] as RGS ;
43
- const listenerWithSelectors = { l : listener , s : fields } ;
44
- ( rgs . l as ListenerWithSelectors [ ] ) . push ( listenerWithSelectors ) ;
45
- return ( ) => {
46
- rgs . l = ( rgs . l as ListenerWithSelectors [ ] ) . filter ( l => l !== listenerWithSelectors ) ;
47
- } ;
48
- } ;
49
- } ;
50
-
51
- /** setter function to set the state. */
52
- export const createSetter = < T > ( key : string ) : SetStateAction < unknown > => {
53
- return val => {
54
- const rgs = globalRGS [ key ] as RGS ;
55
- const oldV = rgs . v as T ;
56
- const newV = ( rgs . v = ( val instanceof Function ? val ( oldV ) : val ) as T ) ;
57
- triggerListeners ( rgs , oldV , newV ) ;
58
- } ;
59
- } ;
60
38
61
39
/** Extract coomon create hook logic to utils */
62
40
export const createHook = < T > ( key : string , fields : ( keyof T ) [ ] ) : [ T , SetStateAction < T > ] => {
63
41
const rgs = globalRGS [ key ] as RGS ;
64
42
/** This function is called by react to get the current stored value. */
65
43
const getSnapshot = ( ) => rgs . v as T ;
66
- const u = createSubcriber ( key , fields ) ;
67
- const val = useSyncExternalStore < T > ( u , getSnapshot , getSnapshot ) ;
44
+ const val = useSyncExternalStore < T > (
45
+ listener => {
46
+ const listenerWithSelectors = { l : listener , s : fields } ;
47
+ rgs . l . push ( listenerWithSelectors ) ;
48
+ return ( ) => {
49
+ rgs . l = rgs . l . filter ( l => l !== listenerWithSelectors ) ;
50
+ } ;
51
+ } ,
52
+ getSnapshot ,
53
+ getSnapshot ,
54
+ ) ;
68
55
return [ val , rgs . s as SetStateAction < T > ] ;
69
56
} ;
70
57
0 commit comments