Mailspring/core/delta-stream-queue.js

30 lines
747 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(process.env.REDIS_URL || null);
2016-06-21 08:28:26 +08:00
this.client.on("error", console.error);
}
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()