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

@jamforce/reactive-event-bus

v1.0.0

Published

event bus with rxjs

Downloads

1

Readme

Disclaimer: This project is a fork of reactive-event-bus. It includes modifications and enhancements made by Jamforce. Please note that Jamforce is not the original author/maintainer of the project.

What is it ?

Reactive Event Bus is a typescript publish/subscribe event bus powered with RXJS. Allows to get events data from the past (subscribing after emitting !) and provides options for automatic events unsubscriptions :star:

Motivation

Imagine having a large scale application containing a lot of components interacting with each other, and we want a way to make your components communicate while maintaining loose coupling and separation of concerns principles. The Event Bus pattern can be a good solution for our problem.

Implementing an Event Bus pattern can be beneficial for our code base as it helps loose coupling your classes and promotes a publish-subscribe pattern. It also help components interact without being aware of each other. Whichever implementation we choose to follow is a matter of taste and requirements. The main idea behind it is that we can connect two objects/two classes that have different lifecycles or a very different hierarchy or items dependency in the simplest way possible. That’s all.

Installation

npm install reactive-event-bus
yarn add reactive-event-bus

:rocket: Usage

In order to be able to use the bus in our components you should import the side effects only exposing a eventBus global instance.

import 'reactive-event-bus';

eventBus.emit(...)
eventBus.on(...)

Declare some Events. Doesn't matter how you call them. The only one important thing is to keep them in one-level nested object. In which way you will declare event names and what data you will pass to them - it's completely up to you

type Events = {
  'app:start': string,
  'message:greet': string,
  'message:bye': string,
  'user:registered': { firstName: string, lastName: string, age?: number },
  'age:changed': number,
}

const bus: EventBus<Events> = eventBus;
...
bus.emit('age:changed', '10'); // it will raise an error, age should be a number
bus.emit('age:changed', 10); // OK
...
bus.on('app:start'); // OK
bus.on('app:stop'); // it will raise an error, the event does not exists
...

You can start publishing the events and subscribing to them. With all types auto-infered.

Registering events

Option 1

eventBus.on('user:registered').subscribe(() => {})

Note: on() returns an observable so you pipe any operator on top of the returned observable. on('user:registered').pipe(debounceTime(2000))subscribe(() => {})


Option 2

Automagically events unsubscription :pray: - the good thing about this option is that the developer does not need to handle the unsubscription of the event as it happens with the on().

NOTE: To use this option you must have declared on your component file the lifecycles which will be overriden by the decorator: (React - componentDidMount/componentWillUnmount, Angular - ngOnInit/ngOnDestroy, VanillaCustomElement/StencilJS - connectedCallback/disconnectedCallback).

@Subscribe('user:registered')
 onUserRegistered(data) {
  // do something
}

Additional options

If we want to just receive the first data of the subscription, there is the option: {once: true}. So after the first subscription, is automatically unsubscribed.

eventBus.on('user:registered', {once: true})).subscribe(() => {})

# or

@Subscribe('user:registered', {once: true})
  onUserRegistered(data) {
   // do something
}

If we want to subscribe and receive passed events data (emits that happened before subscribe), there is the option: { state: true }.

NOTE: With this option you cannot use patterns.

eventBus.on('user:registered', {state: true})).subscribe(() => {})

# or 

@Subscribe('user:registered', {state: true})
  onUserRegistered(config) {
   // do something
}

If we want to emit the first value and then ignore emitted values for a specified duration, there is the option: { throttleTime: durationTime }.

eventBus.on('user:registered', { throttleTime: 1000 })).subscribe(() => {})

# or 

@Subscribe('user:registered', { throttleTime: 1000 })
  onUserRegistered(data) {
   // do something
}

Dispatching events

eventBus.emit({ type: 'user:registered', data: { firstName: 'rafael', lastName: 'fecha' } })

Patterns

Patterns may contain multiple segments split by :. Use this feature to create namespaces for messages you emit. You can use * in pattern to subscribe to any matching segment, or use ** to subscribe to all segments, starting from particular position.

For example, you can use on('error:*') and subscribe to all errors, including something like error:http or error:internal and so on:


eventBus.emit('app:start',     'started');
eventBus.emit('message:greet', 'Hi!');
eventBus.emit('message:bye',   'Bye!');

eventBus.on('app:start').subscribe((data: string) => {
  console.log(data); // will receive 'started' only
});

eventBus.on('message:greet').subscribe((data: string) => {
  console.log(data); // will receive 'Hi!' only
});

eventBus.on('message:*').subscribe((data: string) => {
  console.log(data); // will receive both 'Hi!' and 'Bye!'
});

eventBus.on('**').subscribe((data: string) => {
  console.log(data); // will receive all messages: 'started', 'Hi!' and 'Bye!'
});

Tests

npm run test
yarn test

:metal: Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Acknowledgements

This project is a fork of reactive-event-bus created by rafael-fecha. The original repository can be found here.