HTML5 navigator.geolocation in Web Workers

后端 未结 4 464
温柔的废话
温柔的废话 2020-12-07 01:47

I am trying to move my code for navigator.geolocation in a web worker.

I tried it with Chrome and Safari but getting \'undefined\' on

var isGPSSupported =

4条回答
  •  离开以前
    2020-12-07 02:28

    I had similar question as yours before and asked a related question. Now I believe I have the answer to your question (and also one of my related questions).

    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. Since the two navigators are in two different scopes, there is no name conflict. But when using the APIs in JavaScript, people usually expect similar behavior on both main and worker threads if they have the same name. That's how the ambiguity happens.

提交回复
热议问题