@fozy-labs/rx-toolkit 0.4.6 → 0.4.8
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.
|
@@ -5,6 +5,7 @@ import { shallowEqual } from "../../query/lib/shallowEqual";
|
|
|
5
5
|
import { SKIP } from "../../query/SKIP_TOKEN";
|
|
6
6
|
export function useResourceAgent(res, ...argss) {
|
|
7
7
|
const args = (argss[0] === SKIP ? SKIP : argss[0]);
|
|
8
|
+
const prevArgsRef = React.useRef(args);
|
|
8
9
|
const agent = useConstant(() => {
|
|
9
10
|
const agent = res.createAgent();
|
|
10
11
|
if (args !== SKIP) {
|
|
@@ -12,16 +13,10 @@ export function useResourceAgent(res, ...argss) {
|
|
|
12
13
|
}
|
|
13
14
|
return agent;
|
|
14
15
|
});
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
const state = agent.state$.peek();
|
|
20
|
-
if (state.isInitiated && shallowEqual(args, state.args)) {
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
16
|
+
if (args !== SKIP && !shallowEqual(args, prevArgsRef.current)) {
|
|
17
|
+
prevArgsRef.current = args;
|
|
23
18
|
agent.initiate(args);
|
|
24
|
-
}
|
|
19
|
+
}
|
|
25
20
|
React.useEffect(() => () => {
|
|
26
21
|
agent.complete();
|
|
27
22
|
}, []);
|
|
@@ -1,12 +1,23 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
import { useEventHandler } from "../../common/react";
|
|
3
3
|
export function useSignal(signal$) {
|
|
4
|
+
const doUpdateRef = React.useRef(false);
|
|
4
5
|
const subscribe = React.useCallback((update) => {
|
|
5
|
-
const subscription = signal$.subscribe(
|
|
6
|
+
const subscription = signal$.subscribe(() => {
|
|
7
|
+
doUpdateRef.current = true;
|
|
8
|
+
queueMicrotask(() => {
|
|
9
|
+
if (!doUpdateRef.current)
|
|
10
|
+
return;
|
|
11
|
+
update();
|
|
12
|
+
});
|
|
13
|
+
});
|
|
6
14
|
return () => {
|
|
7
15
|
subscription.unsubscribe();
|
|
8
16
|
};
|
|
9
17
|
}, [signal$]);
|
|
10
|
-
const getSnapshot = useEventHandler(() =>
|
|
18
|
+
const getSnapshot = useEventHandler(() => {
|
|
19
|
+
doUpdateRef.current = false;
|
|
20
|
+
return signal$.peek();
|
|
21
|
+
});
|
|
11
22
|
return React.useSyncExternalStore(subscribe, getSnapshot);
|
|
12
23
|
}
|