angular2 使用自定义可序列化对象在后台线程中工作 [英] angular2 work in background thread with custom serializable objects

查看:32
本文介绍了angular2 使用自定义可序列化对象在后台线程中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能做到这一点:我有一些可能很长的计算(例如,巨大的 JSON 解析为 http 响应)并希望以非阻塞方式进行.

How could I achieve this: I have some potentially long computation (eg. huge JSON to parse as http resp.) and want to do it in a non-blocking way.

我尝试采用 multithread.js 库来使用 web worker 做后台工作.这个库需要 JSON 可序列化对象传递给不知道闭包、DOM 或任何其他全局变量的执行函数.例如.MT.process(longRunningJob, doneCallback)(jsonSerializableArgForBGJob).但是这个库已经很老了(3 年前最后一次提交).有没有更适合angular2的更好的选择?我需要针对最广泛的浏览器,包括较旧的浏览器(IE/Edge 除外),因此使用它的 Service Worker 或 lib 可能不是一种选择.

I tried to adopt multithread.js lib to do the background work using web worker. This lib requires JSON serializable objects to pass to the execution function which is not aware of closures, DOM or any other globals. eg. MT.process(longRunningJob, doneCallback)(jsonSerializableArgForBGJob). But this lib is rather old (3 years ago last commit). Are there any better alternatives more suitable for angular2? I need to target widest range of browsers including older ones (except IE/Edge) so service worker or lib using it probably is not an option.

对于序列化,我发现这个 cerialize 库通过装饰自定义对象的道具来序列化自定义对象.这实际上可以工作,但它添加了相当多的代码并且似乎容易出错.我的另一个担忧是我使用继承和多态性,并且不确定该库是否已准备好用于此类 UC.人们会期望 Java 已知的机制:可序列化接口和重写的序列化/反序列化方法.有没有办法在 typescript/angular2 中实现这一点?

For serialization I found this cerialize lib to serialize custom objects by decorating their props. That could actually work but it adds quite a lot of code and seems error prone. My other concern is that I use inheritance and polymorphism and not sure if the lib is ready for such UC. One would expect mechanism known from Java: Serializable interface and overriden serialize/deserialize methods. Is there a way to accomplish this in typescript/angular2?

推荐答案

JavaScript 是单线程和异步的,只要您的代码是异步的.这意味着,如果您的代码正在阻塞(长时间运行循环),那么您正在阻塞其他要执行的代码.

JavaScript is single threaded and asynchronous as long as your code is asynchronous. This means that if your code is blocking (long running loop) then you're blocking other code to be executed.

RxJS 不使用 WebWorker,所以你所做的一切都是阻塞执行线程.

RxJS doesn't use WebWorkers so everything you do is blocking the execution thread.

我认为,如果您想以非阻塞方式处理数据,目前除了自己使用 WebWorkers 实现之外别无他法.

I think if you want to process data in a non blocking way there's currently no other way than implementing it by yourself with WebWorkers.

另外,请注意异步并不意味着并行.

Also, note that asynchronous doesn't mean parallel.

这篇关于angular2 使用自定义可序列化对象在后台线程中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