npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@gokatz/ember-web-workers

v0.0.1

Published

Service to communicate your application with browser web workers

Downloads

1

Readme

ember-web-workers Build Status GitHub version Dependency Status

Service to communicate your application with browser web workers.

This addon can be installed with ember-cli:

  • ember install ember-web-workers

ember-web-workers

Usage

  • Create the web worker file in public (follow this blueprint):
// public/assets/web-workers/test.js

// Wait for the initial message event.
self.addEventListener('message', function(e) {
  var data = e.data;
  var port = e.ports[0];

  // Do your stuff here.
  if (port) {
    // Message sended throught a worker created with 'open' method.
    port.postMessage({ foo: 'foo' });
  } else {
    // Message sended throught a worker created with 'send' or 'on' method.
    postMessage({ bar: 'bar' });
  }
}, false);

// Ping the Ember service to say that everything is ok.
postMessage(true);
  • Import the service in your application:
// Some Ember context.
{
  worker: Ember.inject.service()
}
  • Use the methods to communicate with the web worker:

postMessage

Method used to make a request and wait for a response. This method returns a promise that will be resolved after the worker responses.

When promise resolves the worker will be terminated.

Arguments:

  • worker: the name of the worker to create (used to create the file path dist/assets/web-workers/${name}.js).
  • data: transferable object (true will be ignored, def. for ping).
// Some Ember context.
{
  foo() {
    return this.get('worker').postMessage('test', { foo: 'bar' }).then((response) => {
        // response => { bar: 'bar' }
      }, (error) => {
        // error contains the message thrown by the worker.
      });
  }
}

terminate

Using this method a pending promise can be cancelled, this will terminate the worker associated and the promise will be rejected.

If promise is not provided, it will kill all the active workers.

Arguments:

  • promise: the promise returned by the send function (optional).
// Some Ember context.
{
  foo() {
    const worker = this.get('worker');
    const promise = worker.postMessage('test', { foo: 'bar' });
    
    worker.terminate(promise);
  }
}

on/off

Methods used to subscribe to a worker events. The worker will be alive until the event is detached.

Arguments:

  • worker: the name of the worker to create (used to create the file path dist/assets/web-workers/${name}.js).
  • callback: callback to be executed each time worker sends a message (postMessage).
// Some Ember context.

function callback(data) {
  console.log(data.foo, data.bar); // 'foo bar'
}

{
  foo() {
    const worker = this.get('worker');

    return worker.on('test', callback).then(() => {
        // Worker has been created.
        // Terminate it after 5 seconds.
        setTimeout(() => {
          worker.off('test', callback);
        }, 5000);
      }, (error) => {
        // Worker error, it has been terminated.
      });
  }
}

open

This method creates a new worker and stablish a communication allowing to keep it alive to send 1..n messages until terminates.

Arguments:

  • worker: the name of the worker to create (used to create the file path dist/assets/web-workers/${name}.js).

Promise argument (object):

  • postMessage: Alias to send a message to the worker.
  • terminate: Close the connection and terminate the worker
// Some Ember context.

{
  foo() {
    const worker = this.get('worker');

    return worker.open('test').then((stream) => {
        const data1 = stream.send({ foo: 'foo' });
        const data2 = stream.send({ bar: 'bar' });
        
        // Wait responses.
        return Ember.RSVP.all([data1, data2]).then(() => {
          // Kill the worker.
          stream.terminate();

          // Do something with the worker responses.
          return data1.foo + data2.bar;
        });
      }, (error) => {
        // Worker error, it has been terminated.
      });
  }
}

Handling errors

To reject the promise an error must be thrown inside the worker:

// Worker context.

throw new Error('foo');

// Some Ember context.

{
  foo() {
    return this.get('worker').postMessage('test').catch((error) => {
        console.error(error); // Unhandled error: foo
      });
  }
}

Installation

  • git clone https://github.com/BBVAEngineering/ember-web-workers.git
  • cd ember-web-workers
  • npm install
  • bower install

Running

Running Tests

Use Chrome for testing (not sure if PhantomJS has web workers).

  • npm test (Runs ember try:each to test your addon against multiple Ember versions)
  • ember test
  • ember test --server

Building

  • ember build

For more information on using ember-cli, visit https://ember-cli.com/.