Re: Geofencing alternative proposal

On Fri, Sep 19, 2014 at 8:31 AM, Martin Thomson <martin.thomson@gmail.com>
wrote:

> On 18 September 2014 10:27, Marijn Kruisselbrink <mek@google.com> wrote:
> > Can you give some sample code how this would look like with your API? I'm
> > not quite following how it could. So the browser would "wake up"
> > (essentially load from scratch) a service worker, and would then have to
> > somehow wait for the service worker code to install event handlers on the
> > right geofences? But to get to a GeolocationFence object it would need
> to do
> > possibly asynchronous API calls, so it's not clear to me how this waiting
> > could work.
>
> A service worker runs the code in the "install" event when it is
> awoken, during which time you would have to register handlers for
> global events.  The only difference with having specific registrations
> is that you would have to force the install event to wait for the
> promise:
>
> this.addEventListener("install", e => {
>   e.waitUntil(
>     this.geolocation.geofence.getRegistrations().then(fences => {
>       fences.forEach(fence => { fence.onenter = handleEnter; });
>     })
>   );
> });
>
> This (e.waitUntil) is a standard feature of service workers.
>
> https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#wait-until-method
>
Then maybe I'm misunderstanding how service workers work, but how I
understand it the "install" event is ran once, essentially the first time a
particular service worker version is loaded. Once a service worker is
installed and activated a browser is free to completely throw away all
javascript state associated with the service worker, and later reload/rerun
it from scratch when it has some event to deliver to the service worker (by
doing it this way a service worker that isn't currently processing any
events uses no resources at all). This means that even if somehow the
browser would keep track of which geofences have event handlers associated
to them by a particular service worker, this doesn't help since the event
handler itself would no longer exist when the service worker file gets
loaded again. Service workers and all the javascript objects/closures/...
generally only exist for the duration of handling an event.

Received on Friday, 19 September 2014 16:23:30 UTC