2016-06-23 15:49:22 +08:00
|
|
|
const Rx = require('rx')
|
|
|
|
const redis = require("redis");
|
2016-06-24 02:45:24 +08:00
|
|
|
|
|
|
|
const SyncPolicy = require('./sync-policy');
|
|
|
|
|
2016-06-24 06:52:45 +08:00
|
|
|
Promise.promisifyAll(redis.RedisClient.prototype);
|
|
|
|
Promise.promisifyAll(redis.Multi.prototype);
|
2016-06-23 15:49:22 +08:00
|
|
|
|
|
|
|
class PubsubConnector {
|
|
|
|
constructor() {
|
|
|
|
this._broadcastClient = null;
|
|
|
|
this._listenClient = null;
|
|
|
|
this._listenClientSubs = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
buildClient() {
|
|
|
|
const client = redis.createClient(process.env.REDIS_URL || null);
|
|
|
|
client.on("error", console.error);
|
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
|
|
|
broadcastClient() {
|
|
|
|
if (!this._broadcastClient) {
|
|
|
|
this._broadcastClient = this.buildClient();
|
|
|
|
}
|
|
|
|
return this._broadcastClient;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shared channel
|
2016-06-28 07:05:31 +08:00
|
|
|
_observableForChannelOnSharedListener(channel) {
|
2016-06-23 15:49:22 +08:00
|
|
|
if (!this._listenClient) {
|
|
|
|
this._listenClient = this.buildClient();
|
|
|
|
this._listenClientSubs = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return Rx.Observable.create((observer) => {
|
|
|
|
this._listenClient.on("message", (msgChannel, message) => {
|
|
|
|
if (msgChannel !== channel) { return }
|
|
|
|
observer.onNext(message)
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!this._listenClientSubs[channel]) {
|
|
|
|
this._listenClientSubs[channel] = 1;
|
|
|
|
this._listenClient.subscribe(channel);
|
|
|
|
} else {
|
|
|
|
this._listenClientSubs[channel] += 1;
|
|
|
|
}
|
|
|
|
return () => {
|
|
|
|
this._listenClientSubs[channel] -= 1;
|
|
|
|
if (this._listenClientSubs[channel] === 0) {
|
|
|
|
this._listenClient.unsubscribe(channel);
|
|
|
|
}
|
|
|
|
}
|
2016-06-28 07:05:31 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-29 06:35:35 +08:00
|
|
|
notify({accountId, type, data}) {
|
|
|
|
this.broadcastClient().publish(`channel-${accountId}`, {type, data});
|
2016-06-28 07:05:31 +08:00
|
|
|
}
|
|
|
|
|
2016-06-29 06:35:35 +08:00
|
|
|
observe(accountId) {
|
|
|
|
return this._observableForChannelOnSharedListener(`channel-${accountId}`);
|
2016-06-28 07:05:31 +08:00
|
|
|
}
|
2016-06-23 15:49:22 +08:00
|
|
|
|
2016-06-29 06:35:35 +08:00
|
|
|
notifyDelta(accountId, data) {
|
|
|
|
this.broadcastClient().publish(`channel-${accountId}-deltas`, JSON.stringify(data))
|
2016-06-23 15:49:22 +08:00
|
|
|
}
|
|
|
|
|
2016-06-29 06:35:35 +08:00
|
|
|
observeDeltas(accountId) {
|
2016-06-23 15:49:22 +08:00
|
|
|
return Rx.Observable.create((observer) => {
|
|
|
|
const sub = this.buildClient();
|
|
|
|
sub.on("message", (channel, message) => observer.onNext(message));
|
2016-06-29 06:35:35 +08:00
|
|
|
sub.subscribe(`channel-${accountId}-deltas`);
|
2016-06-23 15:49:22 +08:00
|
|
|
return () => {
|
|
|
|
sub.unsubscribe();
|
|
|
|
sub.quit();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = new PubsubConnector()
|