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

reactive-event-bus

v1.1.1

Published

event bus with rxjs

Downloads

15

Readme

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

:computer: CodeSandbox Example

https://codesandbox.io/s/nervous-moon-79gh2?file=/src/App.tsx

:rocket: Usage

In order to be able to use the methods in our components you should import them from 'reactive-event-bus';

import { on, emit, Subscribe } from 'reactive-event-bus';

Registering events

Option 1

on('GetSomethingMessage').subscribe(() => {})

Note: on() returns an observable so you pipe any operator on top of the returned observable. on('GetSomethingMessage').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('GetSomethingMessage')
 onGetSomething(config) {
  // 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.

on('GetSomethingMessage', {once: true})).subscribe(() => {})

# or

@Subscribe('GetSomethingMessage', {once: true})
  onGetSomething(config) {
   // do something
}

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

on('GetSomethingMessage', {state: true})).subscribe(() => {})

# or 

@Subscribe('GetSomethingMessage', {state: true})
  onGetSomething(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 }.

on('GetSomethingMessage', { throttleTime: 1000 })).subscribe(() => {})

# or 

@Subscribe('GetSomethingMessage', { throttleTime: 1000 })
  onGetSomething(config) {
   // do something
}

Dispatching events

emit({ type: 'GetSomethingMessage', data: { something: 'someValue'} })

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.