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

hi-bus

v1.0.6

Published

Hi-Bus is a new event bus module for Typescript, it is used *decorate* to **publish** and **subscribe** event.

Downloads

4

Readme

Hi-Bus is a new event bus module for Typescript, it is used decorate to publish and subscribe event.

Lastest version: 1.0.5

Install

This module only work on Typescript now, because it use decorate which is supported only with Typescript, maybe JavaScript will support Decorate in the future.

# NPM package manager
npm install hi-bus

# Yarn package manager
yarn add hi-bus

Sample

Let’s take a sample to show the power of Hi-Bus!

// Import Hi-Bus, you can use any word instead 'HiBus'
import HiBus, {Bus, Publish, Subscribe} from 'hi-bus'

// First we must create a class with decorate ‘@Bus'
// decorate '@Bus’ is used to collect subscribe functions
// it will not work without '@Bus’ when you subscribe message in class

@Bus
class Test {  

  // Subscribe topic “handshake” with doctorate @Subscribe
  // You only focus the function logic
  @Subscribe("handshake")
  handshake() {
    console.log(“Handshake”);
  }

  // when you call function ‘pulbishHandshake’
  // it will publish topic ‘handshake’ automatic
  // the function must return something to trigger publish
  // if return nothing or return void, it will not trigger publish
  @Publish("handshake")
  publishHandshake() {
    return {};
  }

}

const test = new Test();

test.publishHandshake();
// console output: Handshake

Decorate

Please enable decorate in your tsconfig.json.

{
  ...
  "experimentalDecorators": true,
  "emitDecoratorMetadata": true,
}

Decorate list

@Bus

This is class decorate, you just put it at head of class. It was use to collect functions which was decorate by @Sucscribe. Without @Bus, the calss instance can publish topic and not subscribe topic

Sample

import {Bus} from 'hi-bus'

@Bus
class Test {
  ...
}

@Publish

@Publish(topic: string)

This decorate was used to pulbish topic, like bus.publish(topic, data), the decorate catch the result of function to be the topic data, In the decorate, @Publish create a proxied function, and the proxied function's this is always point your class prototype;

You must return any result to trigger @Publish, if return void the @Publish will not work;

there is not have to add @Bus at head of class, @Publish can work isolate with @Bus.

Sample

import { Publish } from 'hi-bus'

class Person {

  @Publish("hello")
  hello(name) {
    return name
  }

  @Publish("bye")
  bye() {
    return null;
  }

  @Publis("nothing")
  nothing(){}

}

const person = new Person();
// like <bus.publish('hello', 'world')>
person.hello('world') 

// like <bus.publish('bye', null)>
person.bye();

// Don't publish anything, because the function is result is void
person.nothing();

@Subscribe

@Subscribe(topic: string, options?:{})

The decorate was used to subscribe topic like bus.subscribe(topic, callbackFunction), you have to add @Bus at head of class, or @Subscribe will not work without @Bus.

Sample

import {Bus, Publish, Subscribe} from 'hi-bus'

@Bus
class Customer {

  @Subscribe("coffe")
  getCoffe(){
    console.log("get coffe");
  }

  @Subscribe("food")
  getFood(foodName) {
    console.log("get " + foodName);
  }

}

@Bus
class Shopper {

  @Publish('coffe')
  sendCoffe() {return null;}

  @Publish("food")
  sendFood(foodName) {return foodName}
}

const customer = new Customer();
const shopper = new Shopper();

shopper.sendCoffe();
// console.log output
// 'get coffe'

shopper.sendFood("bread")
// console.log output
// 'get bread'

@Session

@Session

Sometime we need pass data through one more functions, likeA->B->C->D. We don't return the same data one after one, like id or soket object, so you can use @Session to pass the session data through some functions.

Using @Session you must add @SessionParam decorate, which was used to set session data

Sample

import {Bus, Session, Subscribe, Publish, SessionParam} from 'hi-bus'

@Bus
class Customer {

  @Subscribe("coffe")
  @Session
  getCoffe(@SessionParam session){
    console.log("id:" + session.id);
    console.log("get coffe");
  }

  @Subscribe("food")
  @Session(@SessionParam session)
  getFood(foodName) {
    console.log("id:" + session.id)
    console.log("get " + foodName);
  }

  @Publish("reply")
  @Subscribe("anything")
  @Session
  relay() {return null;}

}

@Bus
class Shopper {

  @Publish('coffe')
  @Session
  sendCoffe(@SessionParam session) {
    session.id = 1234;
    return null;
  }

  @Publish("food")
  @Session
  sendFood(foodName, @SessionParam session) {
    session.id = 4567;
    return foodName
  }

  @Publish("anything")
  @Session
  sendAnything(@SessionParam session) {
    session.id = 123456789;
    return null;
  }

  @Subscribe("reply")
  @Session
  receiveReplay(@SessionParam session) {
    console.log("reply id:" + session.id);
  }
}

shopper.sendCoffe();
// console.log output
// id: 1234
// get coffe

shopper.sendFood("bread")
// console.log output
// id: 4567
// get bread

shopper.sendAnything()
// console.log output
// reply id: 123456789

Class Methods

Hi-Bus also use class function to publish and subscribe message.

Sample

import HiBus from 'hi-bus'

const bus = HiBus.make();

bus.subscribe("hello", () => console.log("hello world"))
bus.publish("hello");

Get the bus instance by HiBus.make() method, DONT'T new a new bus instance, it will not works with decorates

Methods

  • HiBus.make()
  • HiBus.prototype.publish()
  • HiBus.prototype.subscribe()

HiBus.make

(static) HiBus.make()

Get the bus instance object, it is a factory function.

HiBus.prototype.publish

HiBus.prototype.publish(topic, arg1, arg2, ...)

The function publish can send one topic and any arguments. Decorate only send ONE argument, because function's result is only one object. But you can send any argument with method publish

import HiBus from 'hi-bus'

const bus = HiBus.make();

bus.subscribe("hello", (a, b) => console.log("hello" + a + b))
bus.publish("hello", 'world', 'world2');

HiBus.prototype.subscribe

HiBus.prototype.subscribe(topic, (arg1, arg2, ...) => {})

This method is used to subscribe message topic, and the callback function take any number of arguments like publish function.