Skip to main content
Home

A versatile and lightweight event bus implementation with a React hook for easy integration.

This package works with Node.js, Deno, Bun, BrowsersIt is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
41%
Published
a year ago (1.0.1)

event-bus

A versatile and lightweight event bus implementation with a React hook for easy integration. This package provides a simple way to manage events in your JavaScript or TypeScript applications, and it seamlessly integrates with React for managing component-level events.

Installation

Install the package using npm:

npm install @pivanov/event-bus

Usage

Basic Example

First, import the useEventBus and busDispatch functions from the package. Use the useEventBus hook to subscribe to events and busDispatch to dispatch events.

import React from 'react';
import { useEventBus, busDispatch } from '@pivanov/event-bus';

interface MyEvent {
  data: string;
}

function App() {
  // Subscribe to 'my-event'
  useEventBus<MyEvent>('my-event', (event) => {
    console.log('Event received:', event.data);
  });

  // Dispatch 'my-event' when the button is clicked
  return (
    <button onClick={() => busDispatch<MyEvent>({ type: 'my-event', data: 'Hello World!' })}>
      Dispatch Event
    </button>
  );
}

export default App;

Advanced Usage

You can use more complex filters, such as regular expressions or functions, to control which events trigger your callback.

import React from 'react';
import { useEventBus, busDispatch } from '@pivanov/event-bus';

interface MyEvent {
  data: string;
}

function App() {
  // Subscribe to events that match a pattern
  useEventBus<MyEvent>(/my-event-*/, (event) => {
    console.log('Pattern matched event:', event.data);
  });

  // Dispatch different events
  return (
    <div>
      <button onClick={() => busDispatch<MyEvent>({ type: 'my-event-1', data: 'Event 1' })}>
        Dispatch Event 1
      </button>
      <button onClick={() => busDispatch<MyEvent>({ type: 'my-event-2', data: 'Event 2' })}>
        Dispatch Event 2
      </button>
    </div>
  );
}

export default App;

API

useEventBus(type, callback, deps)

  • type: The event type to subscribe to. Can be a string, array of strings, regular expression, or function.
  • callback: The function to call when an event of the specified type is dispatched.
  • deps: Optional array of dependencies for the useEffect hook.

busDispatch(event)

  • event: The event to dispatch. Can be a string or an object with a type property.

License

MIT

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@pivanov/event-bus

Import symbol

import * as event_bus from "@pivanov/event-bus";
or

Import directly with a jsr specifier

import * as event_bus from "jsr:@pivanov/event-bus";

Add Package

pnpm i jsr:@pivanov/event-bus
or (using pnpm 10.8 or older)
pnpm dlx jsr add @pivanov/event-bus

Import symbol

import * as event_bus from "@pivanov/event-bus";

Add Package

yarn add jsr:@pivanov/event-bus
or (using Yarn 4.8 or older)
yarn dlx jsr add @pivanov/event-bus

Import symbol

import * as event_bus from "@pivanov/event-bus";

Add Package

vlt install jsr:@pivanov/event-bus

Import symbol

import * as event_bus from "@pivanov/event-bus";

Add Package

npx jsr add @pivanov/event-bus

Import symbol

import * as event_bus from "@pivanov/event-bus";

Add Package

bunx jsr add @pivanov/event-bus

Import symbol

import * as event_bus from "@pivanov/event-bus";