Skip to content

Make a little more oo-ish. #3240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions src/Adapters/MessageQueue/EventEmitterMQ.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import events from 'events';

const emitter = new events.EventEmitter();
const subscriptions = new Map();

function unsubscribe(channel: string) {
if (!subscriptions.has(channel)) {
//console.log('No channel to unsub from');
return;
}
//console.log('unsub ', channel);
emitter.removeListener(channel, subscriptions.get(channel));
subscriptions.delete(channel);
}

class Publisher {
emitter: any;
Expand All @@ -26,6 +15,7 @@ class Publisher {
}

class Consumer extends events.EventEmitter {
static subscriptions = new Map();
emitter: any;

constructor(emitter: any) {
Expand All @@ -34,16 +24,23 @@ class Consumer extends events.EventEmitter {
}

subscribe(channel: string): void {
unsubscribe(channel);
this.unsubscribe(channel);
const handler = (message) => {
this.emit('message', channel, message);
}
subscriptions.set(channel, handler);
Consumer.subscriptions.set(channel, handler);
this.emitter.on(channel, handler);
}

unsubscribe(channel: string): void {
unsubscribe(channel);
if (!Consumer.subscriptions.has(channel)) {
//console.log('No channel to unsub from');
return;
}

//console.log('unsub ', channel);
this.emitter.removeListener(channel, Consumer.subscriptions.get(channel));
Consumer.subscriptions.delete(channel);
}
}

Expand Down