comlink icon indicating copy to clipboard operation
comlink copied to clipboard

Class example not working

Open Dexus opened this issue 6 years ago • 2 comments

I try to run the example 03 with class in my project:

import * as Comlink from 'comlink';
import {
  Game
} from "./src/index.js"

console.log("start 1");
async function init() {
  console.log("start 2");
  const MyClass = Comlink.wrap(new Worker("worker.js"));
  console.log("start 3");
  console.log(MyClass);
  let worker = new MyClass();
  console.log("start 4");
  console.log(worker);
  let game = new Game(worker);
  console.log("start 5");
  console.log(game);
}
init().catch(console.trace);

worker.js:

import * as Comlink from 'comlink';
import("../../rust-src/pkg").then(wasm => {
  wasm.init();
  class MyClass {

    constructor() {
      console.log("Worker started");
    }

    request() {
      return wasm.request()
    }

    chacha20poly1305_decrypt(key, noce, str) {
      return wasm.chacha20poly1305_decrypt(key, noce, str)
    }

    chacha20poly1305_encrypt(key, noce, str) {
      return wasm.chacha20poly1305_encrypt(key, noce, str)
    }

    aes_encrypt(str) {
      return wasm.aes_encrypt(str)
    }
    hash(str) {
      return wasm.hash(str)
    }
  }

  Comlink.expose(MyClass);
});

But the await blocks the script as shown in the example. Is the example outdated or what is wrong?

Dexus avatar Dec 01 '19 22:12 Dexus

Can you put your example in a glitch or something? I wonder if it has something to do with the fact that you call expose async, but technically that should work 🤔

surma avatar Dec 02 '19 13:12 surma

I'm running into a similar problem, and I think it's related to WebAssembly. Everything works fine if I comment the line of code in my worker that calls a wasm function.

frnsys avatar Jul 18 '21 14:07 frnsys