简体   繁体   English

我怎样才能允许导入我的网络工作者?

[英]How can I allow imports into my webworker?

I have some basic code here.我这里有一些基本代码。

https://codesandbox.io/s/competent-nightingale-80r4ps?file=/src/App.js:24-332 https://codesandbox.io/s/competent-nightingale-80r4ps?file=/src/App.js:24-332

App.js : I create a webworker, and I have a function to post a message to that worker. App.js :我创建了一个 webworker,我有一个 function 可以向该 worker 发送消息。 When I receive a message back, I run console.log(e.data);当我收到回复消息时,我运行console.log(e.data); . .

export default function App() {
  const worker = new Worker("Worker.js");

  worker.onmessage = (e) => {
    console.log(e.data);
  };

  function getCount() {
    worker.postMessage("get count");
  }
  return (
    <div className="App">
      <button onClick={getCount}>Get count!</button>
    </div>
  );
}

Worker.js : Inside the worker, I create a counter object and post a message with its value. Worker.js :在工作人员内部,我创建了一个counter object 并发布了一条消息及其值。

class counter {
  constructor() {
    this.value = Math.floor(Math.random() * 100);
  }
}

onmessage = (e) => {
  const myCounter = new counter();
  this.postMessage(myCounter.value);
};

I would like to import the below helper functions and classes into my webworker, instead of embedding them in the file directly.我想将下面的辅助函数和类导入到我的 webworker 中,而不是直接将它们嵌入到文件中。 Is there a way to do this if my worker is located in public ?如果我的工作人员位于public ,有没有办法做到这一点?

My file structure is like this:我的文件结构是这样的:

root
-src
--utility1
--class1
-public
--webworker

Workers can have type classic or module .工人可以有类型classicmodule Workers with type module support ES6 modules.类型为module的 worker 支持 ES6 模块。

Example: index.html示例: index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Module Worker Example</title>
  </head>
  <body>
    <script src="index.js"></script>
  </body>
</html>

index.js

const worker = new Worker('Worker.js', { type: 'module' });

worker.onmessage = (e) => {
  console.log(e.data);
};

function getCount() {
  worker.postMessage('get count');
}

getCount();

Worker.js

import { Counter } from './Counter.js';

onmessage = (e) => {
  const myCounter = new Counter();
  postMessage(myCounter.value);
};

Counter.js

export class Counter {
  constructor() {
    this.value = Math.floor(Math.random() * 100);
  }
}

Currently, it's not supported by all major browsers.目前,并非所有主流浏览器都支持它。

An alternative option is a worker with type classic and importScripts :另一种选择是使用类型为classicimportScripts的 worker:

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Module Worker Example</title>
  </head>
  <body>
    <script src="index.js"></script>
  </body>
</html>

index.js

const worker = new Worker('Worker.js');

worker.onmessage = (e) => {
  console.log(e.data);
};

function getCount() {
  worker.postMessage('get count');
}

getCount();

Worker.js

importScripts('./Counter.js');

onmessage = (e) => {
  const myCounter = new Counter();
  postMessage(myCounter.value);
};

Counter.js

class Counter {
  constructor() {
    this.value = Math.floor(Math.random() * 100);
  }
}

It works on all major browsers.它适用于所有主要浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我该如何将数据从我的网络工作人员提供给highchart - How can I give data from my webworker to highchart 如何允许WebWorker调用HTTPS URL - How to allow WebWorker to call HTTPS URL 如何将动态生成的代码导入 webworker 以便在调试器中访问 - How can I import dynamically generated code into webworker to be accessible in debugger 如何为我自己的文件使用 React 样式导入? - How can I use React style imports for my own files? ESLint:如何使用“无限制导入”限制某些路径但允许子路径? - ESLint: How can I restrict certain paths with "no-restricted-imports" but allow subpaths? 我可以在网络工程师中加载AMD模块吗? - Can I load AMD modules inside a webworker? 如何在 VSCode 中将命名导入转换为默认导入? - How can I convert named imports to default imports in VSCode? 如何在 NextJS 10 中初始化 webworker? - How do I initialize a webworker in NextJS 10? 如何在不同的 React 库中实现导入以及如何在我自己的库中实现相同的模式? - How are the imports implemented in different React libraries and how can I implement the same pattern in my own library? 如何将动态导入与流星链接在一起? - How can I chain dynamic imports with meteor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM