简体   繁体   English

有没有办法在webworker中加载一个类而不仅仅是普通的javascript文件?

[英]Is there any way I can load a class in webworker instead of just plain javascript file?

I want to load a file in webworker but its not a plain javascript file. 我想在webworker中加载一个文件,但它不是一个普通的javascript文件。 It is a class. 这是一堂课。 So, instead of writing javascript file url in new Worker(filename.js); 所以,而不是在新的Worker(filename.js)中编写javascript文件url;

I am initializing the class by writing new Worker(new ClassName()); 我正在通过编写new Worker(new ClassName())来初始化类;

It is reading the file and loading it properly. 它正在读取文件并正确加载它。 It even shows the console.log message in constructor. 它甚至在构造函数中显示console.log消息。 But when the control moves to postMessage method in main file, it throws error. 但是当控件移动到主文件中的postMessage方法时,它会抛出错误。

The application I am working on has a very complex structure. 我正在处理的应用程序具有非常复杂的结构。 We have a common module that contains several classes which are executing some common functionalities throughout the application. 我们有一个通用模块,它包含几个在整个应用程序中执行一些常用功能的类。 The functionality I am working on can cause the performance issues if I execute it directly in the application. 如果我直接在应用程序中执行它,我正在处理的功能可能会导致性能问题。 So, I am loading it in a webworker. 所以,我将它加载到webworker中。 Since its a class so I have to invoke it using new keyword. 因为它是一个类所以我必须使用new关键字调用它。 So instead of: 所以代替:

var ww = new Worker(filename.js);

I am writing: 我正在写:

var ww = new Worker(new ClassName());

Now, it invokes the class, it console logs the statement written in constructor but when in the main file it executes postMessage, javascript throws an error and stops the method from execution. 现在,它调用类,它控制台记录在构造函数中编写的语句,但是当它在主文件中执行postMessage时,javascript会抛出错误并停止执行该方法。

See the code below: 请参阅以下代码:

mainFile.js mainFile.js

    var ww = new Worker(new CommonWebWorker());
        ww.postMessage('LOAD');
        ww.onmessage = function (e) {
            console.log(e.data);
    }

CommonWebWorker.js CommonWebWorker.js

'use strict';

class CommonWebWorker {
    constructor() {
        console.log("commonWebWorker invoked");
        this.onmessage = function (e) {
            this.postMessage((e.data === 'LOAD' ? 'Loading...' : 'Loading Error'));
        }
    }
}

module.exports = CommonWebWorker;

I expect that the file will load in the background through WebWorker but actually it is breaking and throwing below mentioned error 我希望该文件将通过WebWorker在后台加载,但实际上它正在破坏并抛出下面提到的错误

GET http://localhost:3000/[object%20Object] 404 (Not Found) GET http:// localhost:3000 / [object%20Object] 404(未找到)

Any kind of help is much appreciated. 任何形式的帮助都非常感谢。

The reason for the error is that Worker expects a URL string as its first parameter, not an object. 出错的原因是Worker期望将URL字符串作为其第一个参数,而不是对象。 So it converts it to a string, and when you do that on most objects you get "[object Object]" (although I would have expected "[object CommonWebWorker]" from your code, but I assume you're using a transpiler or something outputting ES5 code, which would give you "[object Object]" instead). 所以它将它转换为一个字符串,当你在大多数对象上执行此操作时,你得到"[object Object]" (虽然我希望你的代码中有"[object CommonWebWorker]" ,但我认为你正在使用一个转换器或者输出ES5代码的东西,它会给你"[object Object]"代替。

Your main page and your work are completely different JavaScript environments. 您的主页和您的工作是完全不同的JavaScript环境。 You can certainly use a class in your worker if you want, but you'd need to instantiate it in the worker, eg: 如果需要,你当然可以在你的worker中使用一个类,但是你需要在worker中实例化它,例如:

class CommonWebWorker {
 // ...
}
new CommonWebWorker(); // To instantiate it

Your worker can also import the class from another JavaScript file via importScripts (before too long it'll be able to use ES2015+ module syntax instead), so if you want to use the same class in multiple workers, you could put it in its own file and do: 您的工作人员也可以通过importScripts从另一个JavaScript文件导入class (不久之后它将能够使用ES2015 +模块语法),因此如果您想在多个工作程序中使用相同的类,您可以将它放在自己的文件和做:

importScripts("common-web-worker.js");
new CommonWebWorker(); // To instantiate it

Once ES2015+ syntax is widely available in workers¹, you'd start your worker with the relevant module flag ( new Worker("worker.js", {type: "module"}) ), and do this instead: 一旦ES2015 +语法在workers¹中广泛使用,你就可以使用相关模块标志( new Worker("worker.js", {type: "module"}) )启动你的worker,并改为:

import CommonWebWorker from "./common-web-worker.js";
new CommonWebWorker();

¹ I checked just a month ago or so, and the only browser supporting it was Chrome behind a --enable-experimental-web-platform-features flag. ¹我在一个月前左右检查过,唯一支持它的浏览器是Chrome背后的--enable-experimental-web-platform-features标志。 But this stuff changes fast... :-) 但这个东西变化很快...... :-)

暂无
暂无

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

相关问题 是否可以在WebWorker中加载JavaScript文件? - Is it possible to load JavaScript file in a WebWorker? 只有一个简单的html和js文件,我怎么能强制链接到MP3下载而不是只是播放? - with just a plain html and js file, how can i force a link to an mp3 to download instead of just play? 如何在不使用任何mysql查询,仅使用简单的javascript或jquery的情况下在HTML表中进行搜索? - How can I search in a HTML table without using any mysql queries, just a plain javascript or jquery to be specific? 我可以在网络工程师中加载AMD模块吗? - Can I load AMD modules inside a webworker? 有没有办法检查普通的香草 javascript object 而不是任何其他特殊类型的 object(即日期)? - Is there a way to check for just a plain vanilla javascript object as opposed to any other special kind of object (i.e. Date)? Javascript webworker不会通过XMLHttpRequest加载XML文件 - Javascript webworker won't load XML file via XMLHttpRequest 有什么方法可以将普通的 object 转换为 class object? - Is there any way in which we can convert plain object to class object? 有什么方法可以像常规 function 一样调用 JavaScript class 的方法吗? - Is there any way I can invoke the method of a JavaScript class as a regular function? 我可以构建一个执行任意 Javascript 代码的 WebWorker 吗? - Can I build a WebWorker that executes arbitrary Javascript code? html 文件中的 javascript 是否可以加载 json 文件数据? - Any way a javascript in a html file with file:// origin can load a json file data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM