Mailspring/core/delta-stream-queue.js
2016-06-20 17:28:26 -07:00

29 lines
786 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}`
}
hasSubscribers(accountId) {
return this.client.existsAsync(this.key(accountId))
}
notify(accountId, data) {
return this.hasSubscribers(accountId).then((hasSubscribers) => {
if (!hasSubscribers) return Promise.resolve()
return this.client.rpushAsync(this.key(accountId), JSON.stringify(data))
})
}
}
module.exports = new DeltaStreamQueue()