Skip to content

🎥 Make videos programmatically with React

Update extract-frame-via-broadcast-channel.ts

JonnyBurger2629712468b8
diff --git a/packages/media/src/video-extraction/extract-frame-via-broadcast-channel.ts b/packages/media/src/video-extraction/extract-frame-via-broadcast-channel.ts
index 408a71256c..4045e2e876 100644
--- a/packages/media/src/video-extraction/extract-frame-via-broadcast-channel.ts
+++ b/packages/media/src/video-extraction/extract-frame-via-broadcast-channel.ts
@@ -186,6 +186,7 @@ export const extractFrameViaBroadcastChannel = ({
}
| 'cannot-decode'
| 'network-error'
+ | 'unknown-container-format'
>((resolve, reject) => {
const onMessage = (event: MessageEvent) => {
const data = event.data as ExtractFrameResponse;
@@ -194,7 +195,11 @@ export const extractFrameViaBroadcastChannel = ({
return;
}
- if (data.type === 'response-success' && data.id === requestId) {
+ if (data.id !== requestId) {
+ return;
+ }
+
+ if (data.type === 'response-success') {
resolve({
frame: data.frame ? data.frame : null,
audio: data.audio ? data.audio : null,
@@ -206,31 +211,48 @@ export const extractFrameViaBroadcastChannel = ({
'message',
onMessage,
);
- } else if (data.type === 'response-error' && data.id === requestId) {
+ return;
+ }
+
+ if (data.type === 'response-error') {
reject(data.errorStack);
window.remotion_broadcastChannel!.removeEventListener(
'message',
onMessage,
);
- } else if (
- data.type === 'response-cannot-decode' &&
- data.id === requestId
- ) {
+ return;
+ }
+
+ if (data.type === 'response-cannot-decode') {
resolve('cannot-decode');
window.remotion_broadcastChannel!.removeEventListener(
'message',
onMessage,
);
- } else if (
- data.type === 'response-network-error' &&
- data.id === requestId
- ) {
+ return;
+ }
+
+ if (data.type === 'response-network-error') {
resolve('network-error');
window.remotion_broadcastChannel!.removeEventListener(
'message',
onMessage,
);
+ return;
}
+
+ if (data.type === 'response-unknown-container-format') {
+ resolve('unknown-container-format');
+ window.remotion_broadcastChannel!.removeEventListener(
+ 'message',
+ onMessage,
+ );
+ return;
+ }
+
+ throw new Error(
+ `Invalid message: ${JSON.stringify(data satisfies never)}`,
+ );
};
window.remotion_broadcastChannel!.addEventListener('message', onMessage);