Code I have so far : proc addModule*(worklet: AudioWorklet, moduleURL: cstring) : Future {.importjs: "#.addModule(@)".} fails compilation with Error: cannot instantiate 'Future[T]' inside of type definition: 'addModule'; Maybe generic arguments are missing?
Context : trying to add AudioWorklet support to nim-webaudio
per https://developer.mozilla.org/en-US/docs/Web/API/Worklet/addModule :
Worklet.addModule()
...
Return value
A Promise that resolves once the module from the given URL has been added. The promise doesn't return any value.
Okay... so I threw a couple of sticks, one of which seems to work : Future[jsNull] .
Final declaration proc addModule*(worklet: AudioWorklet, moduleURL: cstring) : Future[jsNull] {.importjs: "#.addModule(@)".}
proc addModule*(worklet: AudioWorklet; moduleURL: cstring) : Future[void] {.importjs: "#.$1(#)".}
@juancarlospaco hi.
I followed your advice and am still having client code compilation errors.
Here is what I have so far: Declaration:
proc addModule*(worklet: AudioWorklet, moduleURL: cstring) : Future[void] {.importjs: "#.$1(#)".}
Client code:
proc init(c : AudioContext) : AudioWorkletNode {.async.} =
discard await c.audioWorklet.addModule("worklets/random-noise-processor.js")
var n = newAudioWorkletNode(c, "random-noise-processor")
console.log("1", n)
return n
var r : AudioWorkletNode = init()
Compilation error:
$ nim js test_worklet.nim
Error: expression 'await addModule(c.audioWorklet, "worklets/random-noise-processor.js")' has no type (or is ambiguous)
Then, changing the declaration to this:
proc addModule*(worklet: AudioWorklet, moduleURL: cstring) : Future[jsNull] {.importjs: "#.$1(#)".}
gives this error:
Error: type mismatch: got 'Future[webaudio.AudioWorkletNode]' for 'jsResolve(n)' but expected 'AudioWorkletNode = ref AudioWorkletNodeObj'