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

Prevent Uncaught Exception from ThinEngine.runRenderLoop when window.SetTimeout in not defined #13401

Merged
merged 5 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions packages/dev/core/src/Audio/sound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ export class Sound {
this._isReadyToPlay = true;
// Simulating a ready to play event to avoid breaking code path
if (this._readyToPlayCallback) {
window.setTimeout(() => {
setTimeout(() => {
barroij marked this conversation as resolved.
Show resolved Hide resolved
if (this._readyToPlayCallback) {
this._readyToPlayCallback();
}
Expand All @@ -377,7 +377,7 @@ export class Sound {
}
// Simulating a ready to play event to avoid breaking code for non web audio browsers
if (this._readyToPlayCallback) {
window.setTimeout(() => {
setTimeout(() => {
if (this._readyToPlayCallback) {
this._readyToPlayCallback();
}
Expand Down Expand Up @@ -1050,7 +1050,7 @@ export class Sound {
clonedSound.play(0, this._offset, this._length);
}
} else {
window.setTimeout(setBufferAndRun, 300);
setTimeout(setBufferAndRun, 300);
}
};

Expand Down Expand Up @@ -1199,7 +1199,7 @@ export class Sound {
newSound.play(0, newSound._offset, newSound._length);
}
} else {
window.setTimeout(setBufferAndRun, 300);
setTimeout(setBufferAndRun, 300);
}
};

Expand Down
32 changes: 17 additions & 15 deletions packages/dev/core/src/Engines/thinEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5852,31 +5852,33 @@ export class ThinEngine {
}

/**
* Queue a new function into the requested animation frame pool (ie. this function will be executed byt the browser for the next frame)
* Queue a new function into the requested animation frame pool (ie. this function will be executed by the browser (or the javascript engine) for the next frame)
* @param func - the function to be called
* @param requester - the object that will request the next frame. Falls back to window.
* @returns frame number
*/
public static QueueNewFrame(func: () => void, requester?: any): number {
// Note that there is kind of a typing issue here, as `setTimeout` might return something else than a number (NodeJs returns a NodeJS.Timeout object).
// Also if the global `requestAnimationFrame`'s returnType is number, `requester.requestPostAnimationFrame` and `requester.requestAnimationFrame` types
// are `any`.

if (!IsWindowObjectExist()) {
if (typeof requestAnimationFrame !== "undefined") {
if (typeof requestAnimationFrame === "function") {
return requestAnimationFrame(func);
}

return setTimeout(func, 16) as unknown as number;
}

if (!requester) {
requester = window;
}

if (requester.requestPostAnimationFrame) {
return requester.requestPostAnimationFrame(func);
} else if (requester.requestAnimationFrame) {
return requester.requestAnimationFrame(func);
} else {
return window.setTimeout(func, 16);
const { requestPostAnimationFrame, requestAnimationFrame } = requester || window;
if (typeof requestPostAnimationFrame === "function") {
return requestPostAnimationFrame(func);
}
if (typeof requestAnimationFrame === "function") {
return requestAnimationFrame(func);
}
}

// fallback to the global `setTimeout`.
// In most cases (aka in the browser), `window` is the global object, so instead of calling `window.setTimeout` we could call the global `setTimeout`.
return setTimeout(func, 16) as unknown as number;
barroij marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export class TextureEditorComponent extends React.Component<ITextureEditorCompon

textureDidUpdate() {
if (this._timer != null) {
clearTimeout(this._timer);
window.clearTimeout(this._timer);
barroij marked this conversation as resolved.
Show resolved Hide resolved
}
this._timer = window.setTimeout(() => {
this.props.onUpdate();
Expand Down