w3c / ServiceWorker

Service Workers
https://w3c.github.io/ServiceWorker/
Other
3.63k stars 312 forks source link

ServiceWorker constructor instead of navigator.serviceWorker #660

Closed lewispham closed 9 years ago

lewispham commented 9 years ago

Why doesn't Service Worker use Constructor instead of the current approach? Is this a matter of taste?

var sw = new ServiceWorker('/path/to/sw.js',{scope : '/'});
sw.onregister = function(evt){
      console.log('Service Worker is successfully registered');
};
sw.onerror = function(er){
     console.log(er.message);
};
jakearchibald commented 9 years ago

Yeah, taste and consistency. Also, a constructor shouldn't change state outside its own object. In the case of creating a new ServiceWorker registration, that's added to an underlying registry, so the effects persist even if the registration object on the page is garbage collected.

annevk commented 9 years ago

@jakearchibald that reasoning seems flawed as we have many constructors that have side effects. E.g. new Worker() does exactly that. And new SharedWorker() affects a registry.