Skip to content

Instantly share code, notes, and snippets.

@jyasskin
Last active May 6, 2016 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jyasskin/b44ae6d1cf6c209e063447babee8a764 to your computer and use it in GitHub Desktop.
Save jyasskin/b44ae6d1cf6c209e063447babee8a764 to your computer and use it in GitHub Desktop.
Patterns in directing events to particular globals

Patterns in directing events to particular globals

Push

The push event is only dispatched to a ServiceWorkerGlobalScope. Events are registered by calling serviceWorkerRegistration.pushManager.subscribe(...).

Background sync

The sync event is only dispatched to a ServiceWorkerGlobalScope. Events are registered by calling serviceWorkerRegistration.sync.register(...).

Notifications

Persistent notifications dispatch events to a ServiceWorkerGlobalScope, while non-persistent notifications dispatch them to a Notification in a Window's realm. new Notification() creates a non-persistent notification, while serviceWorkerRegistration.showNotification() creates a persistent one.

So, different spellings for the different event targets, and no way to direct an event to a Worker or SharedWorker.

https://notifications.spec.whatwg.org/#create-a-notification

Worklets

Most Worklets don't handle events registered from Windows.

  • paintWorklet is called when CSS uses the paint(name) function.
  • renderWorklet is called when CSS uses the *layout(name) functions.

AudioWorklet

The AudioWorklet proposal includes a AudioWorkletNode(..., "name", ...).postMessage() function that causes a call to the onmessage() member of an AudioWorkletProcessor subclass registered for "name", running in an AudioWorkletGlobalScope. This matches the pattern of calling a method on a main-window representation of a worker to cause an event to happen on an instance of that worker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment