@rpcbase/client 0.252.0 → 0.254.0
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/hooks.ts +1 -0
- package/package.json +6 -2
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useThrottledMeasure.ts +47 -0
package/hooks.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/hooks"
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rpcbase/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.254.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./src/index.ts",
|
|
6
6
|
"scripts": {
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"files": [
|
|
15
15
|
"package.json",
|
|
16
16
|
"./instrument.ts",
|
|
17
|
+
"./hooks.ts",
|
|
17
18
|
"src/**/*"
|
|
18
19
|
],
|
|
19
20
|
"output": [],
|
|
@@ -28,7 +29,10 @@
|
|
|
28
29
|
"dependencies": {
|
|
29
30
|
"@sentry/react": "9.15.0",
|
|
30
31
|
"@sentry/tracing": "7.120.3",
|
|
31
|
-
"axios": "1.9.0"
|
|
32
|
+
"axios": "1.9.0",
|
|
33
|
+
"fast-equals": "5.2.2",
|
|
34
|
+
"lodash": "4.17.21",
|
|
35
|
+
"react-use": "17.6.0"
|
|
32
36
|
},
|
|
33
37
|
"devDependencies": {}
|
|
34
38
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./useThrottledMeasure"
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import {useState, useEffect, useRef, useCallback} from "react"
|
|
2
|
+
import useMeasure from "react-use/lib/useMeasure"
|
|
3
|
+
import _throttle from "lodash/throttle"
|
|
4
|
+
import { deepEqual } from "fast-equals"
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
const DEFAULT_THROTTLE_TIME = 16
|
|
8
|
+
|
|
9
|
+
export const useThrottledMeasure = (throttleDuration = DEFAULT_THROTTLE_TIME) => {
|
|
10
|
+
const hasInitialMeasure = useRef(false)
|
|
11
|
+
|
|
12
|
+
const [ref, measuredRect] = useMeasure()
|
|
13
|
+
const [rect, setRect] = useState(() => {
|
|
14
|
+
return {x: 0, y: 0, width: 0, height: 0, top: 0, left: 0, bottom: 0, right: 0}
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
const throttledSetRect = useCallback(
|
|
18
|
+
_throttle(
|
|
19
|
+
(newRect) => {
|
|
20
|
+
setRect((current) => {
|
|
21
|
+
return deepEqual(current, newRect) ? current : newRect
|
|
22
|
+
})
|
|
23
|
+
},
|
|
24
|
+
throttleDuration,
|
|
25
|
+
{leading: true, trailing: true},
|
|
26
|
+
),
|
|
27
|
+
[throttleDuration],
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
if (measuredRect.width > 0 && !hasInitialMeasure.current) {
|
|
32
|
+
hasInitialMeasure.current = true
|
|
33
|
+
setRect((current) => {
|
|
34
|
+
return deepEqual(current, measuredRect) ? current : measuredRect
|
|
35
|
+
})
|
|
36
|
+
return
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
throttledSetRect(measuredRect)
|
|
40
|
+
|
|
41
|
+
return () => {
|
|
42
|
+
throttledSetRect.cancel()
|
|
43
|
+
}
|
|
44
|
+
}, [measuredRect, throttledSetRect])
|
|
45
|
+
|
|
46
|
+
return [ref, rect]
|
|
47
|
+
}
|