How to pass a function in JS worklet post message call?

297 views Asked by At

I need my worklet to use a function that is in the calling TS file. Since in postMessage only strings and numbers are allowed. I am finding it difficult to pass a function. Is this even possible ?

calling.ts

        // processAudio is the function (This is how i send data to worklet function)
        this.socketWorkletNode.port.postMessage({
            type: 'setState', state: state,
            processAudio: this.filterFunctions.processAudio
        });

worklet.js

class audio_process_worklet {
  ...
  handleMessage_(event) {
        // If process function is available then set it in the class
        if(event.data.processAudio) {
            this.processedData = event.data.processAudio(this.audioData);
        }
  }
  ...
}

More details: The reason i need to pass the function is because it is a function i got through WebAssembly. Its actually is a function written in C code. I compiled it as a WASM file using emcc compiler and served the file from the server. Using WebAssembly module in the browser, i get the function.

0

There are 0 answers