Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/electricessence/eventpublisher
A strongly typed protected event creator/publisher/signaler for use with TypeScript and JavaScript.
https://github.com/electricessence/eventpublisher
Last synced: 8 days ago
JSON representation
A strongly typed protected event creator/publisher/signaler for use with TypeScript and JavaScript.
- Host: GitHub
- URL: https://github.com/electricessence/eventpublisher
- Owner: electricessence
- License: mit
- Created: 2020-04-25T21:53:14.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-11-14T04:15:14.000Z (about 2 years ago)
- Last Synced: 2024-04-27T06:20:15.984Z (7 months ago)
- Language: TypeScript
- Size: 53.7 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# EventPublisher
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://github.com/electricessence/EventPublisher/blob/master/LICENSE)
[![npm version](https://img.shields.io/npm/v/event-publisher.svg?style=flat-square)](https://www.npmjs.com/package/event-publisher)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
```ts
import { Event, EventPublisher } from '../src/EventPublisher';export class MyObservable {
readonly start:Event;
readonly update:Event;
readonly complete:Event;private readonly _dispatcher:{
start:EventPublisher,
update:EventPublisher,
complete:EventPublisher
};constructor()
{
const start = new EventPublisher(1);
const update = new EventPublisher();
const complete = new EventPublisher(1);
this._dispatcher = { start, update, complete };
this.start = start.event;
this.update = update.event;
this.complete = complete.event;
}
}
```## API
### Event
```ts
type Listener = (value: T) => void;
type Unsubscribe = () => void;interface Event
{
/**
* Adds a listener and return an unsubscribe function.
* @param listener
*/
(listener: Listener): 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
```ts
interface IEventPublisher
{
publish(payload: T): void;
publishReverse(payload: T): void;readonly event: Readonly>;
}
```