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

medi

v0.2.4

Published

Medi, a mediator that supports event filtering

Downloads

7

Readme

Medi; a mediator that supports event filtering and promises

npm version code coverage 100%

What is an mediator?

A mediator facilitates communication between objects without them referring to each other explicitly.

Usage

Install medi via npm or yarn

npm install medi
yarn add medi  # or use yarn

The mediator follows your standard pup/sub pattern. Listen to a channel with mediator.when('channel', handlerFn) and emit on that channel with a given payload mediator.emit('channel', payload).

When all handlers have been called a promise on mediator.emit resolves with any values that were returned in the handlers. If a promise was returned emit will wait for that promise to be resolved.

Basic example:

import medi from 'medi';

const mediator = medi();

mediator.when('somethingHappend', data => {
  // data = { foo: bar }
});

mediator.when('somethingHappend', data => {
  // Any value or promise returned will be resolved to the promise on
  // mediator.emit.then()
  return fetch('url').then(response => response.json());
});

mediator.emit('somethingHappend', { foo: 'bar' }).then(results => {
  // do something when all when handlers were called.
  // results = [ json ]
});

Filtering events

const mediator = medi();

const filter = {
  someprop: 'matchingvalue'
};

const notMatchingFilter = {
  someprop: 'notmatchingvalue'
}

const handler = data => {
  console.log(data);
};


mediator.when('somethingHappend', filter, handler);
mediator.emit('somethingHappend', filter, { foo: 'bar' });
mediator.emit('somethingHappend', notMatchingFilter, { foo: 'bar' }); // will not trigger the handler

Deleting an channel

const mediator = medi();

const notCalledHandler = data => {
  console.log(data);
};

mediator.when('somethingHappend', notCalledHandler); // handler will not be called
mediator.delete('somethingHappend');
mediator.emit('somethingHappend', { foo: 'bar' });

Deleting an specific handler on a channel

const mediator = medi();

const calledHandler = data => {
  console.log(data);
};

const notCalledHandler = data => {
  console.log(data);
};

mediator.when('somethingHappend', notCalledHandler); // will not be called
mediator.when('somethingHappend', calledHandler); // will be called
mediator.delete('somethingHappend', notCalledHandler);
mediator.emit('somethingHappend', { foo: 'bar' });

Loose coupling example.

const mediator = medi();

const user = {
  name: null,
  register: function() {
    // Code to register the user
    mediator.emit('userHasRegistered', user);
  }
};

const notificationModal = {
  show: function(message) {
    // code to show a modal.
  },
  init: function() {
    mediator.when('userHasRegistered', user => {
      notificationModal.show(`Welcome! ${user.name}`);
    });
  }
};

// App bootstrap code
notificationModal.init();

// Register a new user
user.name = 'Jane Doe';
user.register();
// this will trigger the notification modal to show because
// it's listing to the 'userHasRegistered' event that the user will emit.

The two components do not explicitly know about each others existence they only listen to the events on the mediator and act on those.

Testing

Install dependencies with yarn install and run yarn test or yarn test -- --watch for watching. To see a coverage report run yarn report (create the directories .nyc_output/ and coverage/ first).