add setTimeRAF and replace setTimeout in pacer (#14704)

* add setTimeRAF and replace setTimeout in pacer
This commit is contained in:
mmyduckx
2023-04-10 10:08:45 +08:00
committed by GitHub
parent de9a017226
commit 361ada4fa3
3 changed files with 64 additions and 11 deletions
+2 -1
View File
@@ -30,6 +30,7 @@ import { getClassName, getset, isEmptyObject } from './js';
import { legacyCC } from '../global-exports';
import { warnID } from '../platform/debug';
import { macro } from '../platform/macro';
import { setTimeoutRAF } from '../../../pal/utils';
export const BUILTIN_CLASSID_RE = /^(?:cc|dragonBones|sp|ccsg)\..+/;
@@ -163,7 +164,7 @@ export function isDomNode (node) {
*/
export function callInNextTick (callback, p1?: any, p2?: any) {
if (callback) {
setTimeout(() => {
setTimeoutRAF(() => {
callback(p1, p2);
}, 0);
}
+3 -2
View File
@@ -23,6 +23,7 @@
*/
import { assertIsTrue } from '../../cocos/core/data/utils/asserts';
import { clearTimeoutRAF, setTimeoutRAF } from '../utils';
export class Pacer {
private _rafHandle = 0;
@@ -118,11 +119,11 @@ export class Pacer {
const currTime = performance.now();
const elapseTime = Math.max(0, (currTime - this._startTime));
const timeToCall = Math.max(0, this._frameTime - elapseTime);
const id = setTimeout(callback, timeToCall);
const id = setTimeoutRAF(callback, timeToCall);
return id;
}
private _ctTime (id: number | undefined) {
clearTimeout(id);
clearTimeoutRAF(id);
}
}
+59 -8
View File
@@ -55,10 +55,10 @@ export function createInnerAudioContextPolyfill (minigameEnv: any, polyfillConfi
// add polyfill if onPlay method doesn't work this platform
if (polyfillConfig.onPlay) {
const originalPlay = audioContext.play;
let _onPlayCB: (()=> void) | null = null;
let _onPlayCB: (() => void) | null = null;
Object.defineProperty(audioContext, 'onPlay', {
configurable: true,
value (cb: ()=> void) {
value (cb: () => void) {
_onPlayCB = cb;
},
});
@@ -80,10 +80,10 @@ export function createInnerAudioContextPolyfill (minigameEnv: any, polyfillConfi
// add polyfill if onPause method doesn't work this platform
if (polyfillConfig.onPause) {
const originalPause = audioContext.pause;
let _onPauseCB: (()=> void) | null = null;
let _onPauseCB: (() => void) | null = null;
Object.defineProperty(audioContext, 'onPause', {
configurable: true,
value (cb: ()=> void) {
value (cb: () => void) {
_onPauseCB = cb;
},
});
@@ -105,10 +105,10 @@ export function createInnerAudioContextPolyfill (minigameEnv: any, polyfillConfi
// add polyfill if onStop method doesn't work on this platform
if (polyfillConfig.onStop) {
const originalStop = audioContext.stop;
let _onStopCB: (()=> void) | null = null;
let _onStopCB: (() => void) | null = null;
Object.defineProperty(audioContext, 'onStop', {
configurable: true,
value (cb: ()=> void) {
value (cb: () => void) {
_onStopCB = cb;
},
});
@@ -130,10 +130,10 @@ export function createInnerAudioContextPolyfill (minigameEnv: any, polyfillConfi
// add polyfill if onSeeked method doesn't work on this platform
if (polyfillConfig.onSeek) {
const originalSeek = audioContext.seek;
let _onSeekCB: (()=> void) | null = null;
let _onSeekCB: (() => void) | null = null;
Object.defineProperty(audioContext, 'onSeeked', {
configurable: true,
value (cb: ()=> void) {
value (cb: () => void) {
_onSeekCB = cb;
},
});
@@ -181,3 +181,54 @@ export function versionCompare (versionA: string, versionB: string): number {
}
return 0;
}
/**
* A custom implementation of setTimeout that uses requestAnimationFrame.
* @param callback The function to be executed after a delay.
* @param delay The delay time in milliseconds.
* @param args The arguments to be passed to the callback function.
* @returns A unique identifier for the timer.
*/
export function setTimeoutRAF (callback: (...args: any[]) => void, delay: number, ...args: any[]): number {
const start = performance.now();
const raf = requestAnimationFrame
|| window.requestAnimationFrame
|| window.webkitRequestAnimationFrame
|| window.mozRequestAnimationFrame
|| window.oRequestAnimationFrame
|| window.msRequestAnimationFrame;
if (raf === undefined) {
return setTimeout(callback, delay, ...args);
}
const handleRAF = () => {
if (performance.now() - start < delay) {
raf(handleRAF);
} else {
callback(...args);
}
};
return raf(handleRAF);
}
/**
* Cancels a timer that was created using the rafTimeout function.
* @param id A numeric ID that represents the timer to be canceled.
* @returns Nothing.
*/
export function clearTimeoutRAF (id) {
const raf = requestAnimationFrame
|| window.requestAnimationFrame
|| window.webkitRequestAnimationFrame
|| window.mozRequestAnimationFrame
|| window.oRequestAnimationFrame
|| window.msRequestAnimationFrame;
if (raf === undefined) {
clearTimeout(id);
} else {
cancelAnimationFrame(id);
}
}