-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathclient.js
48 lines (40 loc) · 1.12 KB
/
client.js
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
40
41
42
43
44
45
46
47
48
Meteor.Stream = function Stream(name, callback) {
EV.call(this);
var self = this;
var streamName = 'stream-' + name;
var collection = new Meteor.Collection(streamName);
var subscription;
var subscriptionId;
var connected = false;
var pendingEvents = [];
self._emit = self.emit;
collection.find({}).observe({
"added": function(item) {
if(item.type == 'subscriptionId') {
subscriptionId = item._id;
connected = true;
pendingEvents.forEach(function(args) {
self.emit.apply(self, args);
});
pendingEvents = [];
} else {
var context = {};
context.subscriptionId = item.subscriptionId;
context.userId = item.userId;
self._emit.apply(context, item.args);
}
}
});
subscription = Meteor.subscribe(streamName, callback);
self.emit = function emit() {
if(connected) {
Meteor.call(streamName, subscriptionId, arguments);
} else {
pendingEvents.push(arguments);
}
};
self.close = function close() {
subscription.stop();
};
}
_.extend(Meteor.Stream.prototype, EV.prototype);