-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy path07-abort-controller.ts
More file actions
53 lines (46 loc) · 1.07 KB
/
Copy path07-abort-controller.ts
File metadata and controls
53 lines (46 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Example 07 — AbortController
*
* Shows how to cancel a rendering operation mid-flight using an
* AbortController. Useful for UIs where a user may cancel a render.
*
* Run:
* npx tsx examples/07-abort-controller.ts
*/
import VideoFlow from '@videoflow/core';
const $ = new VideoFlow({
name: 'Abort Demo',
width: 1280,
height: 720,
fps: 30,
});
const text = $.addText({
text: 'This render will be cancelled…',
fontSize: 2,
fontWeight: 600,
color: '#ff6b6b',
});
text.fadeIn('1s');
$.wait('10s'); // Long video to give us time to abort
text.fadeOut('1s');
$.wait('500ms');
// Create an AbortController and cancel after 2 seconds
const controller = new AbortController();
setTimeout(() => {
console.log('Aborting render…');
controller.abort();
}, 2000);
try {
await $.renderVideo({
outputType: 'file',
output: './07-abort-controller.mp4',
signal: controller.signal,
verbose: true,
});
} catch (err: any) {
if (err.name === 'AbortError' || controller.signal.aborted) {
console.log('Render was successfully cancelled.');
} else {
throw err;
}
}