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

  • 2.0.4
  • latest
  • 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 various event emitters including browser's window object
  • 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, 'addEventListener','removeEventListener');

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

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

// ...foo code...

unsubscribe();

There is also a method unsubscribeAll for unsubscribing all subscribers from all emitter's events.

publisher.unsubscribeAll();

API

Publisher

  • Parameters: emitterInstance, addListenerMethodName, removeListenerMethodName
  • Methods: subscribe, unsubscribeAll, eventSubscribersCount, subscribersCount

Parameters

emitterInstance
  • Object, which exposes addListener and removeListener methods
  • Behind the scenes, the emitterInstance is bound to addListener and removeListener methods as this (see Function.prototype.bind)
addListenerMethodName, removeListenerMethodName
  • Names of methods (strings), which are exposed by emitterInstance and, which add and remove event listeners, respectively

Methods

subscribe
  • Required parameters: eventName, eventCallback

  • Optional parameter: subscriberInstance, which will be bound to the eventCallback argument as this (see Function.prototype.bind)

  • Method subscribes eventCallback to eventName, so whenever event occurs the eventCallback is called

  • Returns function for unsubscribing from the event

unsubscribeAll
  • Unsubscribes all publisher's eventCallbacks from all emitter's eventNames
eventSubscriberCount
  • Required parameter: eventName
  • Returns number of subscribers' eventCallbacks subscribed to publisher's eventName
subscribersCount
  • Returns number of subscriber's eventCallbacks subscribed to all publisher's eventNames

Dependencies

Production dependencies

  • Package has no production dependencies

Main development dependencies

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

Keywords

FAQs

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