opus-codec-worker 0.0.9 → 0.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/actions/Client.d.ts +7 -0
- package/actions/Client.js +42 -0
- package/package.json +1 -1
- package/worker.js +1 -2457
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { IWorkerRequest, RequestResponse, RequestResponseType } from './actions';
|
|
2
|
+
export default class Client {
|
|
3
|
+
#private;
|
|
4
|
+
constructor(worker: Worker);
|
|
5
|
+
sendMessage<T extends IWorkerRequest<unknown, unknown>>(data: T): Promise<RequestResponse<RequestResponseType<T>>>;
|
|
6
|
+
private onMessage;
|
|
7
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
const autobind_decorator_1 = require("autobind-decorator");
|
|
10
|
+
class Client {
|
|
11
|
+
#worker;
|
|
12
|
+
#pending = new Map();
|
|
13
|
+
constructor(worker) {
|
|
14
|
+
this.#worker = worker;
|
|
15
|
+
worker.addEventListener('message', this.onMessage);
|
|
16
|
+
}
|
|
17
|
+
sendMessage(data) {
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
if (this.#pending.has(data.requestId)) {
|
|
20
|
+
reject(new Error(`Request already exists: ${data.requestId}`));
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
this.#pending.set(data.requestId, (data) => {
|
|
24
|
+
resolve(data);
|
|
25
|
+
});
|
|
26
|
+
this.#worker.postMessage(data);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
onMessage(e) {
|
|
30
|
+
const data = e.data;
|
|
31
|
+
const request = this.#pending.get(data.requestId);
|
|
32
|
+
if (!request) {
|
|
33
|
+
console.error('failed to get pending request: %s', data.requestId);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
request(data);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
__decorate([
|
|
40
|
+
autobind_decorator_1.boundMethod
|
|
41
|
+
], Client.prototype, "onMessage", null);
|
|
42
|
+
exports.default = Client;
|