File tree Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Original file line number Diff line number Diff line change 1
1
export * from './base64'
2
2
export * from './merge'
3
+ export * from './set-interval'
3
4
export * from './sleep'
4
5
export * from './trampoline'
Original file line number Diff line number Diff line change
1
+ const timerMap = new Map < number , ReturnType < typeof setTimeout > > ( )
2
+
3
+ export const setClockInterval = ( func : ( ...args : any [ ] ) => any , interval : number ) => {
4
+ let start : number
5
+ let tick : number
6
+ let clockTimer : ReturnType < typeof setTimeout >
7
+
8
+ // eslint-disable-next-line sonarjs/pseudo-random
9
+ const timerId = Math . floor ( Math . random ( ) * 1e10 )
10
+
11
+ const recurFunc = ( ) => {
12
+ func ( )
13
+ const realExecuteTime = new Date ( ) . getTime ( )
14
+ if ( ! start ) {
15
+ start = realExecuteTime
16
+ }
17
+
18
+ tick = tick || start
19
+ const diff = realExecuteTime - tick
20
+ tick += interval
21
+
22
+ // Since setTimeout is not accurate, we need to adjust the interval
23
+ // eslint-disable-next-line @masknet/no-timer
24
+ clockTimer = setTimeout ( recurFunc , interval - diff )
25
+ timerMap . set ( timerId , clockTimer )
26
+ }
27
+
28
+ recurFunc ( )
29
+ return timerId
30
+ }
31
+
32
+ export const clearClockInterval = ( timerId : number ) => {
33
+ const timer = timerMap . get ( timerId )
34
+ if ( timer == null ) {
35
+ return
36
+ }
37
+
38
+ // eslint-disable-next-line @masknet/no-timer
39
+ clearTimeout ( timer )
40
+ timerMap . delete ( timerId )
41
+ }
Original file line number Diff line number Diff line change
1
+ export * from './clock'
You can’t perform that action at this time.
0 commit comments