-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathmemory.ts
More file actions
39 lines (33 loc) · 1.18 KB
/
memory.ts
File metadata and controls
39 lines (33 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
var PubSub = require('./index');
var util = require('../util');
// In-memory ShareDB pub/sub
//
// This is a fully functional implementation. Since ShareDB does not require
// persistence of pub/sub state, it may be used in production environments
// requiring only a single stand alone server process. Additionally, it is
// easy to swap in an external pub/sub adapter if/when additional server
// processes are desired. No pub/sub APIs are adapter specific.
function MemoryPubSub(options) {
if (!(this instanceof MemoryPubSub)) return new MemoryPubSub(options);
PubSub.call(this, options);
}
module.exports = MemoryPubSub;
MemoryPubSub.prototype = Object.create(PubSub.prototype);
MemoryPubSub.prototype._subscribe = function(channel, callback) {
util.nextTick(callback);
};
MemoryPubSub.prototype._unsubscribe = function(channel, callback) {
util.nextTick(callback);
};
MemoryPubSub.prototype._publish = function(channels, data, callback) {
var pubsub = this;
util.nextTick(function() {
for (var i = 0; i < channels.length; i++) {
var channel = channels[i];
if (pubsub.subscribed[channel]) {
pubsub._emit(channel, data);
}
}
callback();
});
};