mirror of
https://github.com/Foundry376/Mailspring.git
synced 2025-01-10 02:03:07 +08:00
33 lines
889 B
JavaScript
33 lines
889 B
JavaScript
const Rx = require('rx')
|
|
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);
|
|
this.client.on("error", console.error);
|
|
}
|
|
|
|
key(accountId) {
|
|
return `delta-${accountId}`
|
|
}
|
|
|
|
notify(accountId, data) {
|
|
this.client.publish(this.key(accountId), JSON.stringify(data))
|
|
}
|
|
|
|
fromAccountId(accountId) {
|
|
return Rx.Observable.create((observer) => {
|
|
this.client.on("message", (channel, message) => {
|
|
if (channel !== this.key(accountId)) { return }
|
|
observer.onNext(message)
|
|
});
|
|
this.client.subscribe(this.key(accountId));
|
|
return () => { this.client.unsubscribe() }
|
|
})
|
|
}
|
|
}
|
|
|
|
module.exports = new DeltaStreamQueue()
|