Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Immediate queue management #16120

Merged
merged 5 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/dev/core/src/Engines/abstractEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { Observable } from "../Misc/observable";
import { EngineFunctionContext, _loadFile } from "./abstractEngine.functions";
import type { Material } from "core/Materials/material";
import { _GetCompatibleTextureLoader } from "core/Materials/Textures/Loaders/textureLoaderManager";
import { _RunImmediateQueue } from "core/Misc/timingTools";

/**
* Defines the interface used by objects working like Scene
Expand Down Expand Up @@ -216,6 +217,9 @@ export abstract class AbstractEngine {

protected _activeRequests: IFileRequest[] = [];

/** @internal */
public _immediateQueue: Array<() => void> = [];
deltakosh marked this conversation as resolved.
Show resolved Hide resolved

/** @internal */
public _badOS = false;
/** @internal */
Expand Down Expand Up @@ -880,6 +884,9 @@ export abstract class AbstractEngine {
this._frameId++;

this.onEndFrameObservable.notifyObservers(this);

/** It is ok to call it even if several engines run */
_RunImmediateQueue();
}

/**
Expand Down
28 changes: 21 additions & 7 deletions packages/dev/core/src/Misc/timingTools.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsWindowObjectExist } from "./domManagement";
const ImmediateQueue: Array<() => void> = [];

/**
* Class used to provide helper for timing
Expand All @@ -9,12 +9,26 @@ export class TimingTools {
* @param action defines the action to execute after the current execution block
*/
public static SetImmediate(action: () => void) {
if (IsWindowObjectExist() && window.setImmediate) {
// Note - deprecated and should not be used directly. Not supported in any browser.
window.setImmediate(action);
} else {
setTimeout(action, 1);
}
ImmediateQueue.push(action);
}
}

/**
* @internal
*/
export function _RunImmediateQueue() {
if (ImmediateQueue.length) {
// Execute all immediate functions
// Doing a copy even though it should be fine but I do not want to deal with race conditions
// in the future. So, let's be safe.
const functionsToCall = ImmediateQueue.slice(0);
ImmediateQueue.length = 0;

setTimeout(() => {
for (let i = 0; i < functionsToCall.length; i++) {
functionsToCall[i]();
}
}, 1);
}
}

Expand Down
Loading