@aiszlab/relax 1.2.40 → 1.2.41
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.
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { useRef } from 'react';
|
|
2
2
|
|
|
3
3
|
var useMemorable = function useMemorable(getter, condition, shouldUpdate) {
|
|
4
|
-
var cacheRef =
|
|
4
|
+
var cacheRef = useRef(null);
|
|
5
5
|
if (cacheRef.current === null || shouldUpdate(cacheRef.current.condition, condition)) {
|
|
6
6
|
cacheRef.current = {
|
|
7
7
|
value: getter(),
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { useEffect } from 'react';
|
|
1
|
+
import { useRef, useEffect } from 'react';
|
|
2
|
+
import { Observable, delay } from 'rxjs';
|
|
3
|
+
import { useEvent } from './use-event.js';
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
6
|
* @author murukal
|
|
@@ -7,13 +9,49 @@ import { useEffect } from 'react';
|
|
|
7
9
|
* timeout effect
|
|
8
10
|
*/
|
|
9
11
|
var useTimeout = function useTimeout(handler, wait) {
|
|
12
|
+
var timer = useRef(null);
|
|
13
|
+
var trigger = useRef(null);
|
|
14
|
+
// when user what to flush timeout handler
|
|
15
|
+
// if trigger already registed, just complete trigger
|
|
16
|
+
// not registed, call `handler` manaully
|
|
17
|
+
var flush = useEvent(function () {
|
|
18
|
+
if (trigger.current) {
|
|
19
|
+
var _timer$current;
|
|
20
|
+
trigger.current.complete();
|
|
21
|
+
(_timer$current = timer.current) === null || _timer$current === void 0 || _timer$current.unsubscribe();
|
|
22
|
+
} else {
|
|
23
|
+
handler();
|
|
24
|
+
}
|
|
25
|
+
trigger.current = null;
|
|
26
|
+
timer.current = null;
|
|
27
|
+
});
|
|
28
|
+
// cancel
|
|
29
|
+
var cancel = useEvent(function () {
|
|
30
|
+
var _trigger$current, _timer$current2;
|
|
31
|
+
(_trigger$current = trigger.current) === null || _trigger$current === void 0 || _trigger$current.error();
|
|
32
|
+
(_timer$current2 = timer.current) === null || _timer$current2 === void 0 || _timer$current2.unsubscribe();
|
|
33
|
+
trigger.current = null;
|
|
34
|
+
timer.current = null;
|
|
35
|
+
});
|
|
10
36
|
useEffect(function () {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
37
|
+
// if 0, always mean not need to set timeout
|
|
38
|
+
if (wait <= 0) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
var _timer = new Observable(function (_trigger) {
|
|
42
|
+
trigger.current = _trigger;
|
|
43
|
+
_trigger.next();
|
|
44
|
+
}).pipe(delay(wait)).subscribe(function () {
|
|
45
|
+
handler();
|
|
46
|
+
});
|
|
47
|
+
timer.current = _timer;
|
|
48
|
+
// unmount callback
|
|
49
|
+
return cancel;
|
|
16
50
|
}, [wait]);
|
|
51
|
+
return {
|
|
52
|
+
flush: flush,
|
|
53
|
+
cancel: cancel
|
|
54
|
+
};
|
|
17
55
|
};
|
|
18
56
|
|
|
19
57
|
export { useTimeout };
|