@@ -4,9 +4,9 @@ const MAX_DELAY = 2147483647; // 2 ** 31 - 1
44const ACCURACY = 250 ;
55
66export interface Timer {
7- state ( ) : "waiting" | "stopped " | "completed" ;
8- stop ( ) : Timer ;
9- start ( ) : Timer ;
7+ state ( ) : "waiting" | "paused " | "completed" ;
8+ pause ( ) : Timer ;
9+ resume ( ) : Timer ;
1010 clear ( ) : void ;
1111}
1212
@@ -19,21 +19,21 @@ export function createTimeout(
1919) : Timer {
2020 let waitUntil = parseDelay ( delay ?? 0 ) ;
2121 let timer : ReturnType < typeof setTimeout > | null = null ;
22- let stopRemains : number | null = null ;
22+ let pauseRemains : number | null = null ;
2323 let isCompleted = false ;
2424
2525 const timeoutInstance = { } as Timer ;
26- function stop ( ) {
26+ function pause ( ) {
2727 if ( isCompleted ) return timeoutInstance ;
2828 timer && clearTimeout ( timer ) ;
29- stopRemains = stopRemains ?? waitUntil - Date . now ( ) ;
29+ pauseRemains = pauseRemains ?? waitUntil - Date . now ( ) ;
3030 return timeoutInstance ;
3131 }
32- function start ( ) {
32+ function resume ( ) {
3333 if ( isCompleted ) return timeoutInstance ;
34- if ( typeof stopRemains === "number" ) {
35- waitUntil = Date . now ( ) + stopRemains ;
36- stopRemains = null ;
34+ if ( typeof pauseRemains === "number" ) {
35+ waitUntil = Date . now ( ) + pauseRemains ;
36+ pauseRemains = null ;
3737 }
3838 const remains = Math . max ( 0 , waitUntil - Date . now ( ) ) ;
3939 if ( remains <= ACCURACY ) {
@@ -42,7 +42,7 @@ export function createTimeout(
4242 cb ( ) ;
4343 } , remains ) ;
4444 } else {
45- timer = setTimeout ( start , Math . min ( remains >> 1 , MAX_DELAY ) ) ;
45+ timer = setTimeout ( resume , Math . min ( remains >> 1 , MAX_DELAY ) ) ;
4646 }
4747 return timeoutInstance ;
4848 }
@@ -52,16 +52,16 @@ export function createTimeout(
5252 }
5353 function state ( ) {
5454 if ( isCompleted ) return "completed" ;
55- if ( typeof stopRemains === "number" ) return "stopped " ;
55+ if ( typeof pauseRemains === "number" ) return "paused " ;
5656 return "waiting" ;
5757 }
5858
5959 timeoutInstance . state = state ;
60- timeoutInstance . start = start ;
61- timeoutInstance . stop = stop ;
60+ timeoutInstance . pause = pause ;
61+ timeoutInstance . resume = resume ;
6262 timeoutInstance . clear = clear ;
6363
64- start ( ) ;
64+ resume ( ) ;
6565
6666 return timeoutInstance ;
6767}
@@ -77,31 +77,31 @@ export function createInterval(
7777) : Timer {
7878 let waitUntil = parseInterval ( parseDelay ( delay ?? 0 ) , interval ?? 0 ) ;
7979 let timer : ReturnType < typeof setTimeout > | null = null ;
80- let stopRemains : number | null = null ;
80+ let pauseRemains : number | null = null ;
8181 let isCompleted = false ;
8282
8383 const intervalInstance = { } as Timer ;
84- function stop ( ) {
84+ function pause ( ) {
8585 if ( isCompleted ) return intervalInstance ;
8686 timer && clearTimeout ( timer ) ;
87- stopRemains = stopRemains ?? waitUntil - Date . now ( ) ;
87+ pauseRemains = pauseRemains ?? waitUntil - Date . now ( ) ;
8888 return intervalInstance ;
8989 }
90- function start ( ) {
90+ function resume ( ) {
9191 if ( isCompleted ) return intervalInstance ;
92- if ( typeof stopRemains === "number" ) {
93- waitUntil = Date . now ( ) + stopRemains ;
94- stopRemains = null ;
92+ if ( typeof pauseRemains === "number" ) {
93+ waitUntil = Date . now ( ) + pauseRemains ;
94+ pauseRemains = null ;
9595 }
9696 const remains = Math . max ( 0 , waitUntil - Date . now ( ) ) ;
9797 if ( remains <= ACCURACY ) {
9898 timer = setTimeout ( ( ) => {
9999 cb ( ) ;
100100 waitUntil = parseInterval ( waitUntil , interval || 0 ) ;
101- start ( ) ;
101+ resume ( ) ;
102102 } , remains ) ;
103103 } else {
104- timer = setTimeout ( start , Math . min ( remains >> 1 , MAX_DELAY ) ) ;
104+ timer = setTimeout ( resume , Math . min ( remains >> 1 , MAX_DELAY ) ) ;
105105 }
106106 return intervalInstance ;
107107 }
@@ -111,16 +111,16 @@ export function createInterval(
111111 }
112112 function state ( ) {
113113 if ( isCompleted ) return "completed" ;
114- if ( typeof stopRemains === "number" ) return "stopped " ;
114+ if ( typeof pauseRemains === "number" ) return "paused " ;
115115 return "waiting" ;
116116 }
117117
118- intervalInstance . start = start ;
119- intervalInstance . stop = stop ;
118+ intervalInstance . pause = pause ;
119+ intervalInstance . resume = resume ;
120120 intervalInstance . clear = clear ;
121121 intervalInstance . state = state ;
122122
123- start ( ) ;
123+ resume ( ) ;
124124
125125 return intervalInstance ;
126126}
0 commit comments