1. Michael P. Jung
  2. opoge-pusher

Commits

Michael P. Jung  committed 3b8cc1c

Replace catch-all callback by subscribe method

  • Participants
  • Parent commits 0fc4310
  • Branches master

Comments (0)

Files changed (2)

File opoge/pusher/static/consumer.js

View file
  • Ignore whitespace
 			throw new Error('Only one instance of opoge.pusher.Consumer must ever be created');
 		}
 		this.sid = options.sid;
-		this.callback = options.callback;
 		this.running = false;
 		this.jqXHR = null;
+		this.subscribers = [];
 		// create tunnel iframe
 		this.tunnel = document.createElement('iframe');
 		var self = this;
 		}
 		this.running = false;
 	};
+	Consumer.prototype.subscribe = function(topicRegExp, callback) {
+		var subscriber = new Subscriber(this, topicRegExp, callback);
+		this.subscribers.push(subscriber);
+	};
+	Consumer.prototype.unsubscribe = function(subscription) {
+		for (i=0; i<this.subscribers; ++i) {
+			if (this.subscribers[i] == subscription) {
+				this.subscribers.splice(i, 1);
+				return;
+			}
+		}
+	};
+	Consumer.prototype.processMessage = function(message) {
+		var topic = message[0];
+		var data = message[1];
+		if (this.subscribers == null) {
+			throw new Error("Foo");
+		}
+		for (var i=0; i<this.subscribers.length; ++i) {
+			var subscriber = this.subscribers[i];
+			if (topic.match(subscriber.topicRegExp)) {
+				subscriber.callback(topic, data);
+			}
+		}
+	};
 	Consumer.prototype._pull = function() {
 		if (typeof this.sid === 'undefined') {
 			throw new Error('sid is mandatory');
 		}
-		if (typeof this.callback === 'undefined') {
-			throw new Error('callback is mandatory');
-		}
 		if (typeof this.$ === 'undefined') {
 			throw new Error('Consumer is not ready, yet');
 		}
 				'X-Session-Id': this.sid
 			},
 			success: function(data) {
-				self.callback(data);
+				var messages = data.messages;
+				for (var i=0; i<messages.length; ++i) {
+					self.processMessage(messages[i]);
+				}
 				self._pull();
 			}
 		});
 	}
 
+	function Subscriber(consumer, topicRegExp, callback) {
+		this.consumer = consumer;
+		this.topicRegExp = topicRegExp;
+		this.callback = callback;
+	}
+	Subscriber.prototype.unsubscribe = function() {
+		this.consumer.unsubscribe(this);
+	};
+
 }());

File opoge/pusher/templates/debug.html

View file
  • Ignore whitespace
 			this.objects = ko.observableArray();
 			var self = this;
 			this.consumer = new opoge.pusher.Consumer({
-				sid: this.sessionId,
-				callback: function(data) {
-					self.processMessages(data)
-				}
+				sid: this.sessionId
 			});
+			this.consumer.subscribe(/^opoge.pusher.debug$/,
+					this.processMessage.bind(this));
 		},
 		connect: function() {
 			this.consumer.ready(this.consumer.start);
 			this.loadSessions();
 			this.loadObjects();
 		},
-		processMessages: function(data) {
-			var messages = data.messages;
-			for (var i=0; i<messages.length; ++i) {
-				this.processMessage(messages[i][0], messages[i][1]);
-			}
-		},
-		processMessage: function(topic, message) {
-			var action = message[0];
-			var data = message[1];
+		processMessage: function(topic, data) {
+			var action = data[0];
+			var data = data[1];
 			switch (action) {
 				case 'session.create':
 					var session = new SessionModel(data);