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

@10ign/pubsub

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@10ign/pubsub - npm Package Compare versions

Comparing version 1.0.4 to 1.0.5

125

dist/PubSub.js

@@ -0,1 +1,16 @@

"use strict";
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _PubSub_instances, _PubSub_subscriptions, _PubSub_lastSubscriptionId, _PubSub_checkEventName, _PubSub_checkCallbackFunction, _PubSub_unsubscribeCallback;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PubSub = exports.PubSubError = void 0;
/**

@@ -5,3 +20,3 @@ * Class which extends the base Error and can be identified as a specific error

*/
export class PubSubError extends Error {
class PubSubError extends Error {
constructor(message) {

@@ -12,62 +27,22 @@ super(message);

}
exports.PubSubError = PubSubError;
/**
* Class that enables implementation of publish/subscribe design pattern.
*/
export class PubSub {
/**
* Map containing all the active event subscriptions and their associated
* callback handlers.
*/
#subscriptions;
/**
* Unique ID to control the associated subscription callback handlers.
*/
#lastSubscriptionId;
class PubSub {
constructor() {
this.#subscriptions = new Map();
this.#lastSubscriptionId = 0;
_PubSub_instances.add(this);
/**
* Map containing all the active event subscriptions and their associated
* callback handlers.
*/
_PubSub_subscriptions.set(this, void 0);
/**
* Unique ID to control the associated subscription callback handlers.
*/
_PubSub_lastSubscriptionId.set(this, void 0);
__classPrivateFieldSet(this, _PubSub_subscriptions, new Map(), "f");
__classPrivateFieldSet(this, _PubSub_lastSubscriptionId, 0, "f");
}
/**
* Checks if the provided event name is valid and throws PubSubError if it is
* invalid.
*
* @param eventName - Event name to be checked
*
* @throws {@link PubSubError}
*/
#checkEventName(eventName) {
if (eventName.length === 0) {
throw new PubSubError('Invalid event name');
}
}
/**
* Checks if the provided callback function is valid and throws PubSubError if
* it is invalid.
*
* @param callback - Function to be checked
*
* @throws {@link PubSubError}
*/
#checkCallbackFunction(callback) {
if (typeof callback !== 'function') {
throw new PubSubError('Invalid callback function');
}
}
/**
* Handles unsubscription for the provided event name and callback function.
*
* @param eventName - Event name for the registered callback
* @param callback - Function to have its subscription removed
*/
#unsubscribeCallback(eventName, callback) {
const callbacks = this.#subscriptions.get(eventName);
if (callbacks) {
callbacks.forEach((subscriptionCallback, subscriptionId) => {
if (subscriptionCallback === callback) {
callbacks.delete(subscriptionId);
}
});
}
}
/**
* Adds a subscription callback for the provided event name.

@@ -80,15 +55,16 @@ *

subscribe(eventName, callback) {
this.#checkEventName(eventName);
this.#checkCallbackFunction(callback);
const subscriptionId = `subscription_${this.#lastSubscriptionId++}`;
const callbacks = this.#subscriptions.get(eventName) || new Map();
var _a, _b;
__classPrivateFieldGet(this, _PubSub_instances, "m", _PubSub_checkEventName).call(this, eventName);
__classPrivateFieldGet(this, _PubSub_instances, "m", _PubSub_checkCallbackFunction).call(this, callback);
const subscriptionId = `subscription_${__classPrivateFieldSet(this, _PubSub_lastSubscriptionId, (_b = __classPrivateFieldGet(this, _PubSub_lastSubscriptionId, "f"), _a = _b++, _b), "f"), _a}`;
const callbacks = __classPrivateFieldGet(this, _PubSub_subscriptions, "f").get(eventName) || new Map();
callbacks.set(subscriptionId, callback);
this.#subscriptions.set(eventName, callbacks);
__classPrivateFieldGet(this, _PubSub_subscriptions, "f").set(eventName, callbacks);
}
unsubscribe(eventName, callback) {
if (!callback) {
this.#subscriptions.delete(eventName);
__classPrivateFieldGet(this, _PubSub_subscriptions, "f").delete(eventName);
}
else {
this.#unsubscribeCallback(eventName, callback);
__classPrivateFieldGet(this, _PubSub_instances, "m", _PubSub_unsubscribeCallback).call(this, eventName, callback);
}

@@ -104,4 +80,4 @@ }

publish(eventName, ...payload) {
this.#checkEventName(eventName);
const callbacks = this.#subscriptions.get(eventName) || new Map();
__classPrivateFieldGet(this, _PubSub_instances, "m", _PubSub_checkEventName).call(this, eventName);
const callbacks = __classPrivateFieldGet(this, _PubSub_subscriptions, "f").get(eventName) || new Map();
callbacks.forEach((callback) => {

@@ -117,5 +93,24 @@ callback(...payload);

unsubscribeFromAll() {
this.#subscriptions.clear();
__classPrivateFieldGet(this, _PubSub_subscriptions, "f").clear();
}
}
exports.PubSub = PubSub;
_PubSub_subscriptions = new WeakMap(), _PubSub_lastSubscriptionId = new WeakMap(), _PubSub_instances = new WeakSet(), _PubSub_checkEventName = function _PubSub_checkEventName(eventName) {
if (eventName.length === 0) {
throw new PubSubError('Invalid event name');
}
}, _PubSub_checkCallbackFunction = function _PubSub_checkCallbackFunction(callback) {
if (typeof callback !== 'function') {
throw new PubSubError('Invalid callback function');
}
}, _PubSub_unsubscribeCallback = function _PubSub_unsubscribeCallback(eventName, callback) {
const callbacks = __classPrivateFieldGet(this, _PubSub_subscriptions, "f").get(eventName);
if (callbacks) {
callbacks.forEach((subscriptionCallback, subscriptionId) => {
if (subscriptionCallback === callback) {
callbacks.delete(subscriptionId);
}
});
}
};
//# sourceMappingURL=PubSub.js.map
{
"name": "@10ign/pubsub",
"version": "1.0.4",
"description": "Common Types",
"version": "1.0.5",
"description": "Publish Subscribe utility",
"main": "dist/PubSub.js",

@@ -6,0 +6,0 @@ "source": "src/PubSub.ts",

@@ -10,3 +10,4 @@ {

"rootDir": "./src",
"target": "ESNext",
"target": "ES2016",
"module": "CommonJS",
"strict": true

@@ -13,0 +14,0 @@ },

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