Sensor reading in web worker

后端 未结 1 1985
时光取名叫无心
时光取名叫无心 2021-01-14 09:51

It seems that we can not get sensor data in the web workers. I wonder the reason behind it. The use case is that I am thinking about getting geolocation data in the worker t

相关标签:
1条回答
  • 2021-01-14 10:30

    OK. After reading some Chromium code, I have the answer to my own question 2 now. I still have no answer to the other 3 questions...

    Answer to question 2: Does navigator.geolocation belong to navigator?

    navigator.geolocation belongs to navigator in the main thread only, but doesn't belong to navigator in the worker thread.

    The main reason is that even though the navigator in worker thread looks exactly the same as the one in main thread, those two navigators have independent implementations on the C++ side. That is why navigator.geolocation is not supported in the worker thread.

    The related code is in Navigator.idl and WorkerNavigator.idl in Chromium code. You can see that they are two independent interfaces in the .idl files. And they have independent implementations on the C++ side of the binding. Navigator is an attribute of DOMWindow, while WorkerNavigator is an attribute of WorkerGlobalScope.

    However, on the JavaScript side, they have the same name: navigator. Well, I understand that the two navigators are in two different scopes, so there is no name conflict. But when I use the APIs in JavaScript, I expect similar behavior on both main and worker threads if they have the same name. That's how the ambiguity happens.

    0 讨论(0)
提交回复
热议问题