Skip to content

Commit 20fa4df

Browse files
committed
add third parameter for useRefValues
1 parent 021d948 commit 20fa4df

File tree

3 files changed

+37
-33
lines changed

3 files changed

+37
-33
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@atomico/hooks",
33
"description": "Series of utilities in hooks format to extend the operation of Atomico",
4-
"version": "3.41.0",
4+
"version": "3.42.0",
55
"type": "module",
66
"workspaces": [
77
"src/**/*"

src/use-intersection-observer/use-intersection-observer.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ import { useRefValues } from "../use-ref-values/use-ref-values.js";
77
* @param {IntersectionObserverInit} [options]
88
*/
99
export function useRefIntersectionObserver(ref, callback, options) {
10-
useRefValues(() => {
11-
const intersection = new IntersectionObserver(callback, options);
12-
intersection.observe(ref.current);
13-
return () => intersection.disconnect();
14-
}, [ref]);
10+
useRefValues(
11+
() => {
12+
const intersection = new IntersectionObserver(callback, options);
13+
intersection.observe(ref.current);
14+
return () => intersection.disconnect();
15+
},
16+
[ref],
17+
true
18+
);
1519
}
1620

1721
/**

src/use-ref-values/use-ref-values.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,40 @@ import { useHook } from "atomico";
44
* @template {import("atomico").Ref[]} T
55
* @param {(args: Args<T>)=>(void | ()=>any)} callback
66
* @param {T} args
7+
* @param {boolean} [mode] - by default it is executed in the cycle of useLayoutEffect, if true it will be executed in the cycle of useEffect
78
*/
8-
export function useRefValues(callback, args) {
9-
useHook(
10-
(state = []) => state,
11-
(state, unmounted) => {
12-
const { length } = args;
13-
let [prevArgs = [], collector] = state;
14-
let withCurrent = 0;
15-
let withDiff = 0;
16-
let nextArgs = [];
9+
export function useRefValues(callback, args, mode) {
10+
const effect = (state, unmounted) => {
11+
const { length } = args;
12+
let [prevArgs = [], collector] = state;
13+
let withCurrent = 0;
14+
let withDiff = 0;
15+
let nextArgs = [];
1716

18-
for (let i = 0; i < length; i++) {
19-
const { current } = args[i];
20-
if (current != null) {
21-
withCurrent++;
22-
}
23-
if (current != prevArgs[i]) {
24-
withDiff++;
25-
}
26-
nextArgs.push(current);
17+
for (let i = 0; i < length; i++) {
18+
const { current } = args[i];
19+
if (current != null) {
20+
withCurrent++;
2721
}
28-
29-
if ((withDiff || unmounted) && collector) {
30-
collector();
31-
collector = null;
22+
if (current != prevArgs[i]) {
23+
withDiff++;
3224
}
25+
nextArgs.push(current);
26+
}
3327

34-
if (withDiff && !unmounted && withCurrent === length) {
35-
collector = callback(nextArgs);
36-
}
28+
if ((withDiff || unmounted) && collector) {
29+
collector();
30+
collector = null;
31+
}
3732

38-
return [nextArgs, collector];
33+
if (withDiff && !unmounted && withCurrent === length) {
34+
collector = callback(nextArgs);
3935
}
40-
);
36+
37+
return [nextArgs, collector];
38+
};
39+
40+
useHook((state = []) => state, !mode && effect, mode && effect);
4141
}
4242

4343
/**

0 commit comments

Comments
 (0)