简体   繁体   English

在Angular中的Angular和Typescript中使用Web Worker(多线程)是否可行?

[英]Is it feasible to use web worker (multi-threading) in Angular and Typescript in NativeScript?

I'm currently develop an App that is based on NativeScript and Angular2. 我目前正在开发基于NativeScript和Angular2的应用程序。

My screen freeze for while when my App fetching data through HTTP, and I'd like to put the fetching action into another thread. 当我的应用程序通过HTTP提取数据时,我的屏幕冻结了一段时间,我想将提取操作放入另一个线程中。

I did a lot of search on the web, and all I got is the code in javascript like the official doc - https://docs.nativescript.org/angular/core-concepts/multithreading-model.html 我在网上做了很多搜索,而我得到的只是像官方文档一样的javascript代码-https: //docs.nativescript.org/angular/core-concepts/multithreading-model.html

Is there any way to implement the muli-threading with WebWorker in "Typescript"(which contain the support of Angular injected HTTP service) instead of the "Javascript" code(the code from the official doc) 有什么方法可以用“ Typescript”(包含对Angular注入的HTTP服务的支持)中的WebWorker来实现多线程,而不是“ Javascript”代码(来自官方文档的代码)

It's appreciated if someone could give me some guide or hint, and it'll be great if I could got some relative example code. 如果有人可以给我一些指导或提示,我们将不胜感激,如果我能得到一些相关的示例代码,那将是非常不错的。 Thanks. 谢谢。

There shouldn't be any big draw back for using WebWorkers in {N} + Angular but be aware that currently the WebWorker is not "exactly" compatible with Angular AoT compilation. 在{N} + Angular中使用WebWorker不应有任何弊端,但请注意,当前WebWorker与Angular AoT编译“不完全”兼容

For me when creating an WebwWrker ( var myWorker = new Worker('~/web.worker.js'); ) throws and error after bundling the application with AoT. 对我来说,创建WebwWrker( var myWorker = new Worker('~/web.worker.js'); )时,将应用程序与AoT捆绑在一起会引发错误。 I have seen soem talk about this in the community and possible the way to fix this is by editing the webpack.common.js and adding an "loaded" like so: 我在社区中看到过soem关于此问题的讨论,解决此问题的可能方法是通过编辑webpack.common.js并添加“ loaded”,如下所示:

{
    test: /\.worker.js$/,
    loaders: [
    "worker-loader"
    ]
}

Disclaimer: I have not tried this approach for fixing the error. 免责声明:我尚未尝试使用此方法来修复错误。

If someone have some problems adding workers in Nativescript with Angular and Webpack, you must follow the steps listed here . 如果有人在使用Angular和Webpack在Nativescript中添加工作程序时遇到问题,则必须遵循此处列出的步骤。

Keep special caution in the next steps: 在接下来的步骤中要特别小心:

  • When you import the worker, the route to the worker file comes after nativescript-worker-loader! 导入工作程序时,到工作程序文件的路由在nativescript-worker-loader! .

  • In the webpack.config.js keep caution adding this piece of code: 在webpack.config.js中,请谨慎添加以下代码:

      { test: /.ts$/, exclude: /.worker.ts$/, use: [ "nativescript-dev-webpack/moduleid-compat-loader", "@ngtools/webpack", ] }, 

because is probable that you already have configured the AoT compilation, like this: 因为可能您已经配置了AoT编译,如下所示:

{
    test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/,
    use: [
        "nativescript-dev-webpack/moduleid-compat-loader",
        "@ngtools/webpack",
    ]
},

and you only need to add the exclude: /.worker.ts$/, 并且您只需要添加exclude: /.worker.ts$/,

  • Finally, there is an example of a worker, in this case it use an Android native library: 最后,有一个工作程序示例,在这种情况下,它使用Android本机库:

    • example.worker.ts: example.worker.ts:

        import "globals"; const context: Worker = self as any; declare const HPRTAndroidSDK; context.onmessage = msg => { let request = msg.data; let port = request.port; let result = HPRTAndroidSDK.HPRTPrinterHelper.PortOpen("Bluetooth," + port.portName); context.postMessage(result); }; 
    • example.component.ts (../../workers/example.worker is the relative route to my worker): example.component.ts(../../ workers / example.worker是到达我的工作人员的相对路径):

       import * as PrinterBTWorker from "nativescript-worker-loader!../../workers/example.worker"; import ... connect(printer: HPRTPrinter): Observable<boolean> { if (this.isConnected()){ this.disconnect(); //Disconnect first if it's already connected } return Observable.create((observer) => { const worker = new PrinterBTWorker(); worker.postMessage({ port: printer }); worker.onmessage = (msg: any) => { worker.terminate(); if (msg.data == 0) { // 0: Connected, -1: Disconnected observer.next(true); } else { observer.next(false); } }; worker.onerror = (err) => { worker.terminate(); observer.next(false); } }).pipe(timeout(5000), catchError(err => of(false))); } 

Note: I use an Observable to make my call to the worker async and to add a timeout to the call to the native code, because in the case that it is not possible to connect to the printer (ex. it's turned off), it takes almost 10 seconds to notify, and this caused in my case the frezing of the app for all that time. 注意:我使用Observable来使对worker的调用异步,并为对本机代码的调用添加超时,因为在无法连接到打印机的情况下(例如,它已关闭),它会通知需要大约10秒的时间,在我看来,这一直困扰着应用程序。

Important: It seem that it's necessary to run again the code manually every time that a change is made, because the worker isn't compiled using AoT. 重要提示:似乎有必要在每次更改时再次手动运行代码,因为工作器不是使用AoT编译的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM