Mailspring/core/delta-stream-queue.js
2016-06-21 15:57:50 -07:00

30 lines
781 B
JavaScript

const bluebird = require('bluebird')
const redis = require("redis");
bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);
class DeltaStreamQueue {
setup() {
this.client = redis.createClient();
this.client.on("error", console.error);
this.client.on("ready", () => console.log("Redis ready"));
}
key(accountId) {
return `delta-${accountId}`
}
notify(accountId, data) {
this.client.publish(this.key(accountId), JSON.stringify(data))
}
subscribe(accountId, callback) {
this.client.on("message", (channel, message) => {
if (channel !== this.key(accountId)) { return }
callback(message)
})
this.client.subscribe(this.key(accountId))
}
}
module.exports = new DeltaStreamQueue()