-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
39 lines (36 loc) · 1.21 KB
/
index.js
File metadata and controls
39 lines (36 loc) · 1.21 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
import { ReadableStream } from '@mfelements/stream-definitions'
const start = Symbol();
class ReadableMediaStream extends ReadableStream{
/** @type {Promise<string>} */
#streamId;
/** @type {Promise<void>} */
#started;
/** @type {boolean} */
#stopped;
/** @arg {Promise<string>} streamId */
constructor(streamId){
super();
this.#streamId = streamId;
this.#started = new Promise(r => this[start] = r)
}
async *[Symbol.asyncIterator](){
/** @type {{ done: boolean, value: any }} */
let currentValue;
await this.#started;
while(currentValue = await mainThreadAction('getStreamChunk', await this.#streamId), !currentValue.done && !this.#stopped)
// hello Python :)
yield currentValue.value
}
async stop(){
this.#stopped = true;
await mainThreadAction('stopStream', await this.#streamId)
}
}
export function getCameraVideo(options){
const streamId = mainThreadAction('createDirtyStream');
const stream = new ReadableMediaStream(streamId);
streamId
.then(streamId => mainThreadAction('getCameraVideo', streamId, options))
.then(stream[start]);
return stream
}