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

ember-message-bus

v0.3.6

Published

A simple message bus for Ember.js.

Downloads

2,340

Readme

npm version Build Status

ember-message-bus

Explore new patterns of event-driven communication with ember-message-bus. This addon adds a simple service to your application, message-bus, which you can use to facilitate loosely coupled communications between services, components, and other Ember objects.

Installation

ember install ember-message-bus

Usage

First, inject the message-bus service into your object:

import Ember from 'ember';

export default Ember.Component.extend({
  messageBus: Ember.inject.service('message-bus')
});

Once the message-bus has been injected, you can subscribe to events through it:

import Ember from 'ember';

export default Ember.Component.extend({
  messageBus: Ember.inject.service('message-bus'),

  init() {
    this._super(...arguments);

    this.get('messageBus').subscribe('my-event', this, this.doSomething);
  },

  doSomething(arg1, arg2) {
    console.log(arg1 + arg2);
  }
});

subscribe expects three arguments: 1) the name of the event, 2) the context, typically this, and 3) the callback.

Finally, you can publish events to trigger the subscription:

import Ember from 'ember';

export default Ember.Component.extend({
  messageBus: Ember.inject.service('message-bus'),

  action: {
    click() {
      this.get('messageBus').publish('my-event', 1, 2);
    }
  }
});

publish expects one or more arguments. The first argument must be the name of the event. After that, you can pass as many arguments as you like into publish. These arguments will be handed to the subscribing callback.

Testing

It's easy to test if a message is published. First, run initializeQUnitAssertions:

import { initializeQUnitAssertions } from 'ember-message-bus';

moduleForComponent('my-component', 'Integration | Component | my component', {
  integration: true,

  beforeEach() {
    const appInstance = getOwner(this);

    initializeQUnitAssertions(appInstance);
  }
});

If you'd like to ensure that events are published, use the Qunit assertion willPublish like so:

assert.willPublish('shouldBeTriggered', '`shouldBeTriggered` was triggered');
assert.willPublish('shouldReceiveArgs', ['foo', arg2], '`shouldReceiveArgs` received the correct args');
assert.willPublish('shouldCallback', (param1, param2) => return param2 === arg2, '`shouldCallback` tested with callback');

Note that if you want to test that args are published, the expected args should be passed in as an array. Alternatively, you can provide a callback that will receive the args as their params.

On the other end, if you want to confirm that a message was not published, you can use willNotPublish:

assert.willNotPublish('thisMessageShouldNotBePublished', '`thisMessageShouldNotBePublished` was not published');

Note that in both cases, these assertions should be made before the message is actually published. So in a component integration test:

const foo = { bar: 'baz' };

this.set('foo', foo);

assert.willPublish('shouldBeTriggered', [foo], '`shouldBeTriggered` was triggered');

this.render(hbs`{{my-component foo=foo}}`);