Skip to content

Commit 659f2dd

Browse files
committed
Allow web-renderer to override wasm binary url
1 parent 86c379b commit 659f2dd

File tree

2 files changed

+19
-28
lines changed

2 files changed

+19
-28
lines changed

js/packages/web-renderer/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,16 @@ export default class WebRenderer extends EventEmitter {
6363
this._promiseMap = new Map();
6464
this._nextRequestId = 0;
6565

66-
const wasmBinaryUrl = new URL("./elementary-wasm.wasm", import.meta.url);
66+
const { processorOptions = {}, ...otherOptions } = workletOptions;
67+
68+
const wasmBinaryUrl =
69+
processorOptions.wasmBinaryUrl ??
70+
new URL("./elementary-wasm.wasm", import.meta.url);
71+
6772
const wasmBinary = await fetch(wasmBinaryUrl).then((response) =>
6873
response.arrayBuffer(),
6974
);
7075

71-
const { processorOptions, ...otherOptions } = workletOptions;
72-
7376
this._worklet = new AudioWorkletNode(
7477
audioContext,
7578
`ElementaryAudioWorkletProcessor@${pkgVersion}`,

js/packages/web-renderer/index.worklet.js

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import invariant from "invariant";
12
import Module from "./raw/elementary-wasm.js";
23

34
const EventTypes = {
@@ -44,14 +45,10 @@ class ElementaryAudioWorkletProcessor extends AudioWorkletProcessor {
4445
0,
4546
);
4647

47-
const hasProcOpts2 =
48-
options.hasOwnProperty("processorOptions") &&
49-
typeof options.processorOptions === "object" &&
50-
options.processorOptions !== null;
48+
const { wasmBinary, virtualFileSystem, ...other } =
49+
options.processorOptions;
5150

52-
const wasmBinary = hasProcOpts2
53-
? options.processorOptions.wasmBinary
54-
: null;
51+
invariant(wasmBinary instanceof ArrayBuffer, "Invalid wasm binary");
5552

5653
Module({
5754
instantiateWasm: async function (imports, receiveInstance) {
@@ -78,26 +75,17 @@ class ElementaryAudioWorkletProcessor extends AudioWorkletProcessor {
7875
// See: https://webaudio.github.io/web-audio-api/#rendering-loop
7976
this._native.prepare(sampleRate, 128);
8077

81-
const hasProcOpts =
82-
options.hasOwnProperty("processorOptions") &&
83-
typeof options.processorOptions === "object" &&
84-
options.processorOptions !== null;
78+
const validVFS =
79+
typeof virtualFileSystem === "object" &&
80+
virtualFileSystem !== null &&
81+
Object.keys(virtualFileSystem).length > 0;
8582

86-
if (hasProcOpts) {
87-
const { virtualFileSystem, ...other } = options.processorOptions;
83+
if (validVFS) {
84+
for (let [key, val] of Object.entries(virtualFileSystem)) {
85+
let result = this._native.addSharedResource(key, val);
8886

89-
const validVFS =
90-
typeof virtualFileSystem === "object" &&
91-
virtualFileSystem !== null &&
92-
Object.keys(virtualFileSystem).length > 0;
93-
94-
if (validVFS) {
95-
for (let [key, val] of Object.entries(virtualFileSystem)) {
96-
let result = this._native.addSharedResource(key, val);
97-
98-
if (!result.success) {
99-
this.port.postMessage(["error", result.message]);
100-
}
87+
if (!result.success) {
88+
this.port.postMessage(["error", result.message]);
10189
}
10290
}
10391
}

0 commit comments

Comments
 (0)