Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
39 lines (31 sloc) 1.15 KB
import { EventEmitter } from 'events';
import { PubSubEngine } from './pubsub-engine';
export interface PubSubOptions {
eventEmitter?: EventEmitter;
}
export class PubSub extends PubSubEngine {
protected ee: EventEmitter;
private subscriptions: { [key: string]: [string, (...args: any[]) => void] };
private subIdCounter: number;
constructor(options: PubSubOptions = {}) {
super();
this.ee = options.eventEmitter || new EventEmitter();
this.subscriptions = {};
this.subIdCounter = 0;
}
public publish(triggerName: string, payload: any): Promise<void> {
this.ee.emit(triggerName, payload);
return Promise.resolve();
}
public subscribe(triggerName: string, onMessage: (...args: any[]) => void): Promise<number> {
this.ee.addListener(triggerName, onMessage);
this.subIdCounter = this.subIdCounter + 1;
this.subscriptions[this.subIdCounter] = [triggerName, onMessage];
return Promise.resolve(this.subIdCounter);
}
public unsubscribe(subId: number) {
const [triggerName, onMessage] = this.subscriptions[subId];
delete this.subscriptions[subId];
this.ee.removeListener(triggerName, onMessage);
}
}
You can’t perform that action at this time.