Skip to content

Commit

Permalink
deprecate misc.callInNextTick, use component.scheduleOnce (#16373)
Browse files Browse the repository at this point in the history
* deprecate `misc.callInNextTick`, use `component.scheduleOnce`
  • Loading branch information
PPpro authored Oct 11, 2023
1 parent 4053bad commit 9df8de8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
6 changes: 3 additions & 3 deletions cocos/asset/asset-manager/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
}
}
Expand Down
3 changes: 2 additions & 1 deletion cocos/asset/asset-manager/release-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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));
}
}

Expand Down
3 changes: 2 additions & 1 deletion cocos/asset/asset-manager/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
});
Expand Down
14 changes: 14 additions & 0 deletions cocos/core/utils/internal.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { setTimeoutRAF } from "@pal/utils";

Check warning on line 1 in cocos/core/utils/internal.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Strings must use singlequote

/**
* @zh
* 重命名对象[自身的、可枚举的](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable)指定属性,并且保持原本的属性顺序。
Expand Down Expand Up @@ -208,3 +210,15 @@ type ExcludeConstructor<T> = T;

// eslint-disable-next-line @typescript-eslint/ban-types
type CreateInstanceofProxySignature = <TConstructor extends Function> (constructor: TConstructor) => ExcludeConstructor<TConstructor>;

/**
* 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);
}
12 changes: 7 additions & 5 deletions cocos/core/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9df8de8

Please sign in to comment.