Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thoughts on Mozilla's email shared worker use case from n.c's perspective #27

Open
jungkees opened this issue Apr 16, 2015 · 0 comments
Labels

Comments

@jungkees
Copy link
Collaborator

I'd like to discuss on the Mozilla's use case w3c/ServiceWorker#678 (comment) from n.c's perspective. I'm not insisting on making this particular use case as an n.c's use case, but would like to think about some related points more generally.

  • The SW on sync event tries to request a separate processing job to a shared worker.
    • So basically, the SW is a client and the shared worker is a service here.
  • There's no way to use (connect to) a shared worker/worker from a service worker now.

Assuming the background job the shared worker does could ever be written as a service worker code (yes it's controversial and we may have to confine the case to those can live with waitUntil(p)), it'll be like:

// in service worker that handles background sync notifications
self.onsync = function(event) {
  if (event.registration.id === "periodicSync") {
    event.waitUntil(new Promise(resolve, reject) {
      // The service now connects to will connect to the email servers 
      // and syncs data to IDB.
      navigator.connect("https://example.com/email-sync").then(function(port) {
        // request sync to the service
        port.postMessage({
          type: 'sync'
        });
        // Listen for a message from the service
        // This port is persistent so this listener can be invoked 
        // when the SW is terminated and spun up again.
        port.onmessage = function(e) {
          var data = e.data;
          if (data.type === 'syncComplete') {
            // The service completed. 
          }
        };
        // Resolve the waitUntil() promise here. This SW can be terminated 
        // whenever from this point.
        resolve();
      });     
    });
  }
};

The service can be cross-origin service worker for sure as well as one in the same-origin as in this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant