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

event-publisher

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

event-publisher

A strongly typed protected event creator/publisher/signaler for use with TypeScript and JavaScript.

  • 1.1.0-beta
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

EventPublisher

GitHub license npm version

A strongly typed protected event creator/publisher/signaler for use with TypeScript and JavaScript.

Purpose

  • Provides an API that separates event listening/subscribing from dispatching/publishing.
  • Simplifies adding events to any object.

Example

import { Event, EventPublisher } from '../src/EventPublisher';

export class MyObservable<T> {

	readonly start:Event<void>;
	readonly update:Event<T>;
	readonly complete:Event<void>;

	private readonly _dispatcher:{
		start:EventPublisher<void>,
		update:EventPublisher<T>,
		complete:EventPublisher<void>
	};

	constructor()
	{
		const start = new EventPublisher<void>(1);
		const update = new EventPublisher<T>();
		const complete = new EventPublisher<void>(1);
		this._dispatcher = { start, update, complete };
		this.start = start.event;
		this.update = update.event;
		this.complete = complete.event;
	}
}

API

Event

type Listener<T> = (value: T) => void;
type Unsubscribe = () => void;

interface Event<T>
{
	/**
	 * Adds a listener and return an unsubscribe function.
	 * @param listener
	 */
	(listener: Listener<T>): Unsubscribe;

	/**
	* Add an entry to the end of the registry.
	* @param value
	*/

	add(value: T): number;
	/**
	* Remove an entry.
	* @param id
	*/
	remove(id: number): boolean;
	
	/**
	* Adds an entry to the registry if it doesn't exist.
	* @param value
	*/
	register(value: T): number;
	
	/**
	* Clears all entries.
	*/
	clear(): void;
}

EventPublisher

interface IEventPublisher<T>
{
	publish(payload: T): void;
	publishReverse(payload: T): void;

	readonly event: Readonly<Event<T>>;
}

Keywords

FAQs

Package last updated on 27 Apr 2020

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