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

combus

v1.0.0

Published

Event based communication bus for JavaScript and TypeScript

Downloads

2

Readme

combus ☎

A JavaScript browser event based communication BUS

Installation

# with npm
npm i --save combus

# with yarn
yarn add combus

Examples

Creating a dependency injection system

// injector.js
import combus from 'combus';

const injectables = new Map();

combus.listen('getInstance', async message => {
  const { name, args } = message.payload;

  if (!injectables.has(name)) {
    return new Error(`Injectable ${name} is not registered`);
  }

  return Reflect.construct(injectables.get(name), [].concat(args));
});

combus.listen('inject', async message => {
  const { name, injectable } = message.payload;

  if (injectables.has(name)) {
    return new Error(`Injectable ${name} is already registered`);
  }

  injectables.set(name, injectable);
});

// injectables-stuff.js
import combus from 'combus';

class Foo {
  constructor(param1, param2) {
    this.param1 = param1;
    this.param2 = param2;
  }

  sum() {
    return this.param1 + this.param2;
  }
}

class Foo2 {
  constructor(arr) {
    this.arr = arr;
  }

  sum() {
    return this.arr.reduce((s, c) => s + c, 0);
  }
}

combus.dispatch('inject', {
  name: Foo.name,
  injectable: Foo,
});

combus.dispatch('inject', {
  name: Foo2.name,
  injectable: Foo2,
});

// file-which-will-retrieve-the-injectable.js
import combus from 'combus';

Promise.all([
  combus.dispatch('getInstance', {
    name: 'Foo',
    args: [5, 10]
  }),
  combus.dispatch('getInstance', {
    name: 'Foo2',
    args: [1, 2, 3, 4]
  }),
]).then(([fooMessage, foo2Message]) => {
  const foo = fooMessage.payload;
  const foo2 = foo2Message.payload;

  console.log(foo.sum()) // 15
  console.log(foo2.sum()) // 10
});

Communicating between bundles

Combus can be used to create a dialogue between different bundles (and different frameworks or libraries).

// bundle.1.js
import combus from 'combus';

const secret = 123;

combus.listen('give-me-your-secret', async message => {
  return `Hello ${message.payload}, the secret is ${secret}`;
});

// bumdle.2.js
import combus from 'combus';

combus.dispatch('give-me-your-secret', 'bundle.2').then(response => {
  console.log(response); // Hello bundle.2 , the secret is 123
});

// bumdle.3.js
import combus from 'combus';

combus.dispatch('give-me-your-secret', 'bundle.3').then(response => {
  console.log(response); // Hello bundle.3 , the secret is 123
});

Creating a state machine

Combus could be useful to create a state machine

// state-machine.js
import { listen } from 'combus';

const state = {
  todos: [],
};

listen('get', async () => state);

listen('add', async message => {
  state.todos.push(message.payload);
  return state;
});

listen('toggle', async message => {
  state.todos.forEach(todo => {
    if (todo.id !== message.payload) {
      return;
    }

    todo.completed = !todo.completed;
  });

  return state;
});

listen('remove', async message => {
  state.todos = state.todos.filter(i => i.id !== message.payload);
  return state;
});

// your-logic.js
import { dispatch } from 'combus';

function create(id, name) {
  return {
    completed: false,
    id,
    name,
  }
}

(async function main() {
  const todos = (await dispatch('get')).payload;

  await dispatch('add', create(0, 'foo'));
  await dispatch('add', create(1, 'bar'));
  await dispatch('add', create(2, 'baz'));

  await dispatch('toggle', 2);
  await dispatch('remove', 0);
  await dispatch('remove', 1);

  const remainingTodos = (await dispatch('get')).payload;

  console.log(remainingTodos); // [{ id: 2, name: 'baz', completed: true }];
})();