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 - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

CHANGELOG.MD

14

dist/index.js

@@ -1,1 +0,13 @@

export { Publisher } from './publisher';
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "Publisher", {
enumerable: true,
get: function get() {
return _publisher.Publisher;
}
});
var _publisher = require("./publisher");

138

dist/publisher.js

@@ -0,6 +1,26 @@

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Publisher = void 0;
var _subscriber = require("./subscriber");
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import { Subscriber } from './subscriber';
export class Publisher {
constructor(emitterInstance, addEventListener, removeEventListener) {
var Publisher =
/*#__PURE__*/
function () {
function Publisher(emitterInstance, addEventListener, removeEventListener) {
var _this = this;
_classCallCheck(this, Publisher);
_defineProperty(this, "eventData", void 0);

@@ -12,15 +32,21 @@

_defineProperty(this, "buildInformSubscribers", eventName => event => {
this.getSubscribers(eventName).forEach(subscriber => subscriber.eventCallback(event));
_defineProperty(this, "buildInformSubscribers", function (eventName) {
return function (event) {
_this.getSubscribers(eventName).forEach(function (subscriber) {
return subscriber.eventCallback(event);
});
};
});
_defineProperty(this, "unsubscribe", (eventName, subscriber) => {
return () => {
const subscribersArray = this.getEventData(eventName)[1];
_defineProperty(this, "unsubscribe", function (eventName, subscriber) {
return function () {
var subscribersArray = _this.getEventData(eventName)[1];
if (subscribersArray) {
subscribersArray.splice(subscribersArray.findIndex(item => item === subscriber), 1);
subscribersArray.splice(subscribersArray.findIndex(function (item) {
return item === subscriber;
}), 1);
if (!subscribersArray.length) {
this.unobserveEvent(eventName);
_this.unobserveEvent(eventName);
}

@@ -33,8 +59,8 @@ }

_defineProperty(this, "observeEvent", eventName => {
this.addEventListener(eventName, this.getEventCallback(eventName));
_defineProperty(this, "observeEvent", function (eventName) {
_this.addEventListener(eventName, _this.getEventCallback(eventName));
});
_defineProperty(this, "unobserveEvent", eventName => {
this.removeEventListener(eventName, this.getEventCallback(eventName));
_defineProperty(this, "unobserveEvent", function (eventName) {
_this.removeEventListener(eventName, _this.getEventCallback(eventName));
});

@@ -47,39 +73,53 @@

subscribe(eventName, eventCallback, subscriberInstance) {
const eventData = this.getEventData(eventName);
const subscriber = new Subscriber(eventCallback, subscriberInstance);
eventData[1].push(subscriber);
this.observeEvent(eventName);
return this.unsubscribe(eventName, subscriber);
}
_createClass(Publisher, [{
key: "subscribe",
value: function subscribe(eventName, eventCallback, subscriberInstance) {
var eventData = this.getEventData(eventName);
var subscriber = new _subscriber.Subscriber(eventCallback, subscriberInstance);
eventData[1].push(subscriber);
this.observeEvent(eventName);
return this.unsubscribe(eventName, subscriber);
}
}, {
key: "unsubscribeAll",
value: function unsubscribeAll() {
var _this2 = this;
unsubscribeAll() {
Array.from(this.eventData.keys()).forEach(eventName => {
this.unobserveEvent(eventName);
});
}
Array.from(this.eventData.keys()).forEach(function (eventName) {
_this2.unobserveEvent(eventName);
});
}
}, {
key: "eventSubscribersCount",
value: function eventSubscribersCount(eventName) {
return this.getSubscribers(eventName).length;
}
}, {
key: "getEventData",
value: function getEventData(eventName) {
return this.eventData.get(eventName) || this.eventData.set(eventName, [this.buildInformSubscribers(eventName), []]).get(eventName);
}
}, {
key: "getEventCallback",
value: function getEventCallback(eventName) {
return this.getEventData(eventName)[0];
}
}, {
key: "getSubscribers",
value: function getSubscribers(eventName) {
return this.getEventData(eventName)[1];
}
}, {
key: "subscribersCount",
get: function get() {
return Array.from(this.eventData.values()).reduce(function (count, eventData) {
count += eventData[1].length;
return count;
}, 0);
}
}]);
eventSubscribersCount(eventName) {
return this.getSubscribers(eventName).length;
}
return Publisher;
}();
get subscribersCount() {
return Array.from(this.eventData.values()).reduce((count, eventData) => {
count += eventData[1].length;
return count;
}, 0);
}
getEventData(eventName) {
return this.eventData.get(eventName) || this.eventData.set(eventName, [this.buildInformSubscribers(eventName), []]).get(eventName);
}
getEventCallback(eventName) {
return this.getEventData(eventName)[0];
}
getSubscribers(eventName) {
return this.getEventData(eventName)[1];
}
}
exports.Publisher = Publisher;

@@ -0,10 +1,20 @@

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Subscriber = void 0;
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
export class Subscriber {
constructor(eventCallback, subscriberInstance) {
_defineProperty(this, "eventCallback", void 0);
var Subscriber = function Subscriber(eventCallback, subscriberInstance) {
_classCallCheck(this, Subscriber);
this.eventCallback = eventCallback.bind(subscriberInstance);
}
_defineProperty(this, "eventCallback", void 0);
}
this.eventCallback = eventCallback.bind(subscriberInstance);
};
exports.Subscriber = Subscriber;
{
"name": "publisher-subscriber-pattern",
"version": "1.0.0",
"description": "Publisher-subscriber pattern",
"version": "1.0.1",
"description": "Publisher subscriber pattern that can be used with different event emitters including browser window",
"main": "dist/index.js",

@@ -17,3 +17,11 @@ "scripts": {

"subscriber",
"javascript"
"window",
"addEventListener",
"removeEventListener",
"subscribe",
"publish",
"unsubscribe",
"javascript",
"typescript",
"unit tests"
],

@@ -20,0 +28,0 @@ "author": "Katarzyna Ziomek-Zdanowicz",

# About
* Repository contains javascript implementation of publisher-subscriber pattern
* dist/index.js returns *Publisher* class
* Javascript implementation of publisher subscriber pattern
* Can be used with different event emitters including browser window
* Provides **Publisher** class
# Publisher
* takes arguments: *emitterInstance*, *addEventListener* and *removeEventListener*
* exposes methods: *subscribe*, *unsubscribeAll* and *eventSubscribersCount*
* has property: *subscribersCount*
# API
## **Publisher**
* Takes arguments: *emitterInstance*, *addEventListener* and *removeEventListener*
* Exposes methods: *subscribe*, *unsubscribeAll* and *eventSubscribersCount*
* Has property: *subscribersCount*
## emitterInstance argument
* object that will be bound to *addEventListener* and *removeEventListener* as **this** ([see Function.prototype.bind](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind))
### Arguments
## addEventListener and removeEventListener arguments
* methods exposed by *emitterInstance* which add and remove event listeners, respectively
#### emitterInstance
* Object that will be bound to *addEventListener* and *removeEventListener* as *this* ([see Function.prototype.bind](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind))
## subscribe method
* requires two arguments: *eventName* and *eventCallback*
* accepts optional argument: *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)
#### addEventListener and removeEventListener
* Methods exposed by *emitterInstance* which add and remove event listeners, respectively
## unsubscribeAll method
* unsubscribes all *eventCallbacks* from all *eventNames*
* runs *removeEventListener* of *emitterInstance* fro all *eventNames*
### Methods
## eventSubscriberCount method
* requires argument: *eventName*
* returns number of *eventCallbacks* subscribed to the *evenName*
#### subscribe
* Requires two arguments: *eventName* and *eventCallback*
* Accepts optional argument: *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)
## subscribersCount method
* returns number of *eventCallbacks* subscribed to all *evenNames*
#### unsubscribeAll
* Unsubscribes all *eventCallbacks* from all *eventNames*
* Runs *removeEventListener* of *emitterInstance* fro all *eventNames*
#### eventSubscriberCount
* Requires argument: *eventName*
* Returns number of *eventCallbacks* subscribed to the *evenName*
### Properties
#### subscribersCount
* Returns number of *eventCallbacks* subscribed to all *evenNames*
# Stack
* typescript
* jasmine, chai, sinon
* babel
* node (EventEmitter)
# Description
* I wanted to code library which I will be able to reuse in future projects
* Typescript
* Jasmine, chai, sinon
* Babel
* Node (EventEmitter)

Sorry, the diff of this file is not supported yet

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