Skip to content

Commit 6ec1528

Browse files
committed
use the same throttle function as the webStoragePersistor
1 parent ca037b1 commit 6ec1528

File tree

1 file changed

+16
-24
lines changed
  • src/createAsyncStoragePersisor-experimental

1 file changed

+16
-24
lines changed
Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,5 @@
11
import AsyncStorage from '@react-native-community/async-storage'
22

3-
function throttle(func, wait) {
4-
if (wait === 0) {
5-
wait = 100
6-
}
7-
8-
var timer = null
9-
return function () {
10-
for (
11-
var _len = arguments.length, args = new Array(_len), _key = 0;
12-
_key < _len;
13-
_key++
14-
) {
15-
args[_key] = arguments[_key]
16-
}
17-
18-
if (timer === null) {
19-
timer = setTimeout(function () {
20-
func.apply(0, args)
21-
timer = null
22-
}, wait)
23-
}
24-
}
25-
}
26-
273
export const asyncStoragePersistor = ({ asyncStorageKey, throttleTime }) => {
284
return {
295
persistClient: throttle(function (persistedClient) {
@@ -43,3 +19,19 @@ export const asyncStoragePersistor = ({ asyncStorageKey, throttleTime }) => {
4319
},
4420
}
4521
}
22+
23+
function throttle<TArgs extends any[]>(
24+
func: (...args: TArgs) => any,
25+
wait = 100
26+
) {
27+
let timer: number | null = null
28+
29+
return function (...args: TArgs) {
30+
if (timer === null) {
31+
timer = setTimeout(() => {
32+
func(...args)
33+
timer = null
34+
}, wait)
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)