11import {
22 cancelAnimationFrame ,
3+ cancelIdleCallback ,
34 clearTimeout ,
45 nativePostTask ,
56 NOOP ,
67 requestAnimationFrame ,
8+ requestIdleCallback ,
79 setTimeout ,
810 TELEMETRY_FREQUENCY_30PS ,
911 TIME_60FPS_MS ,
@@ -119,6 +121,39 @@ type TTimerOptions =
119121 | TTimerIdle
120122 | TTimerTask ;
121123
124+ const timerApi = __mirror__
125+ ? {
126+ get setTimeout ( ) {
127+ return globalThis . setTimeout ;
128+ } ,
129+ get clearTimeout ( ) {
130+ return globalThis . clearTimeout ;
131+ } ,
132+ get requestAnimationFrame ( ) {
133+ return globalThis . requestAnimationFrame ;
134+ } ,
135+ get cancelAnimationFrame ( ) {
136+ return globalThis . cancelAnimationFrame ;
137+ } ,
138+ get requestIdleCallback ( ) {
139+ return globalThis . requestIdleCallback ;
140+ } ,
141+ get cancelIdleCallback ( ) {
142+ return globalThis . cancelIdleCallback ;
143+ } ,
144+ get postTask ( ) {
145+ return globalThis . scheduler . postTask ;
146+ } ,
147+ }
148+ : {
149+ setTimeout,
150+ clearTimeout,
151+ requestAnimationFrame,
152+ cancelAnimationFrame,
153+ requestIdleCallback,
154+ cancelIdleCallback,
155+ postTask : nativePostTask ,
156+ } as const ;
122157/**
123158 * A unification of ways to delay a callback execution
124159 * in javascript event-loop
@@ -158,27 +193,28 @@ export class Timer {
158193 if (
159194 this . #options. type === ETimer . TIMEOUT
160195 ) {
161- this . #handler = setTimeout ( ( ) => {
196+ this . #handler = timerApi . setTimeout ( ( ) => {
162197 this . #handler = 0 ;
163198 this . trigger ( ...args ) ;
164199 } , this . delay ) ;
165200 } else if (
166201 this . #options. type === ETimer . ANIMATION
167202 ) {
168- this . #handler = requestAnimationFrame ( ( ...argsAF ) => {
203+ this . #handler = timerApi . requestAnimationFrame ( ( ...argsAF ) => {
169204 this . #handler = 0 ;
170205 this . trigger ( ...[ ...args , ...argsAF ] ) ;
171206 } ) ;
172207 } else if ( this . #options. type === ETimer . IDLE ) {
173- this . #handler = requestIdleCallback ( ( ...argsIC ) => {
208+ this . #handler = timerApi . requestIdleCallback ( ( ...argsIC ) => {
174209 this . #handler = 0 ;
175210 this . trigger ( ...[ ...args , ...argsIC ] ) ;
176211 } , { timeout : this . delay } ) ;
177212 } else if ( this . #options. type === ETimer . TASK ) {
178213 this . #abortController = new AbortController ( ) ;
179- nativePostTask ( ( ) => {
180- this . #abortController = null ;
214+ timerApi . postTask ( ( ) => {
181215 this . trigger ( ...args ) ;
216+ // nullifying AFTER the trigger to allow use-case when aborting from the callback
217+ this . #abortController = null ;
182218 } , {
183219 delay : this . delay ,
184220 signal : this . #abortController. signal ,
@@ -203,13 +239,13 @@ export class Timer {
203239
204240 stop ( ) {
205241 if ( this . #options. type === ETimer . TIMEOUT ) {
206- this . #handler && clearTimeout ( this . #handler) ;
242+ this . #handler && timerApi . clearTimeout ( this . #handler) ;
207243 this . #handler = 0 ;
208244 } else if ( this . #options. type === ETimer . ANIMATION ) {
209- this . #handler && cancelAnimationFrame ( this . #handler) ;
245+ this . #handler && timerApi . cancelAnimationFrame ( this . #handler) ;
210246 this . #handler = 0 ;
211247 } else if ( this . #options. type === ETimer . IDLE ) {
212- this . #handler && cancelIdleCallback ( this . #handler) ;
248+ this . #handler && timerApi . cancelIdleCallback ( this . #handler) ;
213249 this . #handler = 0 ;
214250 } else if ( this . #options. type === ETimer . TASK ) {
215251 this . #abortController && this . #abortController. abort ( ) ;
0 commit comments