-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
71 lines (53 loc) · 1.39 KB
/
index.mjs
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { Producer } from 'omnistreams';
// Implementation is based off of this one:
// https://gist.github.com/alediaferia/cfb3a7503039f9278381
export class FileReadProducer extends Producer {
constructor(file, options) {
super()
const opts = options ? options : {}
this._file = file
this._offset = 0
this._chunkSize = opts.chunkSize ? opts.chunkSize : 1024*1024
this._paused = true
this._stop = false
}
_demandChanged() {
if (this._paused) {
this._readChunk()
}
}
_readChunk() {
if (this._stop) {
return
}
if (this._offset < this._file.size) {
this._paused = false
const reader = new FileReader()
reader.onload = (event) => {
if (this._stop) {
return
}
const data = new Uint8Array(event.target.result)
this._dataCallback(data)
this._demand--
this._offset += data.byteLength
if (this._demand > 0) {
this._readChunk()
}
else {
this._paused = true
}
if (this._offset >= this._file.size) {
//console.log("end file stream: " + this.id)
this._endCallback()
}
}
const slice = this._file.slice(this._offset, this._offset + this._chunkSize)
reader.readAsArrayBuffer(slice)
//reader.readAsText(slice)
}
}
_terminate() {
this._stop = true
}
}