The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/packages/meteor-streams/lib/client.js

48 lines
1.1 KiB

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);