Update pusher to 2.2.0

This commit is contained in:
Piotr Sarnacki 2014-04-16 09:38:12 +02:00
parent 9e2de81d21
commit 4024de35cc

View File

@ -1,5 +1,5 @@
/*! /*!
* Pusher JavaScript Library v2.2.0-rc2 * Pusher JavaScript Library v2.2.0
* http://pusherapp.com/ * http://pusherapp.com/
* *
* Copyright 2013, Pusher * Copyright 2013, Pusher
@ -40,9 +40,9 @@
} }
var getStrategy = function(options) { var getStrategy = function(options) {
var config = Pusher.Util.extend({}, self.config, options);
return Pusher.StrategyBuilder.build( return Pusher.StrategyBuilder.build(
Pusher.getDefaultStrategy(self.config), Pusher.getDefaultStrategy(config), config
Pusher.Util.extend({}, self.config, options)
); );
}; };
@ -219,6 +219,14 @@
}).call(this); }).call(this);
;(function() { ;(function() {
// We need to bind clear functions this way to avoid exceptions on IE8
function clearTimeout(timer) {
window.clearTimeout(timer);
}
function clearInterval(timer) {
window.clearInterval(timer);
}
function GenericTimer(set, clear, delay, callback) { function GenericTimer(set, clear, delay, callback) {
var self = this; var self = this;
@ -242,7 +250,8 @@
/** Aborts a timer when it's running. */ /** Aborts a timer when it's running. */
prototype.ensureAborted = function() { prototype.ensureAborted = function() {
if (this.timer) { if (this.timer) {
this.clear.call(window, this.timer); // Clear function is already bound
this.clear(this.timer);
this.timer = null; this.timer = null;
} }
}; };
@ -402,9 +411,9 @@
* @param {Array} array * @param {Array} array
* @param {Function} f * @param {Function} f
*/ */
apply: function(array, f) { apply: function(array, f, context) {
for (var i = 0; i < array.length; i++) { for (var i = 0; i < array.length; i++) {
f(array[i], i, array); f.call(context || window, array[i], i, array);
} }
}, },
@ -617,7 +626,7 @@
}).call(this); }).call(this);
;(function() { ;(function() {
Pusher.VERSION = '2.2.0-rc2'; Pusher.VERSION = '2.2.0';
Pusher.PROTOCOL = 7; Pusher.PROTOCOL = 7;
// DEPRECATED: WS connection parameters // DEPRECATED: WS connection parameters
@ -643,11 +652,30 @@
Pusher.dependency_suffix = ''; Pusher.dependency_suffix = '';
Pusher.getDefaultStrategy = function(config) { Pusher.getDefaultStrategy = function(config) {
var wsStrategy;
if (config.encrypted) {
wsStrategy = [
":best_connected_ever",
":ws_loop",
[":delayed", 2000, [":http_fallback_loop"]]
];
} else {
wsStrategy = [
":best_connected_ever",
":ws_loop",
[":delayed", 2000, [":wss_loop"]],
[":delayed", 5000, [":http_fallback_loop"]]
];
}
return [ return [
[":def", "ws_options", { [":def", "ws_options", {
hostUnencrypted: config.wsHost + ":" + config.wsPort, hostUnencrypted: config.wsHost + ":" + config.wsPort,
hostEncrypted: config.wsHost + ":" + config.wssPort hostEncrypted: config.wsHost + ":" + config.wssPort
}], }],
[":def", "wss_options", [":extend", ":ws_options", {
encrypted: true
}]],
[":def", "sockjs_options", { [":def", "sockjs_options", {
hostUnencrypted: config.httpHost + ":" + config.httpPort, hostUnencrypted: config.httpHost + ":" + config.httpPort,
hostEncrypted: config.httpHost + ":" + config.httpsPort hostEncrypted: config.httpHost + ":" + config.httpsPort
@ -670,6 +698,7 @@
}]], }]],
[":def_transport", "ws", "ws", 3, ":ws_options", ":ws_manager"], [":def_transport", "ws", "ws", 3, ":ws_options", ":ws_manager"],
[":def_transport", "wss", "ws", 3, ":wss_options", ":ws_manager"],
[":def_transport", "flash", "flash", 2, ":ws_options", ":ws_manager"], [":def_transport", "flash", "flash", 2, ":ws_options", ":ws_manager"],
[":def_transport", "sockjs", "sockjs", 1, ":sockjs_options"], [":def_transport", "sockjs", "sockjs", 1, ":sockjs_options"],
[":def_transport", "xhr_streaming", "xhr_streaming", 1, ":sockjs_options", ":streaming_manager"], [":def_transport", "xhr_streaming", "xhr_streaming", 1, ":sockjs_options", ":streaming_manager"],
@ -678,6 +707,7 @@
[":def_transport", "xdr_polling", "xdr_polling", 1, ":sockjs_options"], [":def_transport", "xdr_polling", "xdr_polling", 1, ":sockjs_options"],
[":def", "ws_loop", [":sequential", ":timeouts", ":ws"]], [":def", "ws_loop", [":sequential", ":timeouts", ":ws"]],
[":def", "wss_loop", [":sequential", ":timeouts", ":wss"]],
[":def", "flash_loop", [":sequential", ":timeouts", ":flash"]], [":def", "flash_loop", [":sequential", ":timeouts", ":flash"]],
[":def", "sockjs_loop", [":sequential", ":timeouts", ":sockjs"]], [":def", "sockjs_loop", [":sequential", ":timeouts", ":sockjs"]],
@ -713,14 +743,15 @@
[":def", "strategy", [":def", "strategy",
[":cached", 1800000, [":cached", 1800000,
[":first_connected", [":first_connected",
[":if", [":is_supported", ":ws"], [ [":if", [":is_supported", ":ws"],
":best_connected_ever", ":ws_loop", [":delayed", 2000, [":http_fallback_loop"]] wsStrategy,
], [":if", [":is_supported", ":flash"], [ [":if", [":is_supported", ":flash"], [
":best_connected_ever", ":flash_loop", [":delayed", 2000, [":http_fallback_loop"]] ":best_connected_ever",
], [ ":flash_loop",
":http_fallback_loop" [":delayed", 2000, [":http_fallback_loop"]]
] ], [
]] ":http_fallback_loop"
]]]
] ]
] ]
] ]
@ -790,8 +821,8 @@
} }
var prototype = EventsDispatcher.prototype; var prototype = EventsDispatcher.prototype;
prototype.bind = function(eventName, callback) { prototype.bind = function(eventName, callback, context) {
this.callbacks.add(eventName, callback); this.callbacks.add(eventName, callback, context);
return this; return this;
}; };
@ -800,8 +831,8 @@
return this; return this;
}; };
prototype.unbind = function(eventName, callback) { prototype.unbind = function(eventName, callback, context) {
this.callbacks.remove(eventName, callback); this.callbacks.remove(eventName, callback, context);
return this; return this;
}; };
@ -820,7 +851,7 @@
var callbacks = this.callbacks.get(eventName); var callbacks = this.callbacks.get(eventName);
if (callbacks && callbacks.length > 0) { if (callbacks && callbacks.length > 0) {
for (i = 0; i < callbacks.length; i++) { for (i = 0; i < callbacks.length; i++) {
callbacks[i](data); callbacks[i].fn.call(callbacks[i].context || window, data);
} }
} else if (this.failThrough) { } else if (this.failThrough) {
this.failThrough(eventName, data); this.failThrough(eventName, data);
@ -835,46 +866,49 @@
this._callbacks = {}; this._callbacks = {};
} }
CallbackRegistry.prototype.get = function(eventName) { CallbackRegistry.prototype.get = function(name) {
return this._callbacks[this._prefix(eventName)]; return this._callbacks[prefix(name)];
}; };
CallbackRegistry.prototype.add = function(eventName, callback) { CallbackRegistry.prototype.add = function(name, callback, context) {
var prefixedEventName = this._prefix(eventName); var prefixedEventName = prefix(name);
this._callbacks[prefixedEventName] = this._callbacks[prefixedEventName] || []; this._callbacks[prefixedEventName] = this._callbacks[prefixedEventName] || [];
this._callbacks[prefixedEventName].push(callback); this._callbacks[prefixedEventName].push({
fn: callback,
context: context
});
}; };
CallbackRegistry.prototype.remove = function(eventName, callback) { CallbackRegistry.prototype.remove = function(name, callback, context) {
var callbacks = this.get(eventName); if (!name && !callback && !context) {
if (callbacks) { this._callbacks = {};
var index = arrayIndexOf(callbacks, callback); return;
if (index !== -1) { }
var callbacksCopy = callbacks.slice(0);
callbacksCopy.splice(index, 1); var names = name ? [prefix(name)] : Pusher.Util.keys(this._callbacks);
this._callbacks[this._prefix(eventName)] = callbacksCopy;
} if (callback || context) {
Pusher.Util.apply(names, function(name) {
this._callbacks[name] = Pusher.Util.filter(
this._callbacks[name] || [],
function(binding) {
return (callback && callback !== binding.fn) ||
(context && context !== binding.context);
}
);
if (this._callbacks[name].length === 0) {
delete this._callbacks[name];
}
}, this);
} else {
Pusher.Util.apply(names, function(name) {
delete this._callbacks[name];
}, this);
} }
}; };
CallbackRegistry.prototype._prefix = function(eventName) { function prefix(name) {
return "_" + eventName; return "_" + name;
};
function arrayIndexOf(array, item) {
var nativeIndexOf = Array.prototype.indexOf;
if (array === null) {
return -1;
}
if (nativeIndexOf && array.indexOf === nativeIndexOf) {
return array.indexOf(item);
}
for (var i = 0, l = array.length; i < l; i++) {
if (array[i] === item) {
return i;
}
}
return -1;
} }
Pusher.EventsDispatcher = EventsDispatcher; Pusher.EventsDispatcher = EventsDispatcher;
@ -2603,6 +2637,10 @@
} }
var globalContext = { var globalContext = {
extend: function(context, first, second) {
return [Pusher.Util.extend({}, first, second), context];
},
def: function(context, name, value) { def: function(context, name, value) {
if (context[name] !== undefined) { if (context[name] !== undefined) {
throw "Redefining symbol " + name; throw "Redefining symbol " + name;