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.2.0 to 1.3.0

5

CHANGELOG.MD

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

# @1.3.0 [author: Katarzyna Ziomek-Zdanowicz, date: 2019.09.07]
* converts publisher's subscribersCount to method
* renames type EventCallback to SubscribersEventCallback
* exports type SubscribersEventCallback
# @1.2.0 [author: Katarzyna Ziomek-Zdanowicz, date: 2019.09.07]

@@ -2,0 +7,0 @@ * changes publisher's unsubscribeAll and eventSubscribersCount methods to fat arrows

1

dist/index.d.ts
export { Publisher, PublisherProps } from './publisher';
export { SubscriberEventCallback } from './subscriber';

@@ -18,3 +18,11 @@ "use strict";

});
Object.defineProperty(exports, "SubscriberEventCallback", {
enumerable: true,
get: function get() {
return _subscriber.SubscriberEventCallback;
}
});
var _publisher = require("./publisher");
var _publisher = require("./publisher");
var _subscriber = require("./subscriber");

8

dist/publisher.d.ts

@@ -1,3 +0,3 @@

import { EventCallback } from './subscriber';
export declare type SubscriptionFunctions = (eventName: string, eventCallback: EventCallback) => void;
import { SubscriberEventCallback } from './subscriber';
export declare type SubscriptionFunctions = (eventName: string, eventCallback: SubscriberEventCallback) => void;
export declare type PublisherProps = [unknown, SubscriptionFunctions, SubscriptionFunctions];

@@ -9,6 +9,6 @@ export declare class Publisher {

constructor(...[emitterInstance, addEventListener, removeEventListener]: PublisherProps);
subscribe: (eventName: string, eventCallback: EventCallback, subscriberInstance?: Record<string, unknown> | undefined) => () => void;
subscribe: (eventName: string, eventCallback: SubscriberEventCallback, subscriberInstance?: Record<string, unknown> | undefined) => () => void;
unsubscribeAll: () => void;
eventSubscribersCount: (eventName: string) => number;
readonly subscribersCount: number;
subscribersCount: () => number;
private getEventData;

@@ -15,0 +15,0 @@ private getEventCallback;

@@ -61,2 +61,12 @@ "use strict";

_defineProperty(this, "subscribersCount", function () {
return Array.from(_this.eventData.values()).reduce(function () {
var count = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var eventData = arguments.length > 1 ? arguments[1] : undefined;
count += eventData[1].length;
console.log('count', count);
return count;
}, 0);
});
_defineProperty(this, "buildInformSubscribers", function (eventName) {

@@ -116,10 +126,2 @@ return function (event) {

}
}, {
key: "subscribersCount",
get: function get() {
return Array.from(this.eventData.values()).reduce(function (count, eventData) {
count += eventData[1].length;
return count;
}, 0);
}
}]);

@@ -126,0 +128,0 @@

@@ -1,5 +0,5 @@

