Mailspring/core/delta-stream-queue.js

31 lines
781 B
JavaScript
Raw Normal View History

2016-06-21 08:28:26 +08:00
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}`
}
2016-06-22 06:57:50 +08:00
notify(accountId, data) {
this.client.publish(this.key(accountId), JSON.stringify(data))
2016-06-21 08:28:26 +08:00
}
2016-06-22 06:57:50 +08:00
subscribe(accountId, callback) {
this.client.on("message", (channel, message) => {
if (channel !== this.key(accountId)) { return }
callback(message)
2016-06-21 08:28:26 +08:00
})
2016-06-22 06:57:50 +08:00
this.client.subscribe(this.key(accountId))
2016-06-21 08:28:26 +08:00
}
}
module.exports = new DeltaStreamQueue()