Skip to content

Commit 66b64f6

Browse files
committed
clean up
1 parent 0e33dfe commit 66b64f6

File tree

2 files changed

+21
-29
lines changed

2 files changed

+21
-29
lines changed

lib/src/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable @typescript-eslint/non-nullable-type-assertion-style -- as ! operator is forbidden by eslint*/
2-
import { createHook, createSetter, createSubcriber, globalRGS } from "./utils";
2+
import { createHook, globalRGS, triggerListeners } from "./utils";
33

4-
import type { SetStateAction, ValueType } from "./utils";
4+
import type { RGS, SetStateAction, ValueType } from "./utils";
55

66
export type { SetterArgType, SetStateAction, Plugin } from "./utils";
77

@@ -30,7 +30,12 @@ const useRGS = <T>(
3030
globalRGS[key] = {
3131
v: value instanceof Function ? value() : value,
3232
l: [],
33-
s: createSetter(key),
33+
s: val => {
34+
const rgs = globalRGS[key] as RGS;
35+
const oldV = rgs.v as T;
36+
rgs.v = val instanceof Function ? val(oldV) : val;
37+
triggerListeners(rgs, oldV, rgs.v);
38+
},
3439
};
3540

3641
return createHook<T>(key, fields);

lib/src/utils.ts

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { useSyncExternalStore } from "react";
22

33
export type Selector = string | number | Symbol;
44
type Listener = () => void;
5-
type Subscriber = (l: Listener) => () => void;
65
type ListenerWithSelectors = { l: Listener; s: Selector[] };
76

87
export type SetterArgType<T> = T | ((prevState: T) => T);
@@ -12,7 +11,7 @@ export type ValueType<T> = T | (() => T);
1211
/**
1312
* This is a hack to reduce lib size + readability + not encouraging direct access to globalThis
1413
*/
15-
type RGS = {
14+
export type RGS = {
1615
v: unknown;
1716
l: ListenerWithSelectors[];
1817
s: SetStateAction<unknown> | null;
@@ -29,42 +28,30 @@ if (!globalThisForBetterMinification.rgs) globalThisForBetterMinification.rgs =
2928
export const globalRGS = globalThisForBetterMinification.rgs;
3029

3130
/** trigger all listeners */
32-
const triggerListeners = <T>(rgs: RGS, oldV: T, newV: T) => {
31+
export const triggerListeners = <T>(rgs: RGS, oldV: T, newV: T) => {
3332
const updatedFiels: Selector[] = [];
3433
// no need to test this --- it will automatically fail
3534
// if (typeof oldV === "object" && typeof rgs.v === "object")
3635
for (const key in oldV) if (oldV[key] !== newV[key]) updatedFiels.push(key);
3736
rgs.l.forEach(({ l, s }) => (!s.length || s.some(filed => updatedFiels.includes(filed))) && l());
3837
};
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-
};
6038

6139
/** Extract coomon create hook logic to utils */
6240
export const createHook = <T>(key: string, fields: (keyof T)[]): [T, SetStateAction<T>] => {
6341
const rgs = globalRGS[key] as RGS;
6442
/** This function is called by react to get the current stored value. */
6543
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+
);
6855
return [val, rgs.s as SetStateAction<T>];
6956
};
7057

0 commit comments

Comments
 (0)