|
3 | 3 | * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE |
4 | 4 | */ |
5 | 5 |
|
6 | | -import { NzSafeAny } from 'ng-zorro-antd/core/types'; |
7 | | - |
8 | | -const availablePrefixes = ['moz', 'ms', 'webkit']; |
9 | | - |
10 | | -function requestAnimationFramePolyfill(): typeof requestAnimationFrame { |
11 | | - let lastTime = 0; |
12 | | - return function (callback: FrameRequestCallback): number { |
13 | | - const currTime = new Date().getTime(); |
14 | | - const timeToCall = Math.max(0, 16 - (currTime - lastTime)); |
15 | | - const id = window.setTimeout(() => { |
16 | | - callback(currTime + timeToCall); |
17 | | - }, timeToCall); |
18 | | - lastTime = currTime + timeToCall; |
19 | | - return id; |
20 | | - }; |
21 | | -} |
22 | | - |
23 | | -function getRequestAnimationFrame(): typeof requestAnimationFrame { |
24 | | - if (typeof window === 'undefined') { |
25 | | - return () => 0; |
26 | | - } |
27 | | - if (window.requestAnimationFrame) { |
28 | | - // https://github.com/vuejs/vue/issues/4465 |
29 | | - return window.requestAnimationFrame.bind(window); |
30 | | - } |
31 | | - |
32 | | - const prefix = availablePrefixes.filter(key => `${key}RequestAnimationFrame` in window)[0]; |
33 | | - |
34 | | - return prefix ? (window as NzSafeAny)[`${prefix}RequestAnimationFrame`] : requestAnimationFramePolyfill(); |
35 | | -} |
36 | | - |
37 | | -export function cancelRequestAnimationFrame(id: number): NzSafeAny { |
38 | | - if (typeof window === 'undefined') { |
39 | | - return null; |
40 | | - } |
41 | | - if (window.cancelAnimationFrame) { |
42 | | - return window.cancelAnimationFrame(id); |
43 | | - } |
44 | | - const prefix = availablePrefixes.filter( |
45 | | - key => `${key}CancelAnimationFrame` in window || `${key}CancelRequestAnimationFrame` in window |
46 | | - )[0]; |
47 | | - |
48 | | - return prefix |
49 | | - ? ( |
50 | | - ((window as NzSafeAny)[`${prefix}CancelAnimationFrame`] as typeof cancelAnimationFrame) || |
51 | | - ((window as NzSafeAny)[`${prefix}CancelRequestAnimationFrame`] as typeof cancelRequestAnimationFrame) |
52 | | - ) |
53 | | - // @ts-ignore |
54 | | - .call(this, id) |
55 | | - : clearTimeout(id); |
56 | | -} |
57 | | - |
58 | | -export const reqAnimFrame = getRequestAnimationFrame(); |
| 6 | +// Note: This falls back to `setTimeout` if `requestAnimationFrame` is |
| 7 | +// unintentionally called on the server, but ideally, we should never attempt |
| 8 | +// to call `requestAnimationFrame` on the server — all invocations should be |
| 9 | +// wrapped with isBrowser. |
| 10 | +export const requestAnimationFrame = |
| 11 | + typeof globalThis.requestAnimationFrame === 'function' ? globalThis.requestAnimationFrame : globalThis.setTimeout; |
| 12 | + |
| 13 | +export const cancelAnimationFrame = |
| 14 | + typeof globalThis.requestAnimationFrame === 'function' ? globalThis.cancelAnimationFrame : globalThis.clearTimeout; |
0 commit comments