AngularJS和网络工作者(AngularJS and web workers)

系统教程 行业动态 更新时间:2024-06-14 16:57:39
AngularJS和网络工作者(AngularJS and web workers)

angularJS如何使用web worker在后台运行进程? 这样做有什么模式吗?

目前,我正在使用一个在单独的网络工作者中具有该模型的服务。 此服务实现以下方法:

ClientsFacade.calculateDebt(client1); //Just an example..

在实现中,该方法向工作者发送一条消息给数据。 这样我就可以抽象出一个在单独的线程中执行的事实,我也可以提供一个针对服务器进行查询的实现,甚至可以在同一个线程中执行此操作。

由于我是新的javascript,我只是从其他平台回收知识,我不知道这是否会是你会做的,或者也许是角色,这是我正在使用的,提供了一种这样做的方式。 此外,这引入了我的架构的变化,因为工作人员必须明确地将更改推送到控制器,然后更新它的值,然后这反映在视图中,我是否在工程上? 有点令人沮丧的网络工作者“保护”我这么多,不能让我分享内存等。

How can angularJS use web workers to run processes in the background? Is there any pattern I should follow on doing this?

Currently, I am using a service that has the model in a separate web worker. This service implements methods like:

ClientsFacade.calculateDebt(client1); //Just an example..

In the implementation, this method sends a message to the worker with the data. This allows me to abstract the fact that it is being performed in a separate thread and I could also provide an implementation that queries against a server or even one that does this action in the same thread.

Since I'm new to javascript and I'm just recycling knowledge I have from other platforms I wonder if this is something you would do or perhaps Angular which is what I am using, offers a sort of way of doing this. Also this introduces a change in my architecture since the worker must explicitly push changes to the controller, which then updates its values and then this is reflected in the view, am I over engineering this? It's a bit frustrating that web workers "protect" me so much from screwing up by not allowing me to share memory etc.

最满意答案

与Web员工的沟通是通过邮件传送机制进行的。 拦截这些消息发生在回叫中。 在AngularJS中,放置网络工作者的最佳位置正在您正式注明的服务中。 处理这个问题的最好办法是使用Angular作品令人惊奇的承诺。

以下是service中的webworker示例

var app = angular.module("myApp",[]); app.factory("HelloWorldService",['$q',function($q){ var worker = new Worker('doWork.js'); var defer = $q.defer(); worker.addEventListener('message', function(e) { console.log('Worker said: ', e.data); defer.resolve(e.data); }, false); return { doWork : function(myData){ defer = $q.defer(); worker.postMessage(myData); // Send data to our worker. return defer.promise; } }; });

现在任何访问Hello World服务的外部实体无需关心HelloWorldService的实现细节 - HelloWorldService可能可以通过http或通过http处理数据,或者在那里进行处理。

希望这是有道理的。

Communication with Web workers happens through a messaging mechanism. Intercepting these messages happens in a call back. In AngularJS, the best location to put a web worker is in a service as you duly noted. The best way to deal with this is to use promises, which Angular works amazingly with.

Here is an example of a webworker in a service

var app = angular.module("myApp",[]); app.factory("HelloWorldService",['$q',function($q){ var worker = new Worker('doWork.js'); var defer = $q.defer(); worker.addEventListener('message', function(e) { console.log('Worker said: ', e.data); defer.resolve(e.data); }, false); return { doWork : function(myData){ defer = $q.defer(); worker.postMessage(myData); // Send data to our worker. return defer.promise; } }; });

Now whatever external entity that accesses Hello World service need not care about the implementation details of HelloWorldService - HelloWorldService could probably process the data over a web worker, over http or do the processing right there.

Hope this makes sense.

更多推荐

worker,web,电脑培训,计算机培训,IT培训"/> <meta name="description"

本文发布于:2023-04-13 12:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/044444185677faa77dcb816d35f9a462.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作者   网络   AngularJS   web   workers

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!