@@ -248,58 +248,59 @@ const reducePropsToState = propsList => ({
248
248
)
249
249
} ) ;
250
250
251
- const requestIdleCallback = ( ( ) => {
252
- if (
253
- typeof window !== "undefined" &&
254
- typeof window . requestIdleCallback !== "undefined"
255
- ) {
256
- return window . requestIdleCallback ;
257
- }
258
-
259
- return cb => {
260
- const start = Date . now ( ) ;
261
- return setTimeout ( ( ) => {
262
- cb ( {
263
- didTimeout : false ,
264
- timeRemaining ( ) {
265
- return Math . max ( 0 , 50 - ( Date . now ( ) - start ) ) ;
266
- }
267
- } ) ;
268
- } , 1 ) ;
251
+ const rafPolyfill = ( ( ) => {
252
+ let clock = Date . now ( ) ;
253
+
254
+ return ( callback : Function ) => {
255
+ const currentTime = Date . now ( ) ;
256
+
257
+ if ( currentTime - clock > 16 ) {
258
+ clock = currentTime ;
259
+ callback ( currentTime ) ;
260
+ } else {
261
+ setTimeout ( ( ) => {
262
+ rafPolyfill ( callback ) ;
263
+ } , 0 ) ;
264
+ }
269
265
} ;
270
266
} ) ( ) ;
271
267
272
- const cancelIdleCallback = ( ( ) => {
273
- if (
274
- typeof window !== "undefined" &&
275
- typeof window . cancelIdleCallback !== "undefined"
276
- ) {
277
- return window . cancelIdleCallback ;
278
- }
268
+ const cafPolyfill = ( id : string | number ) => clearTimeout ( id ) ;
279
269
280
- return id => clearTimeout ( id ) ;
281
- } ) ( ) ;
270
+ const requestAnimationFrame = typeof window !== "undefined"
271
+ ? window . requestAnimationFrame ||
272
+ window . webkitRequestAnimationFrame ||
273
+ window . mozRequestAnimationFrame ||
274
+ rafPolyfill
275
+ : global . requestAnimationFrame || rafPolyfill ;
276
+
277
+ const cancelAnimationFrame = typeof window !== "undefined"
278
+ ? window . cancelAnimationFrame ||
279
+ window . webkitCancelAnimationFrame ||
280
+ window . mozCancelAnimationFrame ||
281
+ cafPolyfill
282
+ : global . cancelAnimationFrame || cafPolyfill ;
282
283
283
284
const warn = msg => {
284
285
return console && typeof console . warn === "function" && console . warn ( msg ) ;
285
286
} ;
286
287
287
- let _helmetIdleCallback = null ;
288
+ let _helmetCallback = null ;
288
289
289
290
const handleClientStateChange = newState => {
290
- if ( _helmetIdleCallback ) {
291
- cancelIdleCallback ( _helmetIdleCallback ) ;
291
+ if ( _helmetCallback ) {
292
+ cancelAnimationFrame ( _helmetCallback ) ;
292
293
}
293
294
294
295
if ( newState . defer ) {
295
- _helmetIdleCallback = requestIdleCallback ( ( ) => {
296
+ _helmetCallback = requestAnimationFrame ( ( ) => {
296
297
commitTagChanges ( newState , ( ) => {
297
- _helmetIdleCallback = null ;
298
+ _helmetCallback = null ;
298
299
} ) ;
299
300
} ) ;
300
301
} else {
301
302
commitTagChanges ( newState ) ;
302
- _helmetIdleCallback = null ;
303
+ _helmetCallback = null ;
303
304
}
304
305
} ;
305
306
@@ -637,5 +638,5 @@ export {convertReactPropstoHtmlAttributes};
637
638
export { handleClientStateChange } ;
638
639
export { mapStateOnServer } ;
639
640
export { reducePropsToState } ;
640
- export { requestIdleCallback } ;
641
+ export { requestAnimationFrame } ;
641
642
export { warn } ;
0 commit comments