@tamagui/use-store 1.52.7 → 1.52.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/helpers.js +5 -26
- package/dist/cjs/helpers.js.map +1 -1
- package/dist/cjs/index.js +2 -4
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/observe.js +159 -0
- package/dist/cjs/observe.js.map +6 -0
- package/dist/cjs/reaction.js +0 -80
- package/dist/cjs/reaction.js.map +2 -2
- package/dist/cjs/selector.js +64 -9
- package/dist/cjs/selector.js.map +1 -1
- package/dist/cjs/useStore.js +16 -13
- package/dist/cjs/useStore.js.map +1 -1
- package/dist/esm/helpers.js +5 -25
- package/dist/esm/helpers.js.map +1 -1
- package/dist/esm/index.js +1 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/observe.js +134 -0
- package/dist/esm/observe.js.map +6 -0
- package/dist/esm/reaction.js +0 -56
- package/dist/esm/reaction.js.map +2 -2
- package/dist/esm/selector.js +62 -8
- package/dist/esm/selector.js.map +1 -1
- package/dist/esm/useStore.js +15 -13
- package/dist/esm/useStore.js.map +1 -1
- package/package.json +5 -2
- package/src/helpers.tsx +7 -30
- package/src/index.ts +1 -2
- package/src/interfaces.tsx +2 -1
- package/src/{selector.tsx → observe.tsx} +73 -50
- package/src/useStore.tsx +19 -13
- package/types/helpers.d.ts +1 -2
- package/types/helpers.d.ts.map +1 -1
- package/types/index.d.ts +1 -2
- package/types/index.d.ts.map +1 -1
- package/types/interfaces.d.ts +2 -1
- package/types/interfaces.d.ts.map +1 -1
- package/types/observe.d.ts +6 -0
- package/types/observe.d.ts.map +1 -0
- package/types/reaction.d.ts +0 -3
- package/types/reaction.d.ts.map +1 -1
- package/types/selector.d.ts +5 -1
- package/types/selector.d.ts.map +1 -1
- package/types/useStore.d.ts +2 -0
- package/types/useStore.d.ts.map +1 -1
- package/src/reaction.tsx +0 -112
|
@@ -5,8 +5,6 @@ import { UNWRAP_PROXY } from './constants'
|
|
|
5
5
|
import { StoreInfo } from './interfaces'
|
|
6
6
|
import { trackStoresAccess } from './useStore'
|
|
7
7
|
|
|
8
|
-
// TODO i think we can just replace reaction() with this, its not worse in any way
|
|
9
|
-
|
|
10
8
|
const logUpdate =
|
|
11
9
|
process.env.NODE_ENV === 'development'
|
|
12
10
|
? (fn: any, stores: any[], last: any, next: any) => {
|
|
@@ -26,57 +24,59 @@ const logUpdate =
|
|
|
26
24
|
}
|
|
27
25
|
: null
|
|
28
26
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
27
|
+
export function observe(fn: () => any) {
|
|
28
|
+
let prev = getObserverValueAndStoresAccessed(fn)
|
|
29
|
+
let disposeValue: Function | null = null
|
|
30
|
+
|
|
31
|
+
const subscribe = () => {
|
|
32
|
+
const stores = [...prev.storeInfos]
|
|
33
|
+
return subscribeToStores(stores, () => {
|
|
34
|
+
disposeValue?.()
|
|
35
|
+
const next = getObserverValueAndStoresAccessed(fn)
|
|
36
|
+
|
|
37
|
+
if (typeof next.value === 'function') {
|
|
38
|
+
disposeValue = next.value
|
|
39
|
+
if (process.env.NODE_ENV === 'development') {
|
|
40
|
+
logUpdate!(fn, [...next.storeInfos], '(fn)', '(fn)')
|
|
41
|
+
}
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
if (
|
|
45
|
+
isEqualSubsetShallow(prev.storeInfos, next.storeInfos) &&
|
|
46
|
+
isEqualSubsetShallow(prev.value, next.value)
|
|
47
|
+
) {
|
|
48
|
+
return
|
|
49
|
+
}
|
|
50
|
+
if (process.env.NODE_ENV === 'development') {
|
|
51
|
+
logUpdate!(fn, [...next.storeInfos], prev.value, next.value)
|
|
52
|
+
}
|
|
53
|
+
prev = next
|
|
54
|
+
dispose()
|
|
55
|
+
dispose = subscribe()
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
let dispose = subscribe()
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
dispose: () => {
|
|
63
|
+
dispose()
|
|
64
|
+
disposeValue?.()
|
|
65
|
+
},
|
|
66
|
+
getValue: () => prev.value,
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function useObserve<A>(fn: () => A): A {
|
|
71
71
|
const [state, setState] = useState(() => {
|
|
72
|
-
return
|
|
72
|
+
return getObserverValueAndStoresAccessed(fn)
|
|
73
73
|
})
|
|
74
74
|
|
|
75
75
|
useEffect(() => {
|
|
76
76
|
let dispose
|
|
77
77
|
const unsub = subscribeToStores([...state.storeInfos], () => {
|
|
78
78
|
dispose?.()
|
|
79
|
-
const next =
|
|
79
|
+
const next = getObserverValueAndStoresAccessed(fn)
|
|
80
80
|
|
|
81
81
|
const nextStoreInfos = [...next.storeInfos]
|
|
82
82
|
const prevStoreInfos = [...state.storeInfos]
|
|
@@ -89,6 +89,7 @@ export function useSelector<A>(fn: () => A): A {
|
|
|
89
89
|
dispose = next.value
|
|
90
90
|
return
|
|
91
91
|
}
|
|
92
|
+
|
|
92
93
|
setState((prev) => {
|
|
93
94
|
if (
|
|
94
95
|
isEqualSubsetShallow(prevStoreInfos, nextStoreInfos) &&
|
|
@@ -102,6 +103,7 @@ export function useSelector<A>(fn: () => A): A {
|
|
|
102
103
|
return next
|
|
103
104
|
})
|
|
104
105
|
})
|
|
106
|
+
|
|
105
107
|
return () => {
|
|
106
108
|
unsub()
|
|
107
109
|
dispose?.()
|
|
@@ -111,7 +113,7 @@ export function useSelector<A>(fn: () => A): A {
|
|
|
111
113
|
return state.value
|
|
112
114
|
}
|
|
113
115
|
|
|
114
|
-
function
|
|
116
|
+
function getObserverValueAndStoresAccessed<A>(selector: () => A): {
|
|
115
117
|
value: A
|
|
116
118
|
storeInfos: Set<StoreInfo>
|
|
117
119
|
} {
|
|
@@ -127,10 +129,31 @@ function runStoreSelector<A>(selector: () => A): {
|
|
|
127
129
|
}
|
|
128
130
|
}
|
|
129
131
|
|
|
130
|
-
function subscribeToStores(
|
|
132
|
+
function subscribeToStores(storeInfos: StoreInfo[], onUpdate: () => any) {
|
|
131
133
|
const disposes: Function[] = []
|
|
132
|
-
|
|
133
|
-
|
|
134
|
+
|
|
135
|
+
// wrap onUpdate to avoid waterfall calls + avoid tracking during onUpdate
|
|
136
|
+
let isUpdating = false
|
|
137
|
+
const onUpdateDebouncedWithoutTracking = () => {
|
|
138
|
+
if (isUpdating) return
|
|
139
|
+
isUpdating = true
|
|
140
|
+
queueMicrotask(() => {
|
|
141
|
+
try {
|
|
142
|
+
for (const storeInfo of storeInfos) {
|
|
143
|
+
storeInfo.disableTracking = true
|
|
144
|
+
}
|
|
145
|
+
onUpdate()
|
|
146
|
+
} finally {
|
|
147
|
+
isUpdating = false
|
|
148
|
+
for (const storeInfo of storeInfos) {
|
|
149
|
+
storeInfo.disableTracking = false
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
})
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
for (const storeInfo of storeInfos) {
|
|
156
|
+
disposes.push(storeInfo.subscribe(onUpdateDebouncedWithoutTracking))
|
|
134
157
|
}
|
|
135
158
|
return () => {
|
|
136
159
|
disposes.forEach((x) => x())
|
package/src/useStore.tsx
CHANGED
|
@@ -40,10 +40,7 @@ export function createStore<A, B extends Object>(
|
|
|
40
40
|
props?: B,
|
|
41
41
|
options?: UseStoreOptions<A, any>
|
|
42
42
|
): A {
|
|
43
|
-
return getOrCreateStoreInfo(StoreKlass, props,
|
|
44
|
-
...options,
|
|
45
|
-
avoidCache: true,
|
|
46
|
-
}).store as any
|
|
43
|
+
return getOrCreateStoreInfo(StoreKlass, props, options).store as any
|
|
47
44
|
}
|
|
48
45
|
// use singleton with react
|
|
49
46
|
// TODO selector support with types...
|
|
@@ -144,6 +141,17 @@ export function getStoreInfo(StoreKlass: any, props: any) {
|
|
|
144
141
|
})
|
|
145
142
|
}
|
|
146
143
|
|
|
144
|
+
export type CreateStoreListener = (storeInfo: StoreInfo) => void
|
|
145
|
+
|
|
146
|
+
const onCreateListeners = new Set<CreateStoreListener>()
|
|
147
|
+
|
|
148
|
+
export function onCreateStore(cb: CreateStoreListener) {
|
|
149
|
+
onCreateListeners.add(cb)
|
|
150
|
+
return () => {
|
|
151
|
+
onCreateListeners.delete(cb)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
147
155
|
function getOrCreateStoreInfo(
|
|
148
156
|
StoreKlass: any,
|
|
149
157
|
props: any,
|
|
@@ -185,11 +193,12 @@ function getOrCreateStoreInfo(
|
|
|
185
193
|
const listeners = new Set<Function>()
|
|
186
194
|
|
|
187
195
|
const storeInfo = {
|
|
188
|
-
uid
|
|
196
|
+
uid,
|
|
189
197
|
keyComparators,
|
|
190
198
|
storeInstance,
|
|
191
199
|
getters,
|
|
192
200
|
stateKeys,
|
|
201
|
+
props,
|
|
193
202
|
actions,
|
|
194
203
|
debug: options?.debug,
|
|
195
204
|
disableTracking: false,
|
|
@@ -237,6 +246,8 @@ function getOrCreateStoreInfo(
|
|
|
237
246
|
// still set even when avoidCache is true (hmr)
|
|
238
247
|
cache.set(uid, result)
|
|
239
248
|
|
|
249
|
+
onCreateListeners.forEach((cb) => cb(result))
|
|
250
|
+
|
|
240
251
|
return result
|
|
241
252
|
}
|
|
242
253
|
|
|
@@ -552,13 +563,8 @@ function createProxiedStore(storeInfo: StoreInfo) {
|
|
|
552
563
|
if (key === UNWRAP_STORE_INFO) {
|
|
553
564
|
return storeInfo
|
|
554
565
|
}
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
if (storeAccessTrackers.size && !storeAccessTrackers.has(storeInstance)) {
|
|
558
|
-
for (const t of storeAccessTrackers) {
|
|
559
|
-
t(storeInfo)
|
|
560
|
-
}
|
|
561
|
-
}
|
|
566
|
+
if (storeAccessTrackers.size) {
|
|
567
|
+
storeAccessTrackers.forEach((cb) => cb(storeInfo))
|
|
562
568
|
}
|
|
563
569
|
if (typeof key !== 'string') {
|
|
564
570
|
return Reflect.get(storeInstance, key)
|
|
@@ -566,7 +572,7 @@ function createProxiedStore(storeInfo: StoreInfo) {
|
|
|
566
572
|
|
|
567
573
|
// non-actions...
|
|
568
574
|
|
|
569
|
-
if (!
|
|
575
|
+
if (!storeInfo.disableTracking) {
|
|
570
576
|
if (gettersState.isGetting) {
|
|
571
577
|
gettersState.curGetKeys.add(key)
|
|
572
578
|
} else {
|
package/types/helpers.d.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { StoreInfo } from './interfaces';
|
|
2
|
-
export declare function getStoreUid(Constructor: any, props: string | Object | void):
|
|
2
|
+
export declare function getStoreUid(Constructor: any, props: string | Object | void): any;
|
|
3
3
|
export declare const UNWRAP_STORE_INFO: unique symbol;
|
|
4
4
|
export declare const cache: Map<string, StoreInfo>;
|
|
5
5
|
export declare function getStoreDescriptors(storeInstance: any): {
|
|
6
6
|
[x: string]: TypedPropertyDescriptor<any> & PropertyDescriptor;
|
|
7
7
|
};
|
|
8
8
|
export declare function get<A>(_: A, b?: any): A extends new (props?: any) => infer B ? B : A;
|
|
9
|
-
export declare function getKey(props: Object): string;
|
|
10
9
|
export default function useConstant<T>(fn: () => T): T;
|
|
11
10
|
export declare function simpleStr(arg: any): any;
|
|
12
11
|
export declare function getStoreDebugInfo(store: any): any;
|
package/types/helpers.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.tsx"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAExC,wBAAgB,WAAW,CAAC,WAAW,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,OAO1E;AAED,eAAO,MAAM,iBAAiB,eAA8B,CAAA;AAC5D,eAAO,MAAM,KAAK,wBAA+B,CAAA;AAEjD,wBAAgB,mBAAmB,CAAC,aAAa,EAAE,GAAG;;EAWrD;AAED,wBAAgB,GAAG,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,GAAG,CAAC,SAAS,KAAK,KAAK,CAAC,EAAE,GAAG,KAAK,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAEpF;AAID,MAAM,CAAC,OAAO,UAAU,WAAW,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CAMrD;AAED,wBAAgB,SAAS,CAAC,GAAG,EAAE,GAAG,OAejC;AAGD,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,GAAG,OAI3C"}
|
package/types/index.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
export * from './useStore';
|
|
2
2
|
export { configureUseStore } from './configureUseStore';
|
|
3
3
|
export * from './interfaces';
|
|
4
|
-
export * from './
|
|
5
|
-
export * from './reaction';
|
|
4
|
+
export * from './observe';
|
|
6
5
|
export { UNWRAP_PROXY } from './constants';
|
|
7
6
|
export * from './comparators';
|
|
8
7
|
export * from './decorators';
|
package/types/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AACvD,cAAc,cAAc,CAAA;AAC5B,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AACvD,cAAc,cAAc,CAAA;AAC5B,cAAc,WAAW,CAAA;AACzB,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC1C,cAAc,eAAe,CAAA;AAC7B,cAAc,cAAc,CAAA;AAG5B,qBAAa,KAAK,CAAC,KAAK,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAC/B,KAAK,EAAE,KAAK;gBAAZ,KAAK,EAAE,KAAK;CAChC"}
|
package/types/interfaces.d.ts
CHANGED
|
@@ -11,11 +11,12 @@ export interface Store<Props = Record<string, any> | null | undefined> {
|
|
|
11
11
|
props: Props;
|
|
12
12
|
}
|
|
13
13
|
export type StoreInfo<A = Store> = {
|
|
14
|
-
uid:
|
|
14
|
+
uid: string;
|
|
15
15
|
keyComparators?: {
|
|
16
16
|
[key: string]: (a: any, b: any) => boolean;
|
|
17
17
|
};
|
|
18
18
|
store: A;
|
|
19
|
+
props: Record<string, any> | null;
|
|
19
20
|
storeInstance: any;
|
|
20
21
|
getters: {
|
|
21
22
|
[key: string]: any;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interfaces.d.ts","sourceRoot":"","sources":["../src/interfaces.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAEzC,MAAM,MAAM,QAAQ,CAAC,CAAC,GAAG,OAAO,EAAE,CAAC,GAAG,OAAO,IAAI,CAAC,CAAC,EAAE,CAAC,KAAK,CA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AAgB,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG;;;EAyCpC;AAED,wBAAgB,UAAU,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CA4C5C"}
|
package/types/reaction.d.ts
CHANGED
|
@@ -1,4 +1 @@
|
|
|
1
|
-
import { StoreInfo } from './interfaces';
|
|
2
|
-
export declare function useReaction<StoreInstance, Selector extends (a: StoreInstance) => any>(store: StoreInstance, selector: Selector, receiver: Selector extends (a: StoreInstance) => infer Derived ? (a: Derived) => any : unknown, props?: Record<string, any>, equalityFn?: (a: any, b: any) => boolean, memoArgs?: any[]): () => void;
|
|
3
|
-
export declare function reaction<StoreInstance extends StoreInfo, Selector extends (a: StoreInstance) => any>({ store, subscribe }: StoreInstance, selector: Selector, receiver: Selector extends (a: StoreInstance) => infer Derived ? (a: Derived) => any : unknown, equalityFn?: (a: any, b: any) => boolean): () => void;
|
|
4
1
|
//# sourceMappingURL=reaction.d.ts.map
|
package/types/reaction.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reaction.d.ts","sourceRoot":"","sources":["../src/reaction.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"reaction.d.ts","sourceRoot":"","sources":["../src/reaction.tsx"],"names":[],"mappings":""}
|
package/types/selector.d.ts
CHANGED
package/types/selector.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"selector.d.ts","sourceRoot":"","sources":["../src/selector.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"selector.d.ts","sourceRoot":"","sources":["../src/selector.tsx"],"names":[],"mappings":"AA0BA,wBAAgB,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG;;;EAwCpC;AAED,wBAAgB,UAAU,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CA4C5C"}
|
package/types/useStore.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ export declare function trackStoresAccess(cb: StoreAccessTracker): () => void;
|
|
|
12
12
|
export declare function getStore<A, B extends Object>(StoreKlass: (new (props: B) => A) | (new () => A), props?: B): A;
|
|
13
13
|
export declare function getOrCreateStore<A, B extends Object>(StoreKlass: (new (props: B) => A) | (new () => A), props?: B): A;
|
|
14
14
|
export declare function getStoreInfo(StoreKlass: any, props: any): StoreInfo;
|
|
15
|
+
export type CreateStoreListener = (storeInfo: StoreInfo) => void;
|
|
16
|
+
export declare function onCreateStore(cb: CreateStoreListener): () => void;
|
|
15
17
|
export declare const allStores: {};
|
|
16
18
|
export declare const setIsInReaction: (val: boolean) => void;
|
|
17
19
|
export type StoreTracker = {
|
package/types/useStore.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStore.d.ts","sourceRoot":"","sources":["../src/useStore.tsx"],"names":[],"mappings":"AAYA,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAM1E,wBAAgB,QAAQ,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,EAC1C,UAAU,EAAE,CAAC,KAAK,KAAK,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,EACjD,KAAK,CAAC,EAAE,CAAC,GAAG,IAAI,EAChB,OAAO,GAAE,eAAe,CAAC,CAAC,EAAE,GAAG,CAAkB,GAChD,CAAC,CAKH;AAED,wBAAgB,aAAa,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,EAC/C,UAAU,EAAE,CAAC,KAAK,KAAK,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,EACjD,KAAK,CAAC,EAAE,CAAC,GACR,CAAC,CAEH;AAGD,wBAAgB,WAAW,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,EAC7C,UAAU,EAAE,KAAK,KAAK,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,UAAU,CA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AAK,KAAK,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,EACjD,KAAK,CAAC,EAAE,CAAC,GACR,CAAC,CAEH;AAED,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,EAClD,UAAU,EAAE,CAAC,KAAK,KAAK,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,EACjD,KAAK,CAAC,EAAE,CAAC,GACR,CAAC,CAIH;AAGD,wBAAgB,YAAY,CAAC,UAAU,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,aAIvD;AAED,MAAM,MAAM,mBAAmB,GAAG,CAAC,SAAS,EAAE,SAAS,KAAK,IAAI,CAAA;AAIhE,wBAAgB,aAAa,CAAC,EAAE,EAAE,mBAAmB,cAKpD;AAqGD,eAAO,MAAM,SAAS,IAAK,CAAA;AAe3B,eAAO,MAAM,eAAe,QAAS,OAAO,SAE3C,CAAA;AA0ZD,MAAM,MAAM,YAAY,GAAG;IACzB,OAAO,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IACpB,SAAS,CAAC,EAAE,GAAG,CAAA;IACf,IAAI,CAAC,EAAE,GAAG,CAAA;IACV,QAAQ,CAAC,EAAE,GAAG,CAAA;CACf,CAAA"}
|
package/src/reaction.tsx
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import { useMemo } from 'react'
|
|
2
|
-
|
|
3
|
-
import { isEqualSubsetShallow } from './comparators'
|
|
4
|
-
import { UNWRAP_PROXY } from './constants'
|
|
5
|
-
import { StoreInfo } from './interfaces'
|
|
6
|
-
import { getStoreInfo, setIsInReaction } from './useStore'
|
|
7
|
-
|
|
8
|
-
const dispose = (d: any) => {
|
|
9
|
-
if (typeof d === 'function') {
|
|
10
|
-
d()
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function useReaction<StoreInstance, Selector extends (a: StoreInstance) => any>(
|
|
15
|
-
store: StoreInstance,
|
|
16
|
-
selector: Selector,
|
|
17
|
-
receiver: Selector extends (a: StoreInstance) => infer Derived
|
|
18
|
-
? (a: Derived) => any
|
|
19
|
-
: unknown,
|
|
20
|
-
props?: Record<string, any>,
|
|
21
|
-
equalityFn: (a: any, b: any) => boolean = isEqualSubsetShallow,
|
|
22
|
-
memoArgs?: any[]
|
|
23
|
-
) {
|
|
24
|
-
const storeInfo = getStoreInfo(store, props)
|
|
25
|
-
return useMemo(
|
|
26
|
-
() => reaction(storeInfo, selector as any, receiver, equalityFn),
|
|
27
|
-
[memoArgs]
|
|
28
|
-
)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function reaction<
|
|
32
|
-
StoreInstance extends StoreInfo,
|
|
33
|
-
Selector extends (a: StoreInstance) => any
|
|
34
|
-
>(
|
|
35
|
-
{ store, subscribe }: StoreInstance,
|
|
36
|
-
selector: Selector,
|
|
37
|
-
receiver: Selector extends (a: StoreInstance) => infer Derived
|
|
38
|
-
? (a: Derived) => any
|
|
39
|
-
: unknown,
|
|
40
|
-
equalityFn: (a: any, b: any) => boolean = isEqualSubsetShallow
|
|
41
|
-
) {
|
|
42
|
-
let last: any = undefined
|
|
43
|
-
let innerDispose: any
|
|
44
|
-
|
|
45
|
-
function updateReaction() {
|
|
46
|
-
try {
|
|
47
|
-
setIsInReaction(true)
|
|
48
|
-
const storeInstance = store[UNWRAP_PROXY] || store
|
|
49
|
-
const next = selector(storeInstance)
|
|
50
|
-
if (!equalityFn(last, next)) {
|
|
51
|
-
if (process.env.NODE_ENV === 'development') {
|
|
52
|
-
console.groupCollapsed(
|
|
53
|
-
`🌑 ⏭ %c${receiver.name.padStart(24)} (${storeInstance.constructor.name}${
|
|
54
|
-
store.props?.id ? `:${store.props.id}` : ''
|
|
55
|
-
}) ${last} => ${next}`,
|
|
56
|
-
'color: chocolate;'
|
|
57
|
-
)
|
|
58
|
-
console.groupCollapsed('trace >')
|
|
59
|
-
console.trace()
|
|
60
|
-
console.groupEnd()
|
|
61
|
-
// rome-ignore lint/nursery/noConsoleLog: <explanation>
|
|
62
|
-
console.log(' ARG', next)
|
|
63
|
-
console.groupEnd()
|
|
64
|
-
}
|
|
65
|
-
dispose(innerDispose)
|
|
66
|
-
last = next
|
|
67
|
-
innerDispose = receiver(next)
|
|
68
|
-
}
|
|
69
|
-
} finally {
|
|
70
|
-
setIsInReaction(false)
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const disposeSubscribe = subscribe(updateReaction)
|
|
75
|
-
updateReaction()
|
|
76
|
-
|
|
77
|
-
return () => {
|
|
78
|
-
disposeSubscribe()
|
|
79
|
-
dispose(innerDispose)
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// start on simpler reaction
|
|
84
|
-
// export function reaction2(fn: () => any): () => void {
|
|
85
|
-
// let state = runStoreSelector(fn)
|
|
86
|
-
// let disposeSubscribe
|
|
87
|
-
// const disposePrev = () => {
|
|
88
|
-
// // treat return functions as dispose
|
|
89
|
-
// if (typeof state.value === 'function') {
|
|
90
|
-
// state.value()
|
|
91
|
-
// }
|
|
92
|
-
// }
|
|
93
|
-
// const dispose = () => {
|
|
94
|
-
// disposeSubscribe?.()
|
|
95
|
-
// disposePrev()
|
|
96
|
-
// }
|
|
97
|
-
// function update() {
|
|
98
|
-
// dispose()
|
|
99
|
-
// disposeSubscribe = subscribeToStores([...state.stores], () => {
|
|
100
|
-
// const next = runStoreSelector(fn)
|
|
101
|
-
// disposePrev()
|
|
102
|
-
// if (!isEqualSubsetShallow(state.stores, next.stores)) {
|
|
103
|
-
// state = next
|
|
104
|
-
// update()
|
|
105
|
-
// } else {
|
|
106
|
-
// state = next
|
|
107
|
-
// }
|
|
108
|
-
// })
|
|
109
|
-
// }
|
|
110
|
-
// update()
|
|
111
|
-
// return dispose
|
|
112
|
-
// }
|