我可以在Web Workers上使用什么? [英] What can I use with Web Workers?

查看:108
本文介绍了我可以在Web Workers上使用什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个关于网络工作者的问题


  • 工作人员是否可以访问存储?例如indexedDB / webSQL和本地存储的工作人员是从哪里启动的?

  • 我有一个 functions.js ,它具有很多与应用程序相关的快速功能,并且它真的没有意义将文件内容复制粘贴到工作人员中,只有两个不同的地方更新我的功能。
  • 我可以在工作人员中使用DOM吗?像加载临时音频标签中的音频文件来读取持续时间以及是否可播放。不能访问父页面的DOM,但在工作人员本身有一个DOM。

  • 如果对任何问题的答案是否定的,那么如何/应该如何是手动完成的吗? b
    这是一款Chrome应用程序,因此我可以访问最新的Chrome API, t需要关心向后兼容性。

    解决方案

    没有任何来自web worker的DOM访问。所有DOM操作必须从主JS线程完成。 Web Workers只能通过消息传递与主线程通信。



    这个之前的SO问题告诉你,网络工作人员无法访问本地存储,这很容易在Google搜索中找到。



    线程有一个web工作人员可以访问的列表。

    目前还不清楚将文件包含在工作人员中的含义。您可以导入脚本。我不知道你在问什么。您可以使用ajax调用来获取数据,如果这可以帮助您处理音频文件的话。



    这听起来像您需要对您可以做什么进行一些基本研究并不能与网络工作者(因为网上有关于它的TON)相关联,然后再回来一个更具体的问题,将这些知识结合在一起。



    对于网络工作者来说,没有任何窍门可以让你做更多的事情。



    你可以在主JS线程中工作并做到这一点如果你有很多工作要做,并希望保持主界面尽可能的快速响应,那么你可以使用setTimeout小块。这是一个非常古老的设计模式,可以追溯到线程可用或易于使用之前的那些日子。它涉及到设计你的工作,使得它可以在状态存储在某个持久对象中的小块中完成,然后你可以重复执行少量工作,然后返回,只在下一个计时器中选取下一项工作。


    I have a few questions about web workers

    • Does the worker have access to storage? E.g. indexedDB/webSQL and local storage of the file the worker was initiated from?

    • How can i include a file in a worker? I have a functions.js which has alot of quick app related functions and it really wont make sense to copy paste the file's contents in a worker only to have two different places up update my functions.

    • Can I have a DOM inside a worker? like load an audio file in a temp audio tag to read its duration and if it is playable or not. Not access dom of parent page, but have a DOM in the worker itself.

    • If the answers to any of the question is negative then how can/should it be done manually?

    This is for a chrome app so I have access to the latest Chrome APIs and I don't need to care about backward compatibility.

    解决方案

    There is no DOM access of any kind from a web worker - period. ALL DOM manipulation must be done from the main JS thread. Web Workers can only communicate with the main thread via messaging.

    This previous SO question tells you that web workers cannot access local storage, something that was easily found with a Google search.

    That same thread has a list of what web workers can access.

    It is unclear what you mean by "include a file in a worker". You can import scripts. I don't know what else you're asking about. You can use an ajax call to fetch data if that will help you with your audio file.

    This sounds to me like you need to do some basic research about what you can and can't do with web workers (as there is a TON written about it on the web) and then come back with a lot more specific questions that incorporate that knowledge.

    There are no hacks for web workers that let you do more than you are supposed to be able to do.

    You can do work in the main JS thread and do it in little chunks with setTimeout if you have a lot of work to do and want to keep the main UI as responsive as possible. This is a very old design pattern that goes back to the days before threading was available or easy to use. It involves designing your work such that it can be done in little pieces with state stored in some persistent object and then you can repeatedly do small amounts of work and then return back, only to pick up the next piece of work on the next timer tick.

    这篇关于我可以在Web Workers上使用什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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