worker_threads#SHARE_ENV TypeScript Examples

The following examples show how to use worker_threads#SHARE_ENV. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: saved-model.worker.ts    From node-question-answering with Apache License 2.0 5 votes vote down vote up
constructor() {
    super(joinPaths(__filename, "../saved-model.worker-thread.js"), {
      env: SHARE_ENV
    });

    const initChannel = new MessageChannel();
    this.initPort = initChannel.port2;

    const loadChannel = new MessageChannel();
    this.loadPort = loadChannel.port2;

    const inferenceChannel = new MessageChannel();
    this.inferencePort = inferenceChannel.port2;
    this.inferencePort.setMaxListeners(1000000);

    this.once("online", () => {
      this.initPort.once("close", () => (this.loaded = true));
      const message: InitMessage = {
        type: "init",
        initPort: initChannel.port1,
        loadPort: loadChannel.port1,
        inferencePort: inferenceChannel.port1
      };

      this.postMessage(message, [
        initChannel.port1,
        loadChannel.port1,
        inferenceChannel.port1
      ]);
    });

    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    this.loadPort.on("message", (data: { model: string; error?: any }) => {
      const modelInfos = this.models.get(data.model);
      if (data.error) {
        modelInfos?.onloaderror?.(data.error);
        return;
      }

      modelInfos?.onloaded?.();
      this.models.set(data.model, { loaded: true });
      this.run(data.model);
    });
  }