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

@scottburch/rxjs-msg-bus

v1.2.1

Published

A central message bus implimentation for rxjs.

Downloads

23

Readme

Rxjs-msg-bus

A central message bus implimentation for rxjs.

The central msg bus is implimented as a singleton where you can add listeners and fire events.

Defining messages

Messages are defined using a Msg type. The first argument in the type is the string by which this event will be known. The second is the data type for the data sent with the event.

type MyCustomEvent = Msg<'my-custom-event', {name: string}>

Listening for and firing events

Listeners take a message type and pass back a rxjs observable like this.

type NameEnteredEvent = Msg<'name-entered-event', string>


eventListener<NameEnteredEvent>('name-entered-event').subscribe(name => console.log(name)); // Johnny Five
fireEvent<NameEnteredEvent('name-entered-event', 'Johnny Five');

Events are async, they will be fired in the future. There are several other listener functions to fit other scenarios that are described below

Installing

yarn add @scottburch/rxjs-msg-bus

npm install @scottburch/rxjs-msg-bus

Logging events

You can log events for easier troubleshooting by listening to the central bus and console logging the messages.

getCentralMsgBus().subscribe(console.log);

API

Msg<[msg-type-string], [msg-data-type]>

Defines a message type

type UserLoggedInEvent = Msg<'user-logged-in', {username: string, rememberMe: boolean}>
sendEvent<[MsgType]>([msg-type-string], [data]);

Sends an event of MsgTypecontaining data

type UserLoggedInEvent = Msg<'user-logged-in', {username: string, rememberMe: boolean}>;

eventListener<UserLoggedInEvent('user-logged-in').pipe(
	tap(data => verifyUser(data))
).subscribe();

fireEvent<UserLoggedInEvent('user-logged-in', {username: 'scott', rememberMe: true});
eventListener<[MsgType]>([msg-type-string]);

Listens for an event

type UserLoggedInEvent = Msg<'user-logged-in', {username: string, rememberMe: boolean}>;

eventListener<UserLoggedInEvent('user-logged-in').pipe(
	tap(data => verifyUser(data))
).subscribe();

fireEvent<UserLoggedInEvent('user-logged-in', {username: 'scott', rememberMe: true});
eventListenerOnce<[MsgType]>([msg-type-string]);

Same as eventListenerexcept it only fires once and then unsubscribes.

eventListenerMatchOnce<[MsgType]>([msg-type-string], data => true/false)

Same as eventListenerOnce except it takes a matcher function. If the function returns true, the rest of the code is run and the listener is unsubscribed.

type UserReadEvent = Msg<'user-read', UserId>;

eventListenerMatchOnce<UserReadEvent>('user-read', userId => userId === '12345').subscribe(
  // only user 12345 will show up here
)

fireEvent<UserReadEvent>('user-read', '22222')  // will not trigger listener code
fireEvent<UserReadEvent>('user-read', '12345')  // will trigger listener code
sendEventPartial<[MsgType]>([msg-type-string]);

Same as sendEventexcept it is a curried function where the result is partially applied

type UserLoggedInEvent = Msg<'user-logged-in', {username: string}>
type ReadUser = Msg<'read-user', User>

eventListener<UserLoggedInEvent>('user-logged-in').subscribe(sendEventPartial<ReadUser>('read-user'));