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

publisher-subscriber-pattern

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

publisher-subscriber-pattern

Publisher subscriber pattern that can be used with different event emitters including browser window

  • 1.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-72.73%
Maintainers
1
Weekly downloads
 
Created
Source

About

  • Javascript implementation of publisher subscriber pattern
  • Can be used with different event emitters including browser window
  • Provides Publisher class

Installation

npm i publisher-subscriber-pattern

Example

The example uses the browser's window object as emitterInstance. Other valid emitter instances can be also used.

import { Publisher } from 'publisher-subscriber-pattern';

const publisher = new Publisher(window, window.addEventListener, window.removeEventListener);

Publisher subscribe method returns function, which can be used later to unsubscribe from the event.

const unsubscribe = publisher.subscribe('scroll', (event) => {});

// ...foo code...

unsubscribe();

API

Publisher

  • Parameters: emitterInstance, addListener, removeListener
  • Methods: subscribe, unsubscribeAll, eventSubscribersCount, subscribersCount

Parameters

emitterInstance
  • Object that will be bound to addListener and removeListener as this (see Function.prototype.bind)
addListener, removeListener
  • Methods exposed by emitterInstance which add and remove event listeners, respectively

Methods

subscribe
  • Required parameters: eventName, eventCallback
  • Optional parameter: subscriberInstance that will be bound to eventCallback as this
  • Subscribes eventCallback to eventName, so whenever event occurs the eventCallback is called (with subscriberInstance as this, if provided)
  • Returns unsubscribe function
unsubscribeAll
  • Unsubscribes all eventCallbacks from all eventNames
  • Runs removeListener of emitterInstance fro all eventNames
eventSubscriberCount
  • Required parameter: eventName
  • Returns number of eventCallbacks subscribed to the evenName
subscribersCount
  • Returns number of eventCallbacks subscribed to all evenNames

Dependencies

Production dependencies

  • Package has no production dependencies

Main development dependencies

  • Typescript
  • Jasmine, chai, sinon
  • Babel
  • Node (EventEmitter)

Keywords

FAQs

Package last updated on 07 Sep 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