UNPKG

708 BJavaScriptView Raw
1module.exports = InMemory
2
3var uuid = require('uuid')
4
5function InMemory (opts) {
6 opts = opts || {}
7 this.uris = opts.uris || {}
8 this.subscribers = opts.subscribers || {}
9}
10
11InMemory.prototype.subscribe = function (channel, uri, client, callback) {
12 var self = this
13
14 if (!this.subscribers[channel]) {
15 this.subscribers[channel] = {}
16 }
17
18 if (!client.uuid) {
19 client.uuid = uuid.v1()
20 }
21
22 this.subscribers[channel][client.uuid] = [client, uri]
23
24 client.on('close', function () {
25 delete self.subscribers[channel][client.uuid]
26 })
27
28 return callback(null, client.uuid)
29}
30
31InMemory.prototype.get = function (channel, callback) {
32 return callback(null, this.subscribers[channel] || {})
33}