export declare type EventCallback = (event: Event) => void;
export declare type SubscriberEventCallback = (event: Event) => void;
export declare class Subscriber {
eventCallback: EventCallback;
constructor(eventCallback: EventCallback, subscriberInstance?: Record<string, unknown>);
eventCallback: SubscriberEventCallback;
constructor(eventCallback: SubscriberEventCallback, subscriberInstance?: Record<string, unknown>);
}
{
"name": "publisher-subscriber-pattern",
"version": "1.2.0",
"version": "1.3.0",
"description": "Publisher subscriber pattern that can be used with different event emitters including browser window",

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

@@ -6,7 +6,30 @@ # About

# 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.
```javascript
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.
```javascript
const unsubscribe = publisher.subscribe('scroll', (event) => {});
// ...foo code...
unsubscribe();
```
# API
## **Publisher**
* Parameters: *emitterInstance*, *addEventListener*, *removeEventListener*
* Methods: *subscribe*, *unsubscribeAll*, *eventSubscribersCount*
* Properties: *subscribersCount*
* Parameters: *emitterInstance*, *addListener*, *removeListener*
* Methods: *subscribe*, *unsubscribeAll*, *eventSubscribersCount*, *subscribersCount*

@@ -16,5 +39,5 @@ ### Parameters

#### 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))
* Object that will be bound to *addListener* and *removeListener* as *this* ([see Function.prototype.bind](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind))
#### addEventListener, removeEventListener
#### addListener, removeListener
* Methods exposed by *emitterInstance* which *add* and *remove* event listeners, respectively

@@ -32,3 +55,3 @@

* Unsubscribes all *eventCallbacks* from all *eventNames*
* Runs *removeEventListener* of *emitterInstance* fro all *eventNames*
* Runs *removeListener* of *emitterInstance* fro all *eventNames*

@@ -39,8 +62,11 @@ #### eventSubscriberCount

### Properties
#### subscribersCount
* Returns number of *eventCallbacks* subscribed to all *evenNames*
# Stack
# Dependencies
## Production dependencies
* Package has no production dependencies
## Main development dependencies
* Typescript

@@ -47,0 +73,0 @@ * Jasmine, chai, sinon

@@ -81,6 +81,2 @@ require('chai');

expect(typeof callback1).toBe('function');
// TODO check number of subscribers
// TODO przed publikacja: zmienic poprzedniego taga, uzupelnic README o przyklad instalacji, wyeksporotwac potrzebne mi typy
// złapać testami to czego nie łapałam poprzednio
});

@@ -152,4 +148,4 @@

// then
expect(publisher.subscribersCount).toBe(4);
expect(publisher.subscribersCount()).toBe(4);
});
});

@@ -1,1 +0,2 @@

export { Publisher, PublisherProps } from './publisher';
export { Publisher, PublisherProps } from './publisher';
export { SubscriberEventCallback } from './subscriber';
import {
EventCallback,
Subscriber,
SubscriberEventCallback,
} from './subscriber';
type EventName = string;
type EventData = [EventCallback, Subscriber[]];
type EventData = [SubscriberEventCallback, Subscriber[]];
export type SubscriptionFunctions = (eventName: string, eventCallback: EventCallback) => void;
export type SubscriptionFunctions = (eventName: string, eventCallback: SubscriberEventCallback) => void;
export type PublisherProps = [ unknown, SubscriptionFunctions, SubscriptionFunctions ];

@@ -23,3 +23,3 @@

public subscribe = (eventName: EventName, eventCallback: EventCallback, subscriberInstance?: Record<string, unknown>): () => void => {
public subscribe = (eventName: EventName, eventCallback: SubscriberEventCallback, subscriberInstance?: Record<string, unknown>): () => void => {
const eventData = this.getEventData(eventName);

@@ -44,4 +44,4 @@ const subscriber = new Subscriber(eventCallback, subscriberInstance);

public get subscribersCount (): number {
return Array.from(this.eventData.values()).reduce((count, eventData) => {
public subscribersCount = (): number => {
return Array.from(this.eventData.values()).reduce((count = 0, eventData) => {
count += eventData[ 1 ].length;

@@ -59,3 +59,3 @@ return count;

private getEventCallback (eventName: EventName): EventCallback {
private getEventCallback (eventName: EventName): SubscriberEventCallback {
return this.getEventData(eventName)[ 0 ];

@@ -68,3 +68,3 @@ }

private buildInformSubscribers = (eventName: EventName): EventCallback => (
private buildInformSubscribers = (eventName: EventName): SubscriberEventCallback => (
(event: Event): void => {

@@ -71,0 +71,0 @@ this.getSubscribers(eventName)

@@ -1,8 +0,8 @@

export type EventCallback = (event: Event) => void;
export type SubscriberEventCallback = (event: Event) => void;
export class Subscriber {
public eventCallback: EventCallback;
constructor (eventCallback: EventCallback, subscriberInstance?: Record<string, unknown>) {
public eventCallback: SubscriberEventCallback;
constructor (eventCallback: SubscriberEventCallback, subscriberInstance?: Record<string, unknown>) {
this.eventCallback = eventCallback.bind(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