-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzine-compression-client.js
137 lines (123 loc) · 3.45 KB
/
zine-compression-client.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// import ZineCompressionWorker from './zine-compression-server.js?worker';
import {
makeId,
} from './id-utils.js';
import {
makePromise,
} from './zine-utils.js';
const defaultNumWorkers = 1;
export class ZineCompressionClient {
constructor({
numWorkers = defaultNumWorkers,
} = {}) {
this.workers = [];
for (let i = 0; i < numWorkers; i++) {
// XXX need to localize this
// const u = new URL('/packages/zine/zine-compression-server.js', import.meta.url);
const u = '/packages/zine/zine-compression-server.js';
const worker = new Worker(u, {
type: 'module',
});
// const worker = new ZineCompressionWorker();
const messageChannel = new MessageChannel();
const readPort = messageChannel.port1;
const writePort = messageChannel.port2;
worker.postMessage({
method: 'init',
args: {
port: readPort,
},
}, [readPort]);
worker.port = writePort;
writePort.addEventListener('message', e => {
const {data} = e;
const {id, result, error} = data;
const cb = this.cbs.get(id);
if (cb !== undefined) {
this.cbs.delete(id);
cb(error, result);
} else {
console.warn('zine compression client: no cb for id: ' + id);
}
});
writePort.start();
this.workers.push(worker);
}
this.cbs = new Map();
this.availableWorkers = this.workers.slice();
this.queue = [];
this.waitPromises = [];
}
async waitForAvailableWorker() {
if (this.availableWorkers.length > 0) {
// nothing
} else {
return new Promise((accept, reject) => {
this.waitPromises.push({
accept,
reject,
});
});
}
}
async request(method, args, {transfers} = {}) {
if (this.availableWorkers.length > 0) {
const worker = this.availableWorkers.shift();
try {
const id = makeId();
const promise = makePromise();
this.cbs.set(id, (error, result) => {
if (!error) {
promise.resolve(result);
} else {
promise.reject(error);
}
});
worker.port.postMessage({
id,
method,
args,
}, transfers);
const result = await promise;
return result;
} finally {
this.availableWorkers.push(worker);
if (this.queue.length > 0) {
const {method, args, transfers, accept, reject} = this.queue.shift();
this.request(method, args, {transfers})
.then(accept, reject);
}
}
} else {
return new Promise((accept, reject) => {
this.queue.push({
method,
args,
transfers,
accept,
reject,
});
});
}
}
async compress(type, value, {transfers} = {}) {
const result = await this.request('compress', {
type,
value,
}, {transfers});
return result;
}
async decompress(type, value, {transfers} = {}) {
const result = await this.request('decompress', {
type,
value,
}, {transfers});
return result;
}
destroy() {
for (let i = 0; i < this.workers.length; i++) {
const worker = this.workers[i];
worker.terminate();
}
}
}