From 9df8de851f6a109f5fa80529f56f59b1e6ce63c9 Mon Sep 17 00:00:00 2001 From: PP Date: Wed, 11 Oct 2023 16:47:12 +0800 Subject: [PATCH] deprecate `misc.callInNextTick`, use `component.scheduleOnce` (#16373) * deprecate `misc.callInNextTick`, use `component.scheduleOnce` --- cocos/asset/asset-manager/downloader.ts | 6 +++--- cocos/asset/asset-manager/release-manager.ts | 3 ++- cocos/asset/asset-manager/utilities.ts | 3 ++- cocos/core/utils/internal.ts | 14 ++++++++++++++ cocos/core/utils/misc.ts | 12 +++++++----- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/cocos/asset/asset-manager/downloader.ts b/cocos/asset/asset-manager/downloader.ts index b6e6f6b3cd1..c9e13b4ed4a 100644 --- a/cocos/asset/asset-manager/downloader.ts +++ b/cocos/asset/asset-manager/downloader.ts @@ -25,10 +25,10 @@ import { BUILD, EDITOR, EDITOR_NOT_IN_PREVIEW } from 'internal:constants'; import { cclegacy } from '@base/global'; import { js } from '@base/utils'; +import { callInNextTick } from '../../core/utils/internal'; import { sys, misc, path } from '../../core'; import Cache from './cache'; -import downloadFile from './download-file'; -import { FileProgressCallback } from './download-file'; +import downloadFile, { FileProgressCallback } from './download-file'; import downloadScript from './download-script'; import { files } from './shared'; import { retry, RetryFunction, urlAppendTimestamp } from './utilities'; @@ -533,7 +533,7 @@ export class Downloader { private _handleQueueInNextFrame (maxConcurrency: number, maxRequestsPerFrame: number): void { if (!this._checkNextPeriod && this._queue.length > 0) { - misc.callInNextTick(this._handleQueue.bind(this), maxConcurrency, maxRequestsPerFrame); + callInNextTick(this._handleQueue.bind(this), maxConcurrency, maxRequestsPerFrame); this._checkNextPeriod = true; } } diff --git a/cocos/asset/asset-manager/release-manager.ts b/cocos/asset/asset-manager/release-manager.ts index 388f63941b1..d1f5cd42397 100644 --- a/cocos/asset/asset-manager/release-manager.ts +++ b/cocos/asset/asset-manager/release-manager.ts @@ -24,6 +24,7 @@ import { EDITOR, TEST } from 'internal:constants'; import { js } from '@base/utils'; +import { callInNextTick } from '../../core/utils/internal'; import { Asset } from '../assets/asset'; import { isValid, misc } from '../../core'; import { Node, Scene } from '../../scene-graph'; @@ -217,7 +218,7 @@ class ReleaseManager { if (TEST) return; if (!this._eventListener) { this._eventListener = true; - misc.callInNextTick(this._freeAssets.bind(this)); + callInNextTick(this._freeAssets.bind(this)); } } diff --git a/cocos/asset/asset-manager/utilities.ts b/cocos/asset/asset-manager/utilities.ts index 16ea9209d89..a0b7b4ca504 100644 --- a/cocos/asset/asset-manager/utilities.ts +++ b/cocos/asset/asset-manager/utilities.ts @@ -26,6 +26,7 @@ import { EDITOR } from 'internal:constants'; import { cclegacy } from '@base/global'; import { error } from '@base/debug'; import { js } from '@base/utils'; +import { callInNextTick } from '../../core/utils/internal'; import { Asset } from '../assets/asset'; import { misc } from '../../core'; import Config from './config'; @@ -308,7 +309,7 @@ export function asyncify (cb: ((p1?: any, p2?: any) => void) | null): (p1?: any, } else if (p2 instanceof Asset) { refs.push(p2.addRef()); } - misc.callInNextTick((): void => { + callInNextTick((): void => { refs.forEach((x): Asset => x.decRef(false)); cb(p1, p2); }); diff --git a/cocos/core/utils/internal.ts b/cocos/core/utils/internal.ts index 6504a129d21..b123815e3de 100644 --- a/cocos/core/utils/internal.ts +++ b/cocos/core/utils/internal.ts @@ -1,3 +1,5 @@ +import { setTimeoutRAF } from "@pal/utils"; + /** * @zh * 重命名对象[自身的、可枚举的](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable)指定属性,并且保持原本的属性顺序。 @@ -208,3 +210,15 @@ type ExcludeConstructor = T; // eslint-disable-next-line @typescript-eslint/ban-types type CreateInstanceofProxySignature = (constructor: TConstructor) => ExcludeConstructor; + +/** + * Invoke a function in next frame. + * @param callback The function to be invoked next frame. + * @param p1 The first parameter passed to `callback`. + * @param p2 The seconde parameter passed to `callback`. + */ +export function callInNextTick (callback: AnyFunction, p1?: unknown, p2?: unknown): void { + setTimeoutRAF(() => { + callback(p1, p2); + }, 0); +} diff --git a/cocos/core/utils/misc.ts b/cocos/core/utils/misc.ts index dda0daf3dbf..6ca517d2ec9 100644 --- a/cocos/core/utils/misc.ts +++ b/cocos/core/utils/misc.ts @@ -31,6 +31,7 @@ import { cclegacy } from '@base/global'; import { warnID } from '@base/debug'; import { js } from '@base/utils'; import { isPlainEmptyObj } from '@base/utils/internal'; +import { callInNextTick as _callInNextTick } from './internal'; import { toRadian, toDegree, clamp } from '../math'; import values from '../../../external/compression/base64-values'; @@ -100,13 +101,14 @@ export function isDomNode (node): boolean { * @param callback @en The function to be invoked next frame. @zh 下一帧要执行的函数。 * @param p1 @en The first parameter passed to `callback`. @zh 传给回调函数的第一个参数。 * @param p2 @en The seconde parameter passed to `callback`. @zh 传给回调函数的第二个参数。 + * + * @deprecated since v3.9.0, please use `component.scheduleOnce(callback)` instead. */ -export function callInNextTick (callback, p1?: any, p2?: any): void { - if (callback) { - setTimeoutRAF((): void => { - callback(p1, p2); - }, 0); +export function callInNextTick (callback: AnyFunction, p1?: unknown, p2?: unknown): void { + if (DEBUG) { + warnID(16001, 'misc.callInNextTick', '3.9.0', 'component.scheduleOnce'); } + _callInNextTick(callback, p1, p2); } // use anonymous function here to ensure it will not being hoisted without EDITOR