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 2.0.3 to 2.0.4

3

CHANGELOG.MD

@@ -0,1 +1,4 @@

# @2.0.4 [author: Katarzyna Ziomek-Zdanowicz, date: 2019.09.09]
* modifies EmitterInstance type so it extends object with key of string type
# @2.0.3 [author: Katarzyna Ziomek-Zdanowicz, date: 2019.09.09]

@@ -2,0 +5,0 @@ * adds Window to EmitterInstance type

9

dist/_types.d.ts

@@ -5,6 +5,11 @@ import { Subscriber } from './subscriber';

export declare type EventData = [EventCallback, Subscriber[]];
export declare type EmitterInstance = Record<string | number | symbol, unknown>;
interface ObjectI {
[key: string]: unknown;
}
export declare type EmitterInstance = ObjectI | Record<number | string | symbol, unknown>;
export declare type SubscriberInstance = Record<number | string | symbol, unknown>;
export declare type SubscriptionFunctions = (eventName: string, eventCallback: EventCallback) => void;
export declare type PublisherProps = [EmitterInstance, string, string];
export declare type UnsubscribeFunction = () => void;
export declare const isValidEmitter: (emitterInstance: Record<string | number | symbol, unknown>, addListenerMethodName: string | number, removeListenerMethodName: string | number) => emitterInstance is Record<string | number | symbol, unknown>;
export declare const isValidEmitter: (emitterInstance: EmitterInstance, addListenerMethodName: string, removeListenerMethodName: string) => emitterInstance is EmitterInstance;
export {};

@@ -7,18 +7,9 @@ "use strict";

exports.isValidEmitter = void 0;
// EventCallback is generic, thus generic type of Event can be used (e.g. type React.Events )
;
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
// generic type so type React.Events can be used
var isValidEmitter = function isValidEmitter(emitterInstance, addListenerMethodName, removeListenerMethodName) {
var isOfType = function isOfType(value, type) {
return _typeof(value) === type;
};
var addMethodName = addListenerMethodName;
var removeMethodName = removeListenerMethodName;
var addKey = isOfType(addListenerMethodName, 'symbol') ? addListenerMethodName : addMethodName;
var removeKey = isOfType(removeListenerMethodName, 'symbol') ? removeListenerMethodName : removeMethodName;
return isOfType(emitterInstance[addKey], 'function') && isOfType(emitterInstance[removeKey], 'function');
return addListenerMethodName in emitterInstance && removeListenerMethodName in emitterInstance && typeof emitterInstance[addListenerMethodName] === 'function' && typeof emitterInstance[removeListenerMethodName] === 'function';
};
exports.isValidEmitter = isValidEmitter;

@@ -121,4 +121,6 @@ "use strict";

this.eventData = new Map([]);
this.addEventListener = emitterInstance[addListenerMethodName].bind(emitterInstance);
this.removeEventListener = emitterInstance[removeListenerMethodName].bind(emitterInstance);
this.addEventListener = // @ts-ignore
emitterInstance[addListenerMethodName].bind(emitterInstance);
this.removeEventListener = // @ts-ignore
emitterInstance[removeListenerMethodName].bind(emitterInstance);
}

@@ -125,0 +127,0 @@

{
"name": "publisher-subscriber-pattern",
"version": "2.0.3",
"version": "2.0.4",
"description": "Publisher subscriber pattern that can be used with different event emitters including browser window",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -8,4 +8,7 @@ import { Subscriber } from './subscriber';

export type EventData = [EventCallback, Subscriber[]];
export type EmitterInstance = Record<string | number | symbol, unknown> | Window;
interface ObjectI { [ key: string ]: unknown };
export type EmitterInstance = ObjectI | Record<number | string | symbol, unknown>;
export type SubscriberInstance = Record<number | string | symbol, unknown>;
export type SubscriptionFunctions = (eventName: string, eventCallback: EventCallback) => void;

@@ -18,29 +21,12 @@ export type PublisherProps = [ EmitterInstance, string, string ];

emitterInstance: EmitterInstance,
addListenerMethodName: string | number,
removeListenerMethodName: string | number
addListenerMethodName: string,
removeListenerMethodName: string
): emitterInstance is EmitterInstance => {
if (emitterInstance instanceof Window) {
return (
addListenerMethodName in emitterInstance &&
removeListenerMethodName in emitterInstance
);
}
const isOfType = (value: unknown, type: 'symbol' | 'function'): boolean => typeof value === type;
const addMethodName: keyof typeof emitterInstance = addListenerMethodName;
const removeMethodName: keyof typeof emitterInstance = removeListenerMethodName;
const addKey = isOfType(addListenerMethodName, 'symbol')
? addListenerMethodName
: addMethodName;
const removeKey = isOfType(removeListenerMethodName, 'symbol')
? removeListenerMethodName
: removeMethodName;
return (
isOfType(emitterInstance[ addKey ], 'function') &&
isOfType(emitterInstance[ removeKey ], 'function')
addListenerMethodName in emitterInstance &&
removeListenerMethodName in emitterInstance &&
typeof emitterInstance[ addListenerMethodName ] === 'function' &&
typeof emitterInstance[ removeListenerMethodName ] === 'function'
);
};
import {
EmitterInstance,
EventCallback,

@@ -7,2 +6,3 @@ EventData,

PublisherProps,
SubscriberInstance,
SubscriptionFunctions,

@@ -28,5 +28,7 @@ UnsubscribeFunction,

this.addEventListener =
// @ts-ignore
( emitterInstance[ addListenerMethodName ] as SubscriptionFunctions).bind(emitterInstance);
this.removeEventListener =
// @ts-ignore
( emitterInstance[ removeListenerMethodName ] as SubscriptionFunctions).bind(emitterInstance);

@@ -38,3 +40,3 @@ }

eventCallback: EventCallback,
subscriberInstance?: EmitterInstance
subscriberInstance?: SubscriberInstance
): () => void => {

@@ -41,0 +43,0 @@ const subscriber = new Subscriber(eventCallback, subscriberInstance);

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