build/faye.js in faye-0.3.0 vs build/faye.js in faye-0.3.1

- old
+ new

@@ -10,11 +10,11 @@ } return dest; }; Faye.extend(Faye, { - VERSION: '0.3.0', + VERSION: '0.3.1', BAYEUX_VERSION: '1.0', ID_LENGTH: 128, JSONP_CALLBACK: 'jsonpcallback', CONNECTION_TYPES: ["long-polling", "callback-polling"], @@ -166,10 +166,38 @@ return klass; }; +Faye.Deferrable = { + callback: function(callback, scope) { + if (this._deferredStatus === 'succeeded') + return callback.apply(scope, this._deferredArgs); + + this._waiters = this._waiters || []; + this._waiters.push([callback, scope]); + }, + + setDeferredStatus: function() { + var args = Array.prototype.slice.call(arguments), + status = args.shift(); + + this._deferredStatus = status; + this._deferredArgs = args; + + if (status !== 'succeeded') return; + if (!this._waiters) return; + + Faye.each(this._waiters, function(callback) { + callback[0].apply(callback[1], this._deferredArgs); + }, this); + + this._waiters = []; + } +}; + + Faye.Observable = { on: function(eventType, block, scope) { this._observers = this._observers || {}; var list = this._observers[eventType] = this._observers[eventType] || []; list.push([block, scope]); @@ -507,16 +535,16 @@ if (this._advice.reconnect === this.HANDSHAKE || this._state === this.UNCONNECTED) return this.handshake(function() { this.connect(callback, scope) }, this); if (this._state === this.CONNECTING) - return this._callbacks.push([callback, scope]); + return this.callback(callback, scope); if (this._state !== this.CONNECTED) return; - Faye.each(this._callbacks, function(listener) { listener[0].call(listener[1]) }); - this._callbacks = []; + this.setDeferredStatus('succeeded'); + this.setDeferredStatus('deferred'); if (callback) callback.call(scope); if (this._connectionId) return; this._connectionId = this._namespace.generate(); var self = this; @@ -676,11 +704,13 @@ throw 'Clients may not subscribe to channel "' + channel + '"'; }); } }); +Faye.extend(Faye.Client.prototype, Faye.Deferrable); + Faye.Set = Faye.Class({ initialize: function() { this._index = {}; }, @@ -996,11 +1026,11 @@ this._channels.remove(channel); channel.stopObserving('message', this._onMessage, this); }, connect: function(callback) { - this.on('flush', callback); + this.callback(callback); if (this._connected) return; this._connected = true; if (this._deletionTimeout) { @@ -1017,12 +1047,12 @@ this._releaseConnection(); var events = this._inbox.toArray(); this._inbox = new Faye.Set(); - this.fire('flush', events); - this.stopObserving('flush'); + this.setDeferredStatus('succeeded', events); + this.setDeferredStatus('deferred'); }, disconnect: function() { this.unsubscribe('all'); this.flush(); @@ -1069,9 +1099,10 @@ self.fire('staleClient', self); }, 10 * 1000 * this.INTERVAL); } }); +Faye.extend(Faye.Connection.prototype, Faye.Deferrable); Faye.extend(Faye.Connection.prototype, Faye.Observable); Faye.Error = Faye.Class({ initialize: function(code, args, message) { \ No newline at end of file