Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

light-event-bus

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

light-event-bus

Lightweight event bus for node and the browser.

  • 0.0.0-development
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
88
decreased by-91.68%
Maintainers
1
Weekly downloads
 
Created
Source

event-bus.js

Build Status codecov License: MIT

event-bus.js is a lightweight event bus for Node.js and the browser.

Table of Contents

  1. Usage
    1. Browser
      1. Download
      2. Example
    2. Node.js
      1. Download
      2. Example
  2. API documentation

Usage

This library can be used both with Node.js and the browser.

Browser usage

Download - browser

You can download the library here. For the browser there are 2 choiches:

  1. If you are not using ES6 modules: download event-bus.min.js and import it in your app using the <script> tag.
  2. If you are using ES6 modules: run
npm install event-bus 

or

yarn add event-bus 

Or download event-bus.module.min.js and import it using ES6 imports.

These two files are also delivered through a CDN at these addresses:

  1. event-bus.min.js: addurl
  2. event-bus.module.min.js: addurl
Example - browser
  1. Not using ES6 modules
<script src='../build/event-bus.min.js'></script>

<script>
  const eventBus = new EVENT_BUS.EventBus()
            
  const subscription = eventBus.subscribe('event', arg => console.log(arg))
  eventBus.publish('event', 'message')

  subscription.unsubscribe()
</script>
  1. Using ES6 modules
<script type='module'>
  // if you are using npm/yarn
  import { EventBus } from 'event-bus'
  // if you have downloaded the file manually
  import { EventBus } from '../build/event-bus.module.min.js'

  const eventBus = new EventBus()

  const subscription = eventBus.subscribe('event', arg => console.log(arg))
  eventBus.publish('event', 'message')

  subscription.unsubscribe()
</script>

Node.js usage

Download - node

Run

npm install event-bus 

or

yarn add event-bus 
Example - node
const { EventBus } = require('event-bus')

const eventBus = new EventBus()

const subscription = eventBus.subscribe('event', arg => console.log(arg))
eventBus.publish('event', 'message')

subscription.unsubscribe()

API documentation

Table of Contents

  1. EventBus
    1. Subscribe
    2. Unsubscribe
    3. Publish
  2. EventBusSingleton

EventBus

The EventBus constructor can be accessed through the main object exposed by this library.

If you are using Node.js:

const { EventBus } = require('event-bus')

If you are using ES6 imports:

import { EventBus } from 'event-bus'

If you are using <script>:

const eventBus = new EVENT_BUS.EventBus()
Subscribe

Instances of EventBus expose a subscribe method.

Call subscribe when you want to start listening for an event.

subscribe takes two arguments: eventType and callback.

Example:

eventBus.subscribe('event', arg => console.log(arg))
  • eventType can be an object of any type. Strings are recomended.
  • callback must be a function. This function can have 0 or 1 argument.
Unsubscribe

subscribe returns an object that exposes an unsubscribe method.

Call unsubscribe to cancel the current subscription.

Example:

const subscription = eventBus.subscribe('event', arg => console.log(arg))

// To cancel the subscription
subscription.unsubscribe()
Publish

Instances of EventBus expose a publish method.

Call publish when you want to publish an event on the event bus.

publish takes two arguments: eventType and argument.

Example:

eventBus.publish('event', 'message')
  • eventType can be an object of any type. Strings are recomended.
  • argument is optional. It will be passed to all the listeners for the event. It can be of any type.

In the case of this examples, only subscribers of the event 'event' will be called, with the string 'mesage' as argument.

EventBusSingleton

Often a single instance of EventBus is needed across an entire application, a singleton. The main object of this library already exposes a singleton, so that you don't have to create your own.

You can access the EventBusSingleton in the following ways:

If you are using Node.js:

const { EventBusSingleton } = require('event-bus')

If you are using ES6 imports:

import { EventBusSingleton } from 'event-bus'

If you are using <script>:

const eventBus = new EVENT_BUS.EventBusSingleton()

This singleton is just an instance of EventBus, so it can be used as any other user-defined EventBus.

Keywords

FAQs

Package last updated on 18 Jan 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc