🎥 Make videos programmatically with React
should close page
Jonny Burgerf02ad1ac6d7b
diff --git a/packages/renderer/src/browser/DOMWorld.ts b/packages/renderer/src/browser/DOMWorld.tsindex 4306c244f7..cb49e89263 100644--- a/packages/renderer/src/browser/DOMWorld.ts+++ b/packages/renderer/src/browser/DOMWorld.ts@@ -122,11 +122,13 @@ export class DOMWorld {timeout,pageFunction,title,+ shouldClosePage,}: {browser: HeadlessBrowser;timeout: number;pageFunction: Function | string;title: string;+ shouldClosePage: boolean;}): Promise<JSHandle> {return new WaitTask({domWorld: this,@@ -135,6 +137,7 @@ export class DOMWorld {timeout,args: [],browser,+ shouldClosePage,}).promise;}@@ -152,6 +155,7 @@ interface WaitTaskOptions {timeout: number;browser: HeadlessBrowser;args: SerializableOrJSHandle[];+ shouldClosePage: boolean;}const noop = (): void => undefined;@@ -167,6 +171,7 @@ class WaitTask {#timeoutTimer?: NodeJS.Timeout;#terminated = false;#browser: HeadlessBrowser;+ #shouldClosePage: boolean;promise: Promise<JSHandle>;@@ -184,6 +189,7 @@ class WaitTask {this.#predicateBody = getPredicateBody(options.predicateBody);this.#args = options.args;this.#runCount = 0;+ this.#shouldClosePage = options.shouldClosePage;this.#domWorld._waitTasks.add(this);this.promise = new Promise<JSHandle>((resolve, reject) => {@@ -197,7 +203,11 @@ class WaitTask {`waiting for ${options.title} failed: timeout ${options.timeout}ms exceeded`);this.#timeoutTimer = setTimeout(() => {- return this.terminate(timeoutError);+ if (this.#shouldClosePage) {+ return this.terminate(timeoutError);+ }++ return this.#reject(timeoutError);}, options.timeout);}diff --git a/packages/renderer/src/seek-to-frame.ts b/packages/renderer/src/seek-to-frame.tsindex 9f56127b2f..5153e42226 100644--- a/packages/renderer/src/seek-to-frame.ts+++ b/packages/renderer/src/seek-to-frame.ts@@ -1,6 +1,8 @@+import {RenderInternals} from '.';import {BrowserEmittedEvents} from './browser/Browser';import type {Page} from './browser/BrowserPage';import {PageEmittedEvents} from './browser/BrowserPage';+import type {JSHandle} from './browser/JSHandle';import {SymbolicateableError} from './error-handling/symbolicateable-error';import {parseStack} from './parse-browser-error-stack';import {puppeteerEvaluateWithCatch} from './puppeteer-evaluate';@@ -13,27 +15,67 @@ export const waitForReady = ({page: Page;timeoutInMilliseconds: number;frame: number | null;-}) =>- Promise.race([- new Promise((_, reject) => {- page.on(PageEmittedEvents.Disposed, () => {- reject(new Error('Target closed (page disposed)'));- });- }),- new Promise((_, reject) => {- page.browser.on(BrowserEmittedEvents.ClosedSilent, () => {- reject(new Error('Target closed'));+}) => {+ const waitForReadyProm = new Promise<JSHandle>((resolve, reject) => {+ page+ .mainFrame()+ ._mainWorld.waitForFunction({+ browser: page.browser,+ timeout: timeoutInMilliseconds,+ pageFunction: 'window.remotion_renderReady === true',+ title:+ frame === null+ ? 'the page to render the React component'+ : `the page to render the React component at frame ${frame}`,+ shouldClosePage: false,+ })+ .then((a) => resolve(a))+ .catch((err) => {+ if (+ (err as Error).message.includes('timeout') &&+ (err as Error).message.includes('exceeded')+ ) {+ puppeteerEvaluateWithCatch({+ pageFunction: () => {+ return Object.keys(window.remotion_delayRenderTimeouts)+ .map((id, i) => {+ const {label} = window.remotion_delayRenderTimeouts[id];+ if (label === null) {+ return `${i + 1}. (no label)`;+ }++ return `"${i + 1}. ${label}"`;+ })+ .join(', ');+ },+ args: [],+ frame,+ page,+ })+ .then((res) => {+ reject(+ new Error(+ `Timeout exceeded mounting the React component${+ frame ? ' at frame ' + frame : ''+ }. Open delayRender() handles: ${res.value}`+ )+ );+ })+ .catch((newErr) => {+ RenderInternals.Log.warn(+ 'Tried to get delayRender() handles for timeout, but could not do so because of',+ newErr+ );+ // Ignore, use prev error+ reject(err);+ });+ } else {+ reject(err);+ }});- }),- page.mainFrame()._mainWorld.waitForFunction({- browser: page.browser,- timeout: timeoutInMilliseconds,- pageFunction: 'window.remotion_renderReady === true',- title:- frame === null- ? 'the page to render the React component'- : `the page to render the React component at frame ${frame}`,- }),+ });++ const waitForErrorProm = new Promise((_shouldNeverResolve, reject) => {page.mainFrame()._mainWorld.waitForFunction({@@ -41,6 +83,7 @@ export const waitForReady = ({timeout: timeoutInMilliseconds,pageFunction: 'window.remotion_cancelledError !== undefined',title: 'remotion_cancelledError variable to appear on the page',+ shouldClosePage: false,}).then(() => {return puppeteerEvaluateWithCatch({@@ -52,18 +95,47 @@ export const waitForReady = ({}).then(({value: val}) => {if (typeof val !== 'string') {- throw val;+ reject(val);+ return;}- throw new SymbolicateableError({- frame: null,- stack: val,- name: 'CancelledError',- message: val.split('\n')[0],- stackFrame: parseStack(val.split('\n')),- });- }),+ reject(+ new SymbolicateableError({+ frame: null,+ stack: val,+ name: 'CancelledError',+ message: val.split('\n')[0],+ stackFrame: parseStack(val.split('\n')),+ })+ );+ })+ .catch((err) => {+ if (+ (err as Error).message.includes('timeout') &&+ (err as Error).message.includes('exceeded')+ ) {+ // Don't care if a error never appeared+ } else {+ reject(err);+ }+ });+ });++ return Promise.race([+ new Promise((_, reject) => {+ page.on(PageEmittedEvents.Disposed, () => {+ reject(new Error('Target closed (page disposed)'));+ });+ }),+ new Promise((_, reject) => {+ page.browser.on(BrowserEmittedEvents.ClosedSilent, () => {+ reject(new Error('Target closed'));+ });+ }),+ waitForReadyProm,+ waitForErrorProm,]);+};export const seekToFrame = async ({frame,