22import * as React from 'react'
33
44// duplicating this from the .flow file as we don't pull in those files for rn
5- type TimerFunc = ( func : ( ) => void , timing : number ) => number
6- type ClearTimerFunc = ( id : ?number ) => void
75type TimerProps = {
8- setTimeout : TimerFunc ,
9- clearTimeout : ClearTimerFunc ,
10- setInterval : TimerFunc ,
11- clearInterval : ClearTimerFunc ,
12- }
13-
14- function clearId ( clearFunc : ( id ? : number ) = > void , array : Array < number > , id ?: ?number ) : void {
15- if ( ( id || id === 0 ) && array . includes ( id ) ) {
16- array . splice ( array . indexOf ( id ) , 1 )
17- clearFunc ( id )
18- }
6+ setTimeout : ( func : ( ) => void , timing : number ) => TimeoutID ,
7+ clearTimeout : ( id : TimeoutID ) = > void ,
8+ setInterval : ( func : ( ) = > void , timing : number ) = > IntervalID ,
9+ clearInterval : ( id : IntervalID ) = > void ,
1910}
2011
2112function getDisplayName ( WrappedComponent ) : string {
@@ -25,8 +16,8 @@ function getDisplayName(WrappedComponent): string {
2516export default function HOCTimers ( ComposedComponent : any ) {
2617 class TimersComponent extends React . Component < any > {
2718 static displayName = `HOCTimers(${ getDisplayName ( ComposedComponent ) } )`
28- _timeoutIds : Array < number >
29- _intervalIds : Array < number >
19+ _timeoutIds : Array < TimeoutID >
20+ _intervalIds : Array < IntervalID >
3021 _timerFuncs : TimerProps
3122
3223 constructor ( props : any ) {
@@ -40,15 +31,21 @@ export default function HOCTimers(ComposedComponent: any) {
4031 return id
4132 } ,
4233 clearTimeout : id => {
43- clearId ( clearTimeout , this . _timeoutIds , id )
34+ if ( ( id || id === 0 ) && this . _timeoutIds . includes ( id ) ) {
35+ this . _timeoutIds . splice ( this . _timeoutIds . indexOf ( id ) , 1 )
36+ clearTimeout ( id )
37+ }
4438 } ,
4539 setInterval : ( f , n ) => {
4640 const id = setInterval ( f , n )
4741 this . _intervalIds . push ( id )
4842 return id
4943 } ,
5044 clearInterval : id => {
51- clearId ( clearInterval , this . _intervalIds , id )
45+ if ( ( id || id === 0 ) && this . _intervalIds . includes ( id ) ) {
46+ this . _intervalIds . splice ( this . _intervalIds . indexOf ( id ) , 1 )
47+ clearInterval ( id )
48+ }
5249 } ,
5350 }
5451 }
0 commit comments