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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@mr-samani/event-bus

v3.0.1

Published

A lightweight JavaScript event bus for any framework with auto-global support

Readme

@mr-samani/event-bus

A lightweight, dependency-free Event Bus for JavaScript and TypeScript.
Broadcast and listen to events anywhere in your application — no hard coupling, no complexity.


🚀 Installation

npm install @mr-samani/event-bus

or using Yarn:

yarn add @mr-samani/event-bus

🧠 Features

✅ Zero dependencies

✅ Ultra-lightweight (< 1 KB)

✅ Unlimited listeners per event

✅ Global usage via app.event (no imports required)

✅ Fully compatible with TypeScript, JavaScript, Angular, React, Vue, Node.js, Electron


🔧 Usage

Method 1️⃣: Global Usage (Recommended)

To enable global usage without any imports, simply import the auto initializer once at your app entry:

React / Vite / Webpack

main.tsx or index.tsx:

import '@mr-samani/event-bus/auto';

import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';

ReactDOM.createRoot(document.getElementById('root')!).render(<App />);

Angular

main.ts:

import '@mr-samani/event-bus/auto';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';

platformBrowserDynamic().bootstrapModule(AppModule);

Vue 3

main.ts:

import '@mr-samani/event-bus/auto';
import { createApp } from 'vue';
import App from './App.vue';

createApp(App).mount('#app');

Now you can use the Event Bus anywhere without importing:

app.event.on('user:login', (user) => {
  console.log('User logged in:', user);
});

app.event.trigger('user:login', { id: 1, name: 'Samani' });

Method 2️⃣: Direct Import

If you prefer explicit usage:

import eventBus from '@mr-samani/event-bus';

eventBus.on('user:login', (user) => {
  console.log('User logged in:', user);
});

eventBus.trigger('user:login', { id: 1, name: 'Samani' });

🎯 Practical Examples

⚛️ React Example (TypeScript)

main.tsx:

import '@mr-samani/event-bus/auto';

LoginButton.tsx:

export default function LoginButton() {
  const handleLogin = () => {
    app.event.trigger('user:login', { id: 1, name: 'Ali' });
  };

  return <button onClick={handleLogin}>Login</button>;
}

Header.tsx:

import { useEffect, useState } from 'react';

export default function Header() {
  const [user, setUser] = useState<any>(null);

  useEffect(() => {
    app.event.on('user:login', setUser);
    return () => app.event.off('user:login');
  }, []);

  return <div>{user ? `Welcome ${user.name}` : 'Guest'}</div>;
}

⚡ Angular Example

main.ts:

import '@mr-samani/event-bus/auto';

login.component.ts:

export class LoginComponent {
  login() {
    app.event.trigger('user:login', { id: 1, name: 'Mohammad' });
  }
}

header.component.ts:

export class HeaderComponent implements OnInit, OnDestroy {
  ngOnInit() {
    app.event.on('user:login', this.handleLogin.bind(this));
  }

  ngOnDestroy() {
    app.event.off('user:login');
  }

  handleLogin(user: any) {
    console.log('User logged in:', user);
  }
}

🖖 Vue 3 Example

<script setup lang="ts">
import { onMounted, onBeforeUnmount } from 'vue';

const handleTheme = (theme: string) => {
  console.log('Theme changed:', theme);
};

onMounted(() => {
  app.event.on('theme:change', handleTheme);
});

onBeforeUnmount(() => {
  app.event.off('theme:change');
});

const changeTheme = () => {
  app.event.trigger('theme:change', 'dark');
};
</script>

🧪 API Reference

| Method | Description | | ------------------------ | ----------------------------------------- | | on(name, callback) | Registers a listener for an event | | off(name) | Removes all listeners for an event | | trigger(name, ...args) | Triggers an event with optional arguments |


🧙 TypeScript Support

For full IntelliSense, update your tsconfig.json:

{
  "compilerOptions": {
    "types": ["@mr-samani/event-bus"]
  }
}

Or create global.d.ts:

/// <reference types="@mr-samani/event-bus" />

🤓 Use Cases

  • Global loading state management
  • Open/close modals
  • Shared form submission
  • Global notifications
  • Micro-frontend communication
  • Theme management (dark/light)
  • Data synchronization between components

🔥 Important Notes

  1. Import the auto initializer once at your root file: import '@mr-samani/event-bus/auto'
  2. Always cleanup listeners (useEffect cleanup / ngOnDestroy)
  3. Use event naming convention module:action (e.g., user:login)
  4. Add TypeScript types to avoid warnings

🐛 Fixing "app is not defined"

If you encounter app is not defined:

  1. Ensure you imported @mr-samani/event-bus/auto in the entry point
  2. Add "types" in your tsconfig
  3. Check your bundler includes the auto entry file

✍️ Author

Created with ❤️ by Mohammadreza Samani


🪪 License

ISC License — do whatever you want.


📝 Changelog

v3.0.0

  • Added /auto entry point for global usage
  • Improved TypeScript declarations
  • Updated documentation with real examples
  • Fixed app is not defined

v2.0.0

  • First release