@rpcbase/client 0.120.0 → 0.122.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.
Files changed (2) hide show
  1. package/helpers/useRPC.js +18 -4
  2. package/package.json +1 -1
package/helpers/useRPC.js CHANGED
@@ -1,16 +1,30 @@
1
1
  /* @flow */
2
- import {useState, useEffect} from "react"
2
+ import {useCallback, useMemo, useState, useEffect, useRef} from "react"
3
+ import isEqual from "fast-deep-equal"
4
+
5
+ const useRPC = (fn, payload) => {
6
+ const previousPayloadRef = useRef(null)
3
7
 
4
- const useRPC = (fn, payload = {}) => {
5
8
  const [loading, setLoading] = useState(true)
6
9
  const [error, setError] = useState()
7
10
  const [data, setData] = useState()
8
11
 
12
+ const memoFn = useCallback(fn, [fn])
13
+ const memoPayload = useMemo(() => {
14
+ return payload || {}
15
+ }, [payload])
16
+
9
17
  useEffect(() => {
18
+ if (isEqual(previousPayloadRef.current, memoPayload)) {
19
+ return
20
+ }
21
+
22
+ previousPayloadRef.current = memoPayload
23
+
10
24
  const load = async() => {
11
25
  setLoading(true)
12
26
 
13
- const res = await fn(payload)
27
+ const res = await memoFn(memoPayload)
14
28
 
15
29
  setLoading(false)
16
30
  setData(res)
@@ -19,7 +33,7 @@ const useRPC = (fn, payload = {}) => {
19
33
  }
20
34
  }
21
35
  load()
22
- }, [fn, payload])
36
+ }, [memoFn, memoPayload])
23
37
 
24
38
  return {loading, error, data}
25
39
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/client",
3
- "version": "0.120.0",
3
+ "version": "0.122.0",
4
4
  "scripts": {
5
5
  "build-firebase": "webpack -c firebase/webpack.config.js",
6
6
  "build": "yarn build-firebase",