Live query pubsub adapter (#2902)

* Moves LiveQuery pub/sub to adapter folder

* Adds ability to provide custom adapter for LiveQuery pubsub

* Adds test for function based adapter

* Pass all options to createSubscriber

* nits
This commit is contained in:
Florent Vilmart
2016-10-28 12:06:35 -04:00
committed by GitHub
parent f23c0a57ee
commit 23b77f7261
7 changed files with 92 additions and 30 deletions

View File

@@ -0,0 +1,59 @@
import events from 'events';
let emitter = new events.EventEmitter();
class Publisher {
emitter: any;
constructor(emitter: any) {
this.emitter = emitter;
}
publish(channel: string, message: string): void {
this.emitter.emit(channel, message);
}
}
class Subscriber extends events.EventEmitter {
emitter: any;
subscriptions: any;
constructor(emitter: any) {
super();
this.emitter = emitter;
this.subscriptions = new Map();
}
subscribe(channel: string): void {
let handler = (message) => {
this.emit('message', channel, message);
}
this.subscriptions.set(channel, handler);
this.emitter.on(channel, handler);
}
unsubscribe(channel: string): void {
if (!this.subscriptions.has(channel)) {
return;
}
this.emitter.removeListener(channel, this.subscriptions.get(channel));
this.subscriptions.delete(channel);
}
}
function createPublisher(): any {
return new Publisher(emitter);
}
function createSubscriber(): any {
return new Subscriber(emitter);
}
let EventEmitterPubSub = {
createPublisher,
createSubscriber
}
export {
EventEmitterPubSub
}

View File

@@ -0,0 +1,18 @@
import redis from 'redis';
function createPublisher({redisURL}): any {
return redis.createClient(redisURL, { no_ready_check: true });
}
function createSubscriber({redisURL}): any {
return redis.createClient(redisURL, { no_ready_check: true });
}
const RedisPubSub = {
createPublisher,
createSubscriber
}
export {
RedisPubSub
}