Skip to content

Commit

Permalink
支持高级别API通过监听pageCrashed事件监听页面崩溃错误
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinlic committed Nov 22, 2023
1 parent 4ab915a commit d285ed9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -785,12 +785,12 @@ You can actively throw errors in the page to interrupt rendering.

If your page involves heavy computations or consumes excessive memory, it may crash, causing rendering to be interrupted.

If you're using the high-level API, you can be notified of page crashes through the `error` event of the video instance.
If you're using the high-level API, you can be notified of page crashes through the `pageCrashed` event of the video instance.

```javascript
const video = wvc.createSingleVideo({ ... });
// Output "Page crashed:..." when an error occurs
video.on("error", err => console.error(err));
// Output crash errors when an error occurs
video.on("pageCrashed", err => console.error(err));
```

When using the low-level API, you can listen for page crashes through the `crashed` event of the Page instance.
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -788,12 +788,12 @@ video.on("progress", (progress, synthesizedFrameCount, totalFrameCount) => {

如果您的页面存在大量密集计算或者占用过多的运行内存,页面将可能崩溃,从而导致渲染中断。

如果使用高级别API,页面崩溃时通过视频实例的 `error` 事件通知。
如果使用高级别API,页面崩溃时通过视频实例的 `pageCrashed` 事件通知。

```javascript
const video = wvc.createSingleVideo({ ... });
// 错误时输出 Page crashed:...
video.on("error", err => console.error(err));
// 错误时输出崩溃错误
video.on("pageCrashed", err => console.error(err));
```

使用低级别API时,页面崩溃时通过Page实例的 `crashed` 事件通知
Expand Down
14 changes: 13 additions & 1 deletion api/ChunkVideo.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export default class ChunkVideo extends VideoChunk {
// 监听页面实例发生的某些内部错误
page.on("error", err => this._emitError("Page error:\n" + err.stack));
// 监听页面是否崩溃,当内存不足或过载时可能会崩溃
page.on("crashed", err => this._emitError("Page crashed:\n" + err.stack));
page.on("crashed", err => this.#emitPageCrashedError(err));
if (this.consoleLog) {
// 监听页面打印到console的正常日志
page.on("consoleLog", message => logger.log("[page]", message));
Expand Down Expand Up @@ -231,4 +231,16 @@ export default class ChunkVideo extends VideoChunk {
return await this.#pageAcquireFn();
}

/**
* 发送页面崩溃错误
*
* @param {Error} err - 错误对象
*/
#emitPageCrashedError(err) {
if (this.eventNames().includes("pageCrashed"))
this.emit("pageCrashed", err);
else
logger.error("Page crashed:\n" + err.stack);
}

}
14 changes: 13 additions & 1 deletion api/SingleVideo.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export default class SingleVideo extends Synthesizer {
// 监听页面实例发生的某些内部错误
page.on("error", err => this._emitError("Page error:\n" + err.stack));
// 监听页面是否崩溃,当内存不足或过载时可能会崩溃
page.on("crashed", err => this._emitError("Page crashed:\n" + err.stack));
page.on("crashed", err => this.#emitPageCrashedError(err));
if (this.consoleLog) {
// 监听页面打印到console的正常日志
page.on("consoleLog", message => logger.log("[page]", message));
Expand Down Expand Up @@ -226,4 +226,16 @@ export default class SingleVideo extends Synthesizer {
return await this.#pageAcquireFn();
}

/**
* 发送页面崩溃错误
*
* @param {Error} err - 错误对象
*/
#emitPageCrashedError(err) {
if (this.eventNames().includes("pageCrashed"))
this.emit("pageCrashed", err);
else
logger.error("Page crashed:\n" + err.stack);
}

}

0 comments on commit d285ed9

Please sign in to comment.