centrifuge 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1 +1,3043 @@
1
- (function(){"use strict";function e(e,t){return e.prototype=Object.create(t.prototype),e.prototype.constructor=e,t.prototype}function t(){}function n(e,t){for(var n=e.length;n--;)if(e[n].listener===t)return n;return-1}function i(e){return function(){return this[e].apply(this,arguments)}}function s(e,t){for(var n=t||{},i=2;i<arguments.length;++i){var o=arguments[i];if(void 0!==o&&null!==o)for(var c in o){var u=r(o,c),h=r(n,c);if(u!==t&&void 0!==u)if(e&&"object"==typeof u&&null!==u)if(u instanceof Array)n[c]=s(e,h instanceof Array?h:[],u);else{var a="object"!=typeof h||h instanceof Array?{}:h;n[c]=s(e,a,u)}else n[c]=u}}return n}function r(e,t){try{return e[t]}catch(n){return void 0}}function o(e,t){return-1!==e.indexOf(t,e.length-t.length)}function c(e,t){return 0===e.lastIndexOf(t,0)}function u(e){return"/"==e.substring(e.length-1)&&(e=e.substring(0,e.length-1)),e}function h(e){return void 0===e||null===e?!1:"string"==typeof e||e instanceof String}function a(e){return void 0===e||null===e?!1:"function"==typeof e}function f(e,t){if(window.console){var n=window.console[e];a(n)&&n.apply(window.console,t)}}function l(e){this._sockjs=!1,this._sockjsVersion=null,this._status="disconnected",this._reconnect=!0,this._transport=null,this._messageId=0,this._clientId=null,this._subscriptions={},this._messages=[],this._isBatching=!1,this._isAuthBatching=!1,this._authChannels={},this._refreshTimeout=null,this._retry=null,this._config={retry:1e3,maxRetry:1e4,info:"",debug:!1,insecure:!1,server:null,privateChannelPrefix:"$",protocols_whitelist:["websocket","xdr-streaming","xhr-streaming","iframe-eventsource","iframe-htmlfile","xdr-polling","xhr-polling","iframe-xhr-polling","jsonp-polling"],transports:["websocket","xdr-streaming","xhr-streaming","eventsource","iframe-eventsource","iframe-htmlfile","xdr-polling","xhr-polling","iframe-xhr-polling","jsonp-polling"],refreshEndpoint:"/centrifuge/refresh",refreshHeaders:{},refreshParams:{},refreshTransport:"ajax",authEndpoint:"/centrifuge/auth",authHeaders:{},authParams:{},authTransport:"ajax"},e&&this.configure(e)}function g(e,t){this._centrifuge=e,this.channel=t}Object.create||(Object.create=function(){function e(){}return function(t){if(1!=arguments.length)throw new Error("Object.create implementation only accepts one parameter.");return e.prototype=t,new e}}()),Array.prototype.indexOf||(Array.prototype.indexOf=function(e){if(null==this)throw new TypeError;var t,n,i=Object(this),s=i.length>>>0;if(0===s)return-1;if(t=0,arguments.length>1&&(t=Number(arguments[1]),t!=t?t=0:0!=t&&1/0!=t&&t!=-1/0&&(t=(t>0||-1)*Math.floor(Math.abs(t)))),t>=s)return-1;for(n=t>=0?t:Math.max(s-Math.abs(t),0);s>n;n++)if(n in i&&i[n]===e)return n;return-1});var d=t.prototype;d.getListeners=function(e){var t,n,i=this._getEvents();if("object"==typeof e){t={};for(n in i)i.hasOwnProperty(n)&&e.test(n)&&(t[n]=i[n])}else t=i[e]||(i[e]=[]);return t},d.flattenListeners=function(e){var t,n=[];for(t=0;t<e.length;t+=1)n.push(e[t].listener);return n},d.getListenersAsObject=function(e){var t,n=this.getListeners(e);return n instanceof Array&&(t={},t[e]=n),t||n},d.addListener=function(e,t){var i,s=this.getListenersAsObject(e),r="object"==typeof t;for(i in s)s.hasOwnProperty(i)&&-1===n(s[i],t)&&s[i].push(r?t:{listener:t,once:!1});return this},d.on=i("addListener"),d.addOnceListener=function(e,t){return this.addListener(e,{listener:t,once:!0})},d.once=i("addOnceListener"),d.defineEvent=function(e){return this.getListeners(e),this},d.defineEvents=function(e){for(var t=0;t<e.length;t+=1)this.defineEvent(e[t]);return this},d.removeListener=function(e,t){var i,s,r=this.getListenersAsObject(e);for(s in r)r.hasOwnProperty(s)&&(i=n(r[s],t),-1!==i&&r[s].splice(i,1));return this},d.off=i("removeListener"),d.addListeners=function(e,t){return this.manipulateListeners(!1,e,t)},d.removeListeners=function(e,t){return this.manipulateListeners(!0,e,t)},d.manipulateListeners=function(e,t,n){var i,s,r=e?this.removeListener:this.addListener,o=e?this.removeListeners:this.addListeners;if("object"!=typeof t||t instanceof RegExp)for(i=n.length;i--;)r.call(this,t,n[i]);else for(i in t)t.hasOwnProperty(i)&&(s=t[i])&&("function"==typeof s?r.call(this,i,s):o.call(this,i,s));return this},d.removeEvent=function(e){var t,n=typeof e,i=this._getEvents();if("string"===n)delete i[e];else if("object"===n)for(t in i)i.hasOwnProperty(t)&&e.test(t)&&delete i[t];else delete this._events;return this},d.emitEvent=function(e,t){var n,i,s,r,o=this.getListenersAsObject(e);for(s in o)if(o.hasOwnProperty(s))for(i=o[s].length;i--;)n=o[s][i],n.once===!0&&this.removeListener(e,n.listener),r=n.listener.apply(this,t||[]),r===this._getOnceReturnValue()&&this.removeListener(e,n.listener);return this},d.trigger=i("emitEvent"),d.emit=function(e){var t=Array.prototype.slice.call(arguments,1);return this.emitEvent(e,t)},d.setOnceReturnValue=function(e){return this._onceReturnValue=e,this},d._getOnceReturnValue=function(){return this.hasOwnProperty("_onceReturnValue")?this._onceReturnValue:!0},d._getEvents=function(){return this._events||(this._events={})},e(l,t),l._authCallbacks={},l._nextAuthCallbackID=1;var _=l.prototype;_._jsonp=function(e,t,n,i,s){n.length>0&&this._log("Only AJAX request allows to send custom headers, it's not possible with JSONP."),self._debug("sending JSONP request to",e);var r=l._nextAuthCallbackID.toString();l._nextAuthCallbackID++;var o=window.document,c=o.createElement("script");l._authCallbacks[r]=function(e){s(!1,e),delete l[r]};var u="";for(var h in t)u.length>0&&(u+="&"),u+=encodeURIComponent(h)+"="+encodeURIComponent(t[h]);var a="Centrifuge._authCallbacks['"+r+"']";c.src=this._config.authEndpoint+"?callback="+encodeURIComponent(a)+"&data="+encodeURIComponent(JSON.stringify(i))+"&"+u;var f=o.getElementsByTagName("head")[0]||o.documentElement;f.insertBefore(c,f.firstChild)},_._ajax=function(e,t,n,i,s){var r=this;r._debug("sending AJAX request to",e);var o=window.XMLHttpRequest?new window.XMLHttpRequest:new ActiveXObject("Microsoft.XMLHTTP"),c="";for(var u in t)c.length>0&&(c+="&"),c+=encodeURIComponent(u)+"="+encodeURIComponent(t[u]);c.length>0&&(c="?"+c),o.open("POST",e+c,!0),o.setRequestHeader("X-Requested-With","XMLHttpRequest"),o.setRequestHeader("Content-Type","application/json");for(var h in n)o.setRequestHeader(h,n[h]);return o.onreadystatechange=function(){if(4===o.readyState)if(200===o.status){var e,t=!1;try{e=JSON.parse(o.responseText),t=!0}catch(n){s(!0,"JSON returned from webapp was invalid, yet status code was 200. Data was: "+o.responseText)}t&&s(!1,e)}else r._log("Couldn't get auth info from your webapp",o.status),s(!0,o.status)},setTimeout(function(){o.send(JSON.stringify(i))},20),o},_._log=function(){f("info",arguments)},_._debug=function(){this._config.debug===!0&&f("debug",arguments)},_._configure=function(e){if(this._debug("Configuring centrifuge object with",e),e||(e={}),this._config=s(!1,this._config,e),!this._config.url)throw"Missing required configuration parameter 'url' specifying server URL";if(!this._config.user&&""!==this._config.user){if(!this._config.insecure)throw"Missing required configuration parameter 'user' specifying user's unique ID in your application";this._debug("user not found but this is OK for insecure mode - anonymous access will be used"),this._config.user=""}if(!this._config.timestamp){if(!this._config.insecure)throw"Missing required configuration parameter 'timestamp'";this._debug("token not found but this is OK for insecure mode")}if(!this._config.token){if(!this._config.insecure)throw"Missing required configuration parameter 'token' specifying the sign of authorization request";this._debug("timestamp not found but this is OK for insecure mode")}if(this._config.url=u(this._config.url),o(this._config.url,"connection")){if(this._debug("client will connect to SockJS endpoint"),"undefined"==typeof SockJS)throw"include SockJS client library before Centrifuge javascript client library or use raw Websocket connection endpoint";this._sockjs=!0,this._sockjsVersion=SockJS.version}else o(this._config.url,"connection/websocket")?(this._debug("client will connect to raw Websocket endpoint"),this._config.url=this._config.url.replace("http://","ws://"),this._config.url=this._config.url.replace("https://","wss://")):(this._debug("client will detect connection endpoint itself"),"undefined"==typeof SockJS?(this._debug("no SockJS found, client will connect to raw Websocket endpoint"),this._config.url+="/connection/websocket",this._config.url=this._config.url.replace("http://","ws://"),this._config.url=this._config.url.replace("https://","wss://")):(this._debug("SockJS found, client will connect to SockJS endpoint"),this._config.url+="/connection",this._sockjs=!0,this._sockjsVersion=SockJS.version))},_._setStatus=function(e){this._status!==e&&(this._debug("Status",this._status,"->",e),this._status=e)},_._isDisconnected=function(){return this._isConnected()===!1},_._isConnected=function(){return"connected"===this._status},_._isConnecting=function(){return"connecting"===this._status},_._nextMessageId=function(){return++this._messageId},_._clearSubscriptions=function(){this._subscriptions={}},_._resetRetry=function(){this._debug("reset retry timeout"),this._retry=null},_._getRetry=function(){return this._retry=null===this._retry?this._config.retry+Math.round(1e3*Math.random()):this._retry+Math.round(1e3*Math.random()),this._retry>this._config.maxRetry&&(this._retry=this._config.maxRetry),this._retry},_._send=function(e){for(var t=0;t<e.length;++t){var n=e[t];n.uid=""+this._nextMessageId(),this._clientId&&(n.clientId=this._clientId),this._debug("Send",n),this._transport.send(JSON.stringify(n))}},_._connect=function(e){if(!this.isConnected()){this._clientId=null,this._reconnect=!0,this._clearSubscriptions(),this._setStatus("connecting");var t=this;if(e&&this.on("connect",e),this._sockjs===!0){var n={};c(this._sockjsVersion,"1.")?n.transports=this._config.transports:(this._log("SockJS <= 0.3.4 is deprecated, use SockJS >= 1.0.0 instead"),n.protocols_whitelist=this._config.protocols_whitelist),null!==this._config.server&&(n.server=this._config.server),this._transport=new SockJS(this._config.url,null,n)}else this._transport=new WebSocket(this._config.url);this._setStatus("connecting"),this._transport.onopen=function(){t._resetRetry();var e={method:"connect",params:{user:t._config.user,info:t._config.info}};t._config.insecure||(e.params.timestamp=t._config.timestamp,e.params.token=t._config.token),t.send(e)},this._transport.onerror=function(e){t._debug(e)},this._transport.onclose=function(){if(t._setStatus("disconnected"),t.trigger("disconnect"),t._reconnect===!0){var e=t._getRetry();t._debug("reconnect after "+e+" milliseconds"),window.setTimeout(function(){t._reconnect===!0&&t._connect.call(t)},e)}},this._transport.onmessage=function(e){var n;n=JSON.parse(e.data),t._debug("Received",n),t._receive(n)}}},_._disconnect=function(){this._clientId=null,this._setStatus("disconnected"),this._subscriptions={},this._reconnect=!1,this._transport.close()},_._getSubscription=function(e){var t;return t=this._subscriptions[e],t?t:null},_._removeSubscription=function(e){try{delete this._subscriptions[e]}catch(t){this._debug("nothing to delete for channel ",e)}try{delete this._authChannels[e]}catch(t){this._debug("nothing to delete from authChannels for channel ",e)}},_._connectResponse=function(e){if(!this.isConnected())if(null===e.error){if(!e.body)return;if(e.body.expires){var t=e.body.expired;if(t)return void this.refresh()}if(this._clientId=e.body.client,this._setStatus("connected"),this.trigger("connect",[e]),this._refreshTimeout&&window.clearTimeout(this._refreshTimeout),e.body.expires){var n=this;this._refreshTimeout=window.setTimeout(function(){n.refresh.call(n)},1e3*e.body.ttl)}}else this.trigger("error",[e]),this.trigger("connect:error",[e])},_._disconnectResponse=function(e){null===e.error?this.disconnect():(this.trigger("error",[e]),this.trigger("disconnect:error",[e.error]))},_._subscribeResponse=function(e){null!==e.error&&this.trigger("error",[e]);var t=e.body;if(null!==t){var n=t.channel,i=this.getSubscription(n);i&&(null===e.error?(i.trigger("subscribe:success",[t]),i.trigger("ready",[t])):(i.trigger("subscribe:error",[e.error]),i.trigger("error",[e])))}},_._unsubscribeResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&null===e.error&&(i.trigger("unsubscribe",[t]),this._centrifuge._removeSubscription(n))},_._publishResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&(null===e.error?i.trigger("publish:success",[t]):(i.trigger("publish:error",[e.error]),this.trigger("error",[e])))},_._presenceResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&(null===e.error?(i.trigger("presence",[t]),i.trigger("presence:success",[t])):(i.trigger("presence:error",[e.error]),this.trigger("error",[e])))},_._historyResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&(null===e.error?(i.trigger("history",[t]),i.trigger("history:success",[t])):(i.trigger("history:error",[e.error]),this.trigger("error",[e])))},_._joinResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&i.trigger("join",[t])},_._leaveResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&i.trigger("leave",[t])},_._messageResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);null!==i&&i.trigger("message",[t])},_._refreshResponse=function(e){if(this._refreshTimeout&&window.clearTimeout(this._refreshTimeout),e.body.expires){var t=this,n=e.body.expired;if(n)return void(t._refreshTimeout=window.setTimeout(function(){t.refresh.call(t)},3e3+Math.round(1e3*Math.random())));this._clientId=e.body.client,t._refreshTimeout=window.setTimeout(function(){t.refresh.call(t)},1e3*e.body.ttl)}},_._dispatchMessage=function(e){if(void 0!==e&&null!==e){var t=e.method;if(t)switch(t){case"connect":this._connectResponse(e);break;case"disconnect":this._disconnectResponse(e);break;case"subscribe":this._subscribeResponse(e);break;case"unsubscribe":this._unsubscribeResponse(e);break;case"publish":this._publishResponse(e);break;case"presence":this._presenceResponse(e);break;case"history":this._historyResponse(e);break;case"join":this._joinResponse(e);break;case"leave":this._leaveResponse(e);break;case"ping":break;case"refresh":this._refreshResponse(e);break;case"message":this._messageResponse(e)}}},_._receive=function(e){if(Object.prototype.toString.call(e)===Object.prototype.toString.call([])){for(var t in e)if(e.hasOwnProperty(t)){var n=e[t];this._dispatchMessage(n)}}else Object.prototype.toString.call(e)===Object.prototype.toString.call({})&&this._dispatchMessage(e)},_._flush=function(){var e=this._messages.slice(0);this._messages=[],this._send(e)},_._ping=function(){var e={method:"ping",params:{}};this.send(e)},_.getClientId=function(){return this._clientId},_.isConnected=_._isConnected,_.isConnecting=_._isConnecting,_.isDisconnected=_._isDisconnected,_.configure=function(e){this._configure.call(this,e)},_.connect=_._connect,_.disconnect=_._disconnect,_.getSubscription=_._getSubscription,_.ping=_._ping,_.send=function(e){this._isBatching===!0?this._messages.push(e):this._send([e])},_.startBatching=function(){this._isBatching=!0},_.stopBatching=function(e){e=e||!1,this._isBatching=!1,e===!0&&this.flush()},_.flush=function(){this._flush()},_.startAuthBatching=function(){this._isAuthBatching=!0},_.stopAuthBatching=function(e){this._isAuthBatching=!1;var t=this._authChannels;this._authChannels={};var n=[];for(var i in t){var s=this.getSubscription(i);s&&n.push(i)}if(0==n.length)return void(e&&e());var r={client:this.getClientId(),channels:n},o=this,c=function(t,i){if(t===!0){o._debug("authorization request failed");for(var s in n){var r=n[s];o._subscribeResponse({error:"authorization request failed",body:{channel:r}})}return void(e&&e())}for(var s in n){var r=n[s],c=i[r];if(c)if(c.status&&200!==c.status)o._subscribeResponse({error:c.status,body:{channel:r}});else{var u={method:"subscribe",params:{channel:r,client:o.getClientId(),info:c.info,sign:c.sign}};o.send(u)}else o._subscribeResponse({error:404,body:{channel:r}})}e&&e()},u=this._config.authTransport.toLowerCase();if("ajax"===u)this._ajax(this._config.authEndpoint,this._config.authParams,this._config.authHeaders,r,c);else{if("jsonp"!==u)throw"Unknown auth transport "+u;this._jsonp(this._config.authEndpoint,this._config.authParams,this._config.authHeaders,r,c)}},_.subscribe=function(e,t){if(arguments.length<1)throw"Illegal arguments number: required 1, got "+arguments.length;if(!h(e))throw"Illegal argument type: channel must be a string";if(this.isDisconnected())throw"Can not subscribe in disconnected state";var n=this.getSubscription(e);if(null!==n)return n;var i=new g(this,e);return this._subscriptions[e]=i,i.subscribe(t),i},_.unsubscribe=function(e){if(arguments.length<1)throw"Illegal arguments number: required 1, got "+arguments.length;if(!h(e))throw"Illegal argument type: channel must be a string";if(!this.isDisconnected()){var t=this.getSubscription(e);null!==t&&t.unsubscribe()}},_.publish=function(e,t,n){var i=this.getSubscription(e);return null===i?(this._debug("subscription not found for channel "+e),null):(i.publish(t,n),i)},_.presence=function(e,t){var n=this.getSubscription(e);return null===n?(this._debug("subscription not found for channel "+e),null):(n.presence(t),n)},_.history=function(e,t){var n=this.getSubscription(e);return null===n?(this._debug("subscription not found for channel "+e),null):(n.history(t),n)},_.refresh=function(){var e=this;this._debug("refresh credentials");var t=function(t,n){if(t===!0)return e._debug("error getting connect parameters",n),e._refreshTimeout&&window.clearTimeout(e._refreshTimeout),void(e._refreshTimeout=window.setTimeout(function(){e.refresh.call(e)},3e3));if(e._config.user=n.user,e._config.timestamp=n.timestamp,e._config.info=n.info,e._config.token=n.token,e.isDisconnected())e._debug("credentials refreshed, connect from scratch"),e._connect();else{e._debug("send refreshed credentials");var i={method:"refresh",params:{user:e._config.user,timestamp:e._config.timestamp,info:e._config.info,token:e._config.token}};e.send(i)}},n=this._config.refreshTransport.toLowerCase();if("ajax"===n)this._ajax(this._config.refreshEndpoint,this._config.refreshParams,this._config.refreshHeaders,{},t);else{if("jsonp"!==n)throw"Unknown refresh transport "+n;this._jsonp(this._config.refreshEndpoint,this._config.refreshParams,this._config.refreshHeaders,{},t)}},e(g,t);var p=g.prototype;p.getChannel=function(){return this.channel},p.getCentrifuge=function(){return this._centrifuge},p.subscribe=function(e){var t={method:"subscribe",params:{channel:this.channel}};c(this.channel,this._centrifuge._config.privateChannelPrefix)?this._centrifuge._isAuthBatching?this._centrifuge._authChannels[this.channel]=!0:(this._centrifuge.startAuthBatching(),this.subscribe(e),this._centrifuge.stopAuthBatching()):this._centrifuge.send(t),e&&this.on("message",e)},p.unsubscribe=function(){this._centrifuge._removeSubscription(this.channel);var e={method:"unsubscribe",params:{channel:this.channel}};this._centrifuge.send(e)},p.publish=function(e,t){var n={method:"publish",params:{channel:this.channel,data:e}};t&&this.on("publish:success",t),this._centrifuge.send(n)},p.presence=function(e){var t={method:"presence",params:{channel:this.channel}};e&&this.on("presence",e),this._centrifuge.send(t)},p.history=function(e){var t={method:"history",params:{channel:this.channel}};e&&this.on("history",e),this._centrifuge.send(t)},"function"==typeof define&&define.amd?define(function(){return l}):"object"==typeof module&&module.exports?module.exports=l:this.Centrifuge=l}).call(this);
1
+ !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.Centrifuge=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
2
+ // shim for using process in browser
3
+
4
+ var process = module.exports = {};
5
+
6
+ process.nextTick = (function () {
7
+ var canSetImmediate = typeof window !== 'undefined'
8
+ && window.setImmediate;
9
+ var canMutationObserver = typeof window !== 'undefined'
10
+ && window.MutationObserver;
11
+ var canPost = typeof window !== 'undefined'
12
+ && window.postMessage && window.addEventListener
13
+ ;
14
+
15
+ if (canSetImmediate) {
16
+ return function (f) { return window.setImmediate(f) };
17
+ }
18
+
19
+ var queue = [];
20
+
21
+ if (canMutationObserver) {
22
+ var hiddenDiv = document.createElement("div");
23
+ var observer = new MutationObserver(function () {
24
+ var queueList = queue.slice();
25
+ queue.length = 0;
26
+ queueList.forEach(function (fn) {
27
+ fn();
28
+ });
29
+ });
30
+
31
+ observer.observe(hiddenDiv, { attributes: true });
32
+
33
+ return function nextTick(fn) {
34
+ if (!queue.length) {
35
+ hiddenDiv.setAttribute('yes', 'no');
36
+ }
37
+ queue.push(fn);
38
+ };
39
+ }
40
+
41
+ if (canPost) {
42
+ window.addEventListener('message', function (ev) {
43
+ var source = ev.source;
44
+ if ((source === window || source === null) && ev.data === 'process-tick') {
45
+ ev.stopPropagation();
46
+ if (queue.length > 0) {
47
+ var fn = queue.shift();
48
+ fn();
49
+ }
50
+ }
51
+ }, true);
52
+
53
+ return function nextTick(fn) {
54
+ queue.push(fn);
55
+ window.postMessage('process-tick', '*');
56
+ };
57
+ }
58
+
59
+ return function nextTick(fn) {
60
+ setTimeout(fn, 0);
61
+ };
62
+ })();
63
+
64
+ process.title = 'browser';
65
+ process.browser = true;
66
+ process.env = {};
67
+ process.argv = [];
68
+
69
+ function noop() {}
70
+
71
+ process.on = noop;
72
+ process.addListener = noop;
73
+ process.once = noop;
74
+ process.off = noop;
75
+ process.removeListener = noop;
76
+ process.removeAllListeners = noop;
77
+ process.emit = noop;
78
+
79
+ process.binding = function (name) {
80
+ throw new Error('process.binding is not supported');
81
+ };
82
+
83
+ // TODO(shtylman)
84
+ process.cwd = function () { return '/' };
85
+ process.chdir = function (dir) {
86
+ throw new Error('process.chdir is not supported');
87
+ };
88
+
89
+ },{}],2:[function(require,module,exports){
90
+ (function (process,global){
91
+ /*!
92
+ * @overview es6-promise - a tiny implementation of Promises/A+.
93
+ * @copyright Copyright (c) 2014 Yehuda Katz, Tom Dale, Stefan Penner and contributors (Conversion to ES6 API by Jake Archibald)
94
+ * @license Licensed under MIT license
95
+ * See https://raw.githubusercontent.com/jakearchibald/es6-promise/master/LICENSE
96
+ * @version 3.0.2
97
+ */
98
+
99
+ (function() {
100
+ "use strict";
101
+ function lib$es6$promise$utils$$objectOrFunction(x) {
102
+ return typeof x === 'function' || (typeof x === 'object' && x !== null);
103
+ }
104
+
105
+ function lib$es6$promise$utils$$isFunction(x) {
106
+ return typeof x === 'function';
107
+ }
108
+
109
+ function lib$es6$promise$utils$$isMaybeThenable(x) {
110
+ return typeof x === 'object' && x !== null;
111
+ }
112
+
113
+ var lib$es6$promise$utils$$_isArray;
114
+ if (!Array.isArray) {
115
+ lib$es6$promise$utils$$_isArray = function (x) {
116
+ return Object.prototype.toString.call(x) === '[object Array]';
117
+ };
118
+ } else {
119
+ lib$es6$promise$utils$$_isArray = Array.isArray;
120
+ }
121
+
122
+ var lib$es6$promise$utils$$isArray = lib$es6$promise$utils$$_isArray;
123
+ var lib$es6$promise$asap$$len = 0;
124
+ var lib$es6$promise$asap$$toString = {}.toString;
125
+ var lib$es6$promise$asap$$vertxNext;
126
+ var lib$es6$promise$asap$$customSchedulerFn;
127
+
128
+ var lib$es6$promise$asap$$asap = function asap(callback, arg) {
129
+ lib$es6$promise$asap$$queue[lib$es6$promise$asap$$len] = callback;
130
+ lib$es6$promise$asap$$queue[lib$es6$promise$asap$$len + 1] = arg;
131
+ lib$es6$promise$asap$$len += 2;
132
+ if (lib$es6$promise$asap$$len === 2) {
133
+ // If len is 2, that means that we need to schedule an async flush.
134
+ // If additional callbacks are queued before the queue is flushed, they
135
+ // will be processed by this flush that we are scheduling.
136
+ if (lib$es6$promise$asap$$customSchedulerFn) {
137
+ lib$es6$promise$asap$$customSchedulerFn(lib$es6$promise$asap$$flush);
138
+ } else {
139
+ lib$es6$promise$asap$$scheduleFlush();
140
+ }
141
+ }
142
+ }
143
+
144
+ function lib$es6$promise$asap$$setScheduler(scheduleFn) {
145
+ lib$es6$promise$asap$$customSchedulerFn = scheduleFn;
146
+ }
147
+
148
+ function lib$es6$promise$asap$$setAsap(asapFn) {
149
+ lib$es6$promise$asap$$asap = asapFn;
150
+ }
151
+
152
+ var lib$es6$promise$asap$$browserWindow = (typeof window !== 'undefined') ? window : undefined;
153
+ var lib$es6$promise$asap$$browserGlobal = lib$es6$promise$asap$$browserWindow || {};
154
+ var lib$es6$promise$asap$$BrowserMutationObserver = lib$es6$promise$asap$$browserGlobal.MutationObserver || lib$es6$promise$asap$$browserGlobal.WebKitMutationObserver;
155
+ var lib$es6$promise$asap$$isNode = typeof process !== 'undefined' && {}.toString.call(process) === '[object process]';
156
+
157
+ // test for web worker but not in IE10
158
+ var lib$es6$promise$asap$$isWorker = typeof Uint8ClampedArray !== 'undefined' &&
159
+ typeof importScripts !== 'undefined' &&
160
+ typeof MessageChannel !== 'undefined';
161
+
162
+ // node
163
+ function lib$es6$promise$asap$$useNextTick() {
164
+ // node version 0.10.x displays a deprecation warning when nextTick is used recursively
165
+ // see https://github.com/cujojs/when/issues/410 for details
166
+ return function() {
167
+ process.nextTick(lib$es6$promise$asap$$flush);
168
+ };
169
+ }
170
+
171
+ // vertx
172
+ function lib$es6$promise$asap$$useVertxTimer() {
173
+ return function() {
174
+ lib$es6$promise$asap$$vertxNext(lib$es6$promise$asap$$flush);
175
+ };
176
+ }
177
+
178
+ function lib$es6$promise$asap$$useMutationObserver() {
179
+ var iterations = 0;
180
+ var observer = new lib$es6$promise$asap$$BrowserMutationObserver(lib$es6$promise$asap$$flush);
181
+ var node = document.createTextNode('');
182
+ observer.observe(node, { characterData: true });
183
+
184
+ return function() {
185
+ node.data = (iterations = ++iterations % 2);
186
+ };
187
+ }
188
+
189
+ // web worker
190
+ function lib$es6$promise$asap$$useMessageChannel() {
191
+ var channel = new MessageChannel();
192
+ channel.port1.onmessage = lib$es6$promise$asap$$flush;
193
+ return function () {
194
+ channel.port2.postMessage(0);
195
+ };
196
+ }
197
+
198
+ function lib$es6$promise$asap$$useSetTimeout() {
199
+ return function() {
200
+ setTimeout(lib$es6$promise$asap$$flush, 1);
201
+ };
202
+ }
203
+
204
+ var lib$es6$promise$asap$$queue = new Array(1000);
205
+ function lib$es6$promise$asap$$flush() {
206
+ for (var i = 0; i < lib$es6$promise$asap$$len; i+=2) {
207
+ var callback = lib$es6$promise$asap$$queue[i];
208
+ var arg = lib$es6$promise$asap$$queue[i+1];
209
+
210
+ callback(arg);
211
+
212
+ lib$es6$promise$asap$$queue[i] = undefined;
213
+ lib$es6$promise$asap$$queue[i+1] = undefined;
214
+ }
215
+
216
+ lib$es6$promise$asap$$len = 0;
217
+ }
218
+
219
+ function lib$es6$promise$asap$$attemptVertx() {
220
+ try {
221
+ var r = require;
222
+ var vertx = r('vertx');
223
+ lib$es6$promise$asap$$vertxNext = vertx.runOnLoop || vertx.runOnContext;
224
+ return lib$es6$promise$asap$$useVertxTimer();
225
+ } catch(e) {
226
+ return lib$es6$promise$asap$$useSetTimeout();
227
+ }
228
+ }
229
+
230
+ var lib$es6$promise$asap$$scheduleFlush;
231
+ // Decide what async method to use to triggering processing of queued callbacks:
232
+ if (lib$es6$promise$asap$$isNode) {
233
+ lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useNextTick();
234
+ } else if (lib$es6$promise$asap$$BrowserMutationObserver) {
235
+ lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useMutationObserver();
236
+ } else if (lib$es6$promise$asap$$isWorker) {
237
+ lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useMessageChannel();
238
+ } else if (lib$es6$promise$asap$$browserWindow === undefined && typeof require === 'function') {
239
+ lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$attemptVertx();
240
+ } else {
241
+ lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useSetTimeout();
242
+ }
243
+
244
+ function lib$es6$promise$$internal$$noop() {}
245
+
246
+ var lib$es6$promise$$internal$$PENDING = void 0;
247
+ var lib$es6$promise$$internal$$FULFILLED = 1;
248
+ var lib$es6$promise$$internal$$REJECTED = 2;
249
+
250
+ var lib$es6$promise$$internal$$GET_THEN_ERROR = new lib$es6$promise$$internal$$ErrorObject();
251
+
252
+ function lib$es6$promise$$internal$$selfFulfillment() {
253
+ return new TypeError("You cannot resolve a promise with itself");
254
+ }
255
+
256
+ function lib$es6$promise$$internal$$cannotReturnOwn() {
257
+ return new TypeError('A promises callback cannot return that same promise.');
258
+ }
259
+
260
+ function lib$es6$promise$$internal$$getThen(promise) {
261
+ try {
262
+ return promise.then;
263
+ } catch(error) {
264
+ lib$es6$promise$$internal$$GET_THEN_ERROR.error = error;
265
+ return lib$es6$promise$$internal$$GET_THEN_ERROR;
266
+ }
267
+ }
268
+
269
+ function lib$es6$promise$$internal$$tryThen(then, value, fulfillmentHandler, rejectionHandler) {
270
+ try {
271
+ then.call(value, fulfillmentHandler, rejectionHandler);
272
+ } catch(e) {
273
+ return e;
274
+ }
275
+ }
276
+
277
+ function lib$es6$promise$$internal$$handleForeignThenable(promise, thenable, then) {
278
+ lib$es6$promise$asap$$asap(function(promise) {
279
+ var sealed = false;
280
+ var error = lib$es6$promise$$internal$$tryThen(then, thenable, function(value) {
281
+ if (sealed) { return; }
282
+ sealed = true;
283
+ if (thenable !== value) {
284
+ lib$es6$promise$$internal$$resolve(promise, value);
285
+ } else {
286
+ lib$es6$promise$$internal$$fulfill(promise, value);
287
+ }
288
+ }, function(reason) {
289
+ if (sealed) { return; }
290
+ sealed = true;
291
+
292
+ lib$es6$promise$$internal$$reject(promise, reason);
293
+ }, 'Settle: ' + (promise._label || ' unknown promise'));
294
+
295
+ if (!sealed && error) {
296
+ sealed = true;
297
+ lib$es6$promise$$internal$$reject(promise, error);
298
+ }
299
+ }, promise);
300
+ }
301
+
302
+ function lib$es6$promise$$internal$$handleOwnThenable(promise, thenable) {
303
+ if (thenable._state === lib$es6$promise$$internal$$FULFILLED) {
304
+ lib$es6$promise$$internal$$fulfill(promise, thenable._result);
305
+ } else if (thenable._state === lib$es6$promise$$internal$$REJECTED) {
306
+ lib$es6$promise$$internal$$reject(promise, thenable._result);
307
+ } else {
308
+ lib$es6$promise$$internal$$subscribe(thenable, undefined, function(value) {
309
+ lib$es6$promise$$internal$$resolve(promise, value);
310
+ }, function(reason) {
311
+ lib$es6$promise$$internal$$reject(promise, reason);
312
+ });
313
+ }
314
+ }
315
+
316
+ function lib$es6$promise$$internal$$handleMaybeThenable(promise, maybeThenable) {
317
+ if (maybeThenable.constructor === promise.constructor) {
318
+ lib$es6$promise$$internal$$handleOwnThenable(promise, maybeThenable);
319
+ } else {
320
+ var then = lib$es6$promise$$internal$$getThen(maybeThenable);
321
+
322
+ if (then === lib$es6$promise$$internal$$GET_THEN_ERROR) {
323
+ lib$es6$promise$$internal$$reject(promise, lib$es6$promise$$internal$$GET_THEN_ERROR.error);
324
+ } else if (then === undefined) {
325
+ lib$es6$promise$$internal$$fulfill(promise, maybeThenable);
326
+ } else if (lib$es6$promise$utils$$isFunction(then)) {
327
+ lib$es6$promise$$internal$$handleForeignThenable(promise, maybeThenable, then);
328
+ } else {
329
+ lib$es6$promise$$internal$$fulfill(promise, maybeThenable);
330
+ }
331
+ }
332
+ }
333
+
334
+ function lib$es6$promise$$internal$$resolve(promise, value) {
335
+ if (promise === value) {
336
+ lib$es6$promise$$internal$$reject(promise, lib$es6$promise$$internal$$selfFulfillment());
337
+ } else if (lib$es6$promise$utils$$objectOrFunction(value)) {
338
+ lib$es6$promise$$internal$$handleMaybeThenable(promise, value);
339
+ } else {
340
+ lib$es6$promise$$internal$$fulfill(promise, value);
341
+ }
342
+ }
343
+
344
+ function lib$es6$promise$$internal$$publishRejection(promise) {
345
+ if (promise._onerror) {
346
+ promise._onerror(promise._result);
347
+ }
348
+
349
+ lib$es6$promise$$internal$$publish(promise);
350
+ }
351
+
352
+ function lib$es6$promise$$internal$$fulfill(promise, value) {
353
+ if (promise._state !== lib$es6$promise$$internal$$PENDING) { return; }
354
+
355
+ promise._result = value;
356
+ promise._state = lib$es6$promise$$internal$$FULFILLED;
357
+
358
+ if (promise._subscribers.length !== 0) {
359
+ lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publish, promise);
360
+ }
361
+ }
362
+
363
+ function lib$es6$promise$$internal$$reject(promise, reason) {
364
+ if (promise._state !== lib$es6$promise$$internal$$PENDING) { return; }
365
+ promise._state = lib$es6$promise$$internal$$REJECTED;
366
+ promise._result = reason;
367
+
368
+ lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publishRejection, promise);
369
+ }
370
+
371
+ function lib$es6$promise$$internal$$subscribe(parent, child, onFulfillment, onRejection) {
372
+ var subscribers = parent._subscribers;
373
+ var length = subscribers.length;
374
+
375
+ parent._onerror = null;
376
+
377
+ subscribers[length] = child;
378
+ subscribers[length + lib$es6$promise$$internal$$FULFILLED] = onFulfillment;
379
+ subscribers[length + lib$es6$promise$$internal$$REJECTED] = onRejection;
380
+
381
+ if (length === 0 && parent._state) {
382
+ lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publish, parent);
383
+ }
384
+ }
385
+
386
+ function lib$es6$promise$$internal$$publish(promise) {
387
+ var subscribers = promise._subscribers;
388
+ var settled = promise._state;
389
+
390
+ if (subscribers.length === 0) { return; }
391
+
392
+ var child, callback, detail = promise._result;
393
+
394
+ for (var i = 0; i < subscribers.length; i += 3) {
395
+ child = subscribers[i];
396
+ callback = subscribers[i + settled];
397
+
398
+ if (child) {
399
+ lib$es6$promise$$internal$$invokeCallback(settled, child, callback, detail);
400
+ } else {
401
+ callback(detail);
402
+ }
403
+ }
404
+
405
+ promise._subscribers.length = 0;
406
+ }
407
+
408
+ function lib$es6$promise$$internal$$ErrorObject() {
409
+ this.error = null;
410
+ }
411
+
412
+ var lib$es6$promise$$internal$$TRY_CATCH_ERROR = new lib$es6$promise$$internal$$ErrorObject();
413
+
414
+ function lib$es6$promise$$internal$$tryCatch(callback, detail) {
415
+ try {
416
+ return callback(detail);
417
+ } catch(e) {
418
+ lib$es6$promise$$internal$$TRY_CATCH_ERROR.error = e;
419
+ return lib$es6$promise$$internal$$TRY_CATCH_ERROR;
420
+ }
421
+ }
422
+
423
+ function lib$es6$promise$$internal$$invokeCallback(settled, promise, callback, detail) {
424
+ var hasCallback = lib$es6$promise$utils$$isFunction(callback),
425
+ value, error, succeeded, failed;
426
+
427
+ if (hasCallback) {
428
+ value = lib$es6$promise$$internal$$tryCatch(callback, detail);
429
+
430
+ if (value === lib$es6$promise$$internal$$TRY_CATCH_ERROR) {
431
+ failed = true;
432
+ error = value.error;
433
+ value = null;
434
+ } else {
435
+ succeeded = true;
436
+ }
437
+
438
+ if (promise === value) {
439
+ lib$es6$promise$$internal$$reject(promise, lib$es6$promise$$internal$$cannotReturnOwn());
440
+ return;
441
+ }
442
+
443
+ } else {
444
+ value = detail;
445
+ succeeded = true;
446
+ }
447
+
448
+ if (promise._state !== lib$es6$promise$$internal$$PENDING) {
449
+ // noop
450
+ } else if (hasCallback && succeeded) {
451
+ lib$es6$promise$$internal$$resolve(promise, value);
452
+ } else if (failed) {
453
+ lib$es6$promise$$internal$$reject(promise, error);
454
+ } else if (settled === lib$es6$promise$$internal$$FULFILLED) {
455
+ lib$es6$promise$$internal$$fulfill(promise, value);
456
+ } else if (settled === lib$es6$promise$$internal$$REJECTED) {
457
+ lib$es6$promise$$internal$$reject(promise, value);
458
+ }
459
+ }
460
+
461
+ function lib$es6$promise$$internal$$initializePromise(promise, resolver) {
462
+ try {
463
+ resolver(function resolvePromise(value){
464
+ lib$es6$promise$$internal$$resolve(promise, value);
465
+ }, function rejectPromise(reason) {
466
+ lib$es6$promise$$internal$$reject(promise, reason);
467
+ });
468
+ } catch(e) {
469
+ lib$es6$promise$$internal$$reject(promise, e);
470
+ }
471
+ }
472
+
473
+ function lib$es6$promise$enumerator$$Enumerator(Constructor, input) {
474
+ var enumerator = this;
475
+
476
+ enumerator._instanceConstructor = Constructor;
477
+ enumerator.promise = new Constructor(lib$es6$promise$$internal$$noop);
478
+
479
+ if (enumerator._validateInput(input)) {
480
+ enumerator._input = input;
481
+ enumerator.length = input.length;
482
+ enumerator._remaining = input.length;
483
+
484
+ enumerator._init();
485
+
486
+ if (enumerator.length === 0) {
487
+ lib$es6$promise$$internal$$fulfill(enumerator.promise, enumerator._result);
488
+ } else {
489
+ enumerator.length = enumerator.length || 0;
490
+ enumerator._enumerate();
491
+ if (enumerator._remaining === 0) {
492
+ lib$es6$promise$$internal$$fulfill(enumerator.promise, enumerator._result);
493
+ }
494
+ }
495
+ } else {
496
+ lib$es6$promise$$internal$$reject(enumerator.promise, enumerator._validationError());
497
+ }
498
+ }
499
+
500
+ lib$es6$promise$enumerator$$Enumerator.prototype._validateInput = function(input) {
501
+ return lib$es6$promise$utils$$isArray(input);
502
+ };
503
+
504
+ lib$es6$promise$enumerator$$Enumerator.prototype._validationError = function() {
505
+ return new Error('Array Methods must be provided an Array');
506
+ };
507
+
508
+ lib$es6$promise$enumerator$$Enumerator.prototype._init = function() {
509
+ this._result = new Array(this.length);
510
+ };
511
+
512
+ var lib$es6$promise$enumerator$$default = lib$es6$promise$enumerator$$Enumerator;
513
+
514
+ lib$es6$promise$enumerator$$Enumerator.prototype._enumerate = function() {
515
+ var enumerator = this;
516
+
517
+ var length = enumerator.length;
518
+ var promise = enumerator.promise;
519
+ var input = enumerator._input;
520
+
521
+ for (var i = 0; promise._state === lib$es6$promise$$internal$$PENDING && i < length; i++) {
522
+ enumerator._eachEntry(input[i], i);
523
+ }
524
+ };
525
+
526
+ lib$es6$promise$enumerator$$Enumerator.prototype._eachEntry = function(entry, i) {
527
+ var enumerator = this;
528
+ var c = enumerator._instanceConstructor;
529
+
530
+ if (lib$es6$promise$utils$$isMaybeThenable(entry)) {
531
+ if (entry.constructor === c && entry._state !== lib$es6$promise$$internal$$PENDING) {
532
+ entry._onerror = null;
533
+ enumerator._settledAt(entry._state, i, entry._result);
534
+ } else {
535
+ enumerator._willSettleAt(c.resolve(entry), i);
536
+ }
537
+ } else {
538
+ enumerator._remaining--;
539
+ enumerator._result[i] = entry;
540
+ }
541
+ };
542
+
543
+ lib$es6$promise$enumerator$$Enumerator.prototype._settledAt = function(state, i, value) {
544
+ var enumerator = this;
545
+ var promise = enumerator.promise;
546
+
547
+ if (promise._state === lib$es6$promise$$internal$$PENDING) {
548
+ enumerator._remaining--;
549
+
550
+ if (state === lib$es6$promise$$internal$$REJECTED) {
551
+ lib$es6$promise$$internal$$reject(promise, value);
552
+ } else {
553
+ enumerator._result[i] = value;
554
+ }
555
+ }
556
+
557
+ if (enumerator._remaining === 0) {
558
+ lib$es6$promise$$internal$$fulfill(promise, enumerator._result);
559
+ }
560
+ };
561
+
562
+ lib$es6$promise$enumerator$$Enumerator.prototype._willSettleAt = function(promise, i) {
563
+ var enumerator = this;
564
+
565
+ lib$es6$promise$$internal$$subscribe(promise, undefined, function(value) {
566
+ enumerator._settledAt(lib$es6$promise$$internal$$FULFILLED, i, value);
567
+ }, function(reason) {
568
+ enumerator._settledAt(lib$es6$promise$$internal$$REJECTED, i, reason);
569
+ });
570
+ };
571
+ function lib$es6$promise$promise$all$$all(entries) {
572
+ return new lib$es6$promise$enumerator$$default(this, entries).promise;
573
+ }
574
+ var lib$es6$promise$promise$all$$default = lib$es6$promise$promise$all$$all;
575
+ function lib$es6$promise$promise$race$$race(entries) {
576
+ /*jshint validthis:true */
577
+ var Constructor = this;
578
+
579
+ var promise = new Constructor(lib$es6$promise$$internal$$noop);
580
+
581
+ if (!lib$es6$promise$utils$$isArray(entries)) {
582
+ lib$es6$promise$$internal$$reject(promise, new TypeError('You must pass an array to race.'));
583
+ return promise;
584
+ }
585
+
586
+ var length = entries.length;
587
+
588
+ function onFulfillment(value) {
589
+ lib$es6$promise$$internal$$resolve(promise, value);
590
+ }
591
+
592
+ function onRejection(reason) {
593
+ lib$es6$promise$$internal$$reject(promise, reason);
594
+ }
595
+
596
+ for (var i = 0; promise._state === lib$es6$promise$$internal$$PENDING && i < length; i++) {
597
+ lib$es6$promise$$internal$$subscribe(Constructor.resolve(entries[i]), undefined, onFulfillment, onRejection);
598
+ }
599
+
600
+ return promise;
601
+ }
602
+ var lib$es6$promise$promise$race$$default = lib$es6$promise$promise$race$$race;
603
+ function lib$es6$promise$promise$resolve$$resolve(object) {
604
+ /*jshint validthis:true */
605
+ var Constructor = this;
606
+
607
+ if (object && typeof object === 'object' && object.constructor === Constructor) {
608
+ return object;
609
+ }
610
+
611
+ var promise = new Constructor(lib$es6$promise$$internal$$noop);
612
+ lib$es6$promise$$internal$$resolve(promise, object);
613
+ return promise;
614
+ }
615
+ var lib$es6$promise$promise$resolve$$default = lib$es6$promise$promise$resolve$$resolve;
616
+ function lib$es6$promise$promise$reject$$reject(reason) {
617
+ /*jshint validthis:true */
618
+ var Constructor = this;
619
+ var promise = new Constructor(lib$es6$promise$$internal$$noop);
620
+ lib$es6$promise$$internal$$reject(promise, reason);
621
+ return promise;
622
+ }
623
+ var lib$es6$promise$promise$reject$$default = lib$es6$promise$promise$reject$$reject;
624
+
625
+ var lib$es6$promise$promise$$counter = 0;
626
+
627
+ function lib$es6$promise$promise$$needsResolver() {
628
+ throw new TypeError('You must pass a resolver function as the first argument to the promise constructor');
629
+ }
630
+
631
+ function lib$es6$promise$promise$$needsNew() {
632
+ throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.");
633
+ }
634
+
635
+ var lib$es6$promise$promise$$default = lib$es6$promise$promise$$Promise;
636
+ /**
637
+ Promise objects represent the eventual result of an asynchronous operation. The
638
+ primary way of interacting with a promise is through its `then` method, which
639
+ registers callbacks to receive either a promise's eventual value or the reason
640
+ why the promise cannot be fulfilled.
641
+
642
+ Terminology
643
+ -----------
644
+
645
+ - `promise` is an object or function with a `then` method whose behavior conforms to this specification.
646
+ - `thenable` is an object or function that defines a `then` method.
647
+ - `value` is any legal JavaScript value (including undefined, a thenable, or a promise).
648
+ - `exception` is a value that is thrown using the throw statement.
649
+ - `reason` is a value that indicates why a promise was rejected.
650
+ - `settled` the final resting state of a promise, fulfilled or rejected.
651
+
652
+ A promise can be in one of three states: pending, fulfilled, or rejected.
653
+
654
+ Promises that are fulfilled have a fulfillment value and are in the fulfilled
655
+ state. Promises that are rejected have a rejection reason and are in the
656
+ rejected state. A fulfillment value is never a thenable.
657
+
658
+ Promises can also be said to *resolve* a value. If this value is also a
659
+ promise, then the original promise's settled state will match the value's
660
+ settled state. So a promise that *resolves* a promise that rejects will
661
+ itself reject, and a promise that *resolves* a promise that fulfills will
662
+ itself fulfill.
663
+
664
+
665
+ Basic Usage:
666
+ ------------
667
+
668
+ ```js
669
+ var promise = new Promise(function(resolve, reject) {
670
+ // on success
671
+ resolve(value);
672
+
673
+ // on failure
674
+ reject(reason);
675
+ });
676
+
677
+ promise.then(function(value) {
678
+ // on fulfillment
679
+ }, function(reason) {
680
+ // on rejection
681
+ });
682
+ ```
683
+
684
+ Advanced Usage:
685
+ ---------------
686
+
687
+ Promises shine when abstracting away asynchronous interactions such as
688
+ `XMLHttpRequest`s.
689
+
690
+ ```js
691
+ function getJSON(url) {
692
+ return new Promise(function(resolve, reject){
693
+ var xhr = new XMLHttpRequest();
694
+
695
+ xhr.open('GET', url);
696
+ xhr.onreadystatechange = handler;
697
+ xhr.responseType = 'json';
698
+ xhr.setRequestHeader('Accept', 'application/json');
699
+ xhr.send();
700
+
701
+ function handler() {
702
+ if (this.readyState === this.DONE) {
703
+ if (this.status === 200) {
704
+ resolve(this.response);
705
+ } else {
706
+ reject(new Error('getJSON: `' + url + '` failed with status: [' + this.status + ']'));
707
+ }
708
+ }
709
+ };
710
+ });
711
+ }
712
+
713
+ getJSON('/posts.json').then(function(json) {
714
+ // on fulfillment
715
+ }, function(reason) {
716
+ // on rejection
717
+ });
718
+ ```
719
+
720
+ Unlike callbacks, promises are great composable primitives.
721
+
722
+ ```js
723
+ Promise.all([
724
+ getJSON('/posts'),
725
+ getJSON('/comments')
726
+ ]).then(function(values){
727
+ values[0] // => postsJSON
728
+ values[1] // => commentsJSON
729
+
730
+ return values;
731
+ });
732
+ ```
733
+
734
+ @class Promise
735
+ @param {function} resolver
736
+ Useful for tooling.
737
+ @constructor
738
+ */
739
+ function lib$es6$promise$promise$$Promise(resolver) {
740
+ this._id = lib$es6$promise$promise$$counter++;
741
+ this._state = undefined;
742
+ this._result = undefined;
743
+ this._subscribers = [];
744
+
745
+ if (lib$es6$promise$$internal$$noop !== resolver) {
746
+ if (!lib$es6$promise$utils$$isFunction(resolver)) {
747
+ lib$es6$promise$promise$$needsResolver();
748
+ }
749
+
750
+ if (!(this instanceof lib$es6$promise$promise$$Promise)) {
751
+ lib$es6$promise$promise$$needsNew();
752
+ }
753
+
754
+ lib$es6$promise$$internal$$initializePromise(this, resolver);
755
+ }
756
+ }
757
+
758
+ lib$es6$promise$promise$$Promise.all = lib$es6$promise$promise$all$$default;
759
+ lib$es6$promise$promise$$Promise.race = lib$es6$promise$promise$race$$default;
760
+ lib$es6$promise$promise$$Promise.resolve = lib$es6$promise$promise$resolve$$default;
761
+ lib$es6$promise$promise$$Promise.reject = lib$es6$promise$promise$reject$$default;
762
+ lib$es6$promise$promise$$Promise._setScheduler = lib$es6$promise$asap$$setScheduler;
763
+ lib$es6$promise$promise$$Promise._setAsap = lib$es6$promise$asap$$setAsap;
764
+ lib$es6$promise$promise$$Promise._asap = lib$es6$promise$asap$$asap;
765
+
766
+ lib$es6$promise$promise$$Promise.prototype = {
767
+ constructor: lib$es6$promise$promise$$Promise,
768
+
769
+ /**
770
+ The primary way of interacting with a promise is through its `then` method,
771
+ which registers callbacks to receive either a promise's eventual value or the
772
+ reason why the promise cannot be fulfilled.
773
+
774
+ ```js
775
+ findUser().then(function(user){
776
+ // user is available
777
+ }, function(reason){
778
+ // user is unavailable, and you are given the reason why
779
+ });
780
+ ```
781
+
782
+ Chaining
783
+ --------
784
+
785
+ The return value of `then` is itself a promise. This second, 'downstream'
786
+ promise is resolved with the return value of the first promise's fulfillment
787
+ or rejection handler, or rejected if the handler throws an exception.
788
+
789
+ ```js
790
+ findUser().then(function (user) {
791
+ return user.name;
792
+ }, function (reason) {
793
+ return 'default name';
794
+ }).then(function (userName) {
795
+ // If `findUser` fulfilled, `userName` will be the user's name, otherwise it
796
+ // will be `'default name'`
797
+ });
798
+
799
+ findUser().then(function (user) {
800
+ throw new Error('Found user, but still unhappy');
801
+ }, function (reason) {
802
+ throw new Error('`findUser` rejected and we're unhappy');
803
+ }).then(function (value) {
804
+ // never reached
805
+ }, function (reason) {
806
+ // if `findUser` fulfilled, `reason` will be 'Found user, but still unhappy'.
807
+ // If `findUser` rejected, `reason` will be '`findUser` rejected and we're unhappy'.
808
+ });
809
+ ```
810
+ If the downstream promise does not specify a rejection handler, rejection reasons will be propagated further downstream.
811
+
812
+ ```js
813
+ findUser().then(function (user) {
814
+ throw new PedagogicalException('Upstream error');
815
+ }).then(function (value) {
816
+ // never reached
817
+ }).then(function (value) {
818
+ // never reached
819
+ }, function (reason) {
820
+ // The `PedgagocialException` is propagated all the way down to here
821
+ });
822
+ ```
823
+
824
+ Assimilation
825
+ ------------
826
+
827
+ Sometimes the value you want to propagate to a downstream promise can only be
828
+ retrieved asynchronously. This can be achieved by returning a promise in the
829
+ fulfillment or rejection handler. The downstream promise will then be pending
830
+ until the returned promise is settled. This is called *assimilation*.
831
+
832
+ ```js
833
+ findUser().then(function (user) {
834
+ return findCommentsByAuthor(user);
835
+ }).then(function (comments) {
836
+ // The user's comments are now available
837
+ });
838
+ ```
839
+
840
+ If the assimliated promise rejects, then the downstream promise will also reject.
841
+
842
+ ```js
843
+ findUser().then(function (user) {
844
+ return findCommentsByAuthor(user);
845
+ }).then(function (comments) {
846
+ // If `findCommentsByAuthor` fulfills, we'll have the value here
847
+ }, function (reason) {
848
+ // If `findCommentsByAuthor` rejects, we'll have the reason here
849
+ });
850
+ ```
851
+
852
+ Simple Example
853
+ --------------
854
+
855
+ Synchronous Example
856
+
857
+ ```javascript
858
+ var result;
859
+
860
+ try {
861
+ result = findResult();
862
+ // success
863
+ } catch(reason) {
864
+ // failure
865
+ }
866
+ ```
867
+
868
+ Errback Example
869
+
870
+ ```js
871
+ findResult(function(result, err){
872
+ if (err) {
873
+ // failure
874
+ } else {
875
+ // success
876
+ }
877
+ });
878
+ ```
879
+
880
+ Promise Example;
881
+
882
+ ```javascript
883
+ findResult().then(function(result){
884
+ // success
885
+ }, function(reason){
886
+ // failure
887
+ });
888
+ ```
889
+
890
+ Advanced Example
891
+ --------------
892
+
893
+ Synchronous Example
894
+
895
+ ```javascript
896
+ var author, books;
897
+
898
+ try {
899
+ author = findAuthor();
900
+ books = findBooksByAuthor(author);
901
+ // success
902
+ } catch(reason) {
903
+ // failure
904
+ }
905
+ ```
906
+
907
+ Errback Example
908
+
909
+ ```js
910
+
911
+ function foundBooks(books) {
912
+
913
+ }
914
+
915
+ function failure(reason) {
916
+
917
+ }
918
+
919
+ findAuthor(function(author, err){
920
+ if (err) {
921
+ failure(err);
922
+ // failure
923
+ } else {
924
+ try {
925
+ findBoooksByAuthor(author, function(books, err) {
926
+ if (err) {
927
+ failure(err);
928
+ } else {
929
+ try {
930
+ foundBooks(books);
931
+ } catch(reason) {
932
+ failure(reason);
933
+ }
934
+ }
935
+ });
936
+ } catch(error) {
937
+ failure(err);
938
+ }
939
+ // success
940
+ }
941
+ });
942
+ ```
943
+
944
+ Promise Example;
945
+
946
+ ```javascript
947
+ findAuthor().
948
+ then(findBooksByAuthor).
949
+ then(function(books){
950
+ // found books
951
+ }).catch(function(reason){
952
+ // something went wrong
953
+ });
954
+ ```
955
+
956
+ @method then
957
+ @param {Function} onFulfilled
958
+ @param {Function} onRejected
959
+ Useful for tooling.
960
+ @return {Promise}
961
+ */
962
+ then: function(onFulfillment, onRejection) {
963
+ var parent = this;
964
+ var state = parent._state;
965
+
966
+ if (state === lib$es6$promise$$internal$$FULFILLED && !onFulfillment || state === lib$es6$promise$$internal$$REJECTED && !onRejection) {
967
+ return this;
968
+ }
969
+
970
+ var child = new this.constructor(lib$es6$promise$$internal$$noop);
971
+ var result = parent._result;
972
+
973
+ if (state) {
974
+ var callback = arguments[state - 1];
975
+ lib$es6$promise$asap$$asap(function(){
976
+ lib$es6$promise$$internal$$invokeCallback(state, child, callback, result);
977
+ });
978
+ } else {
979
+ lib$es6$promise$$internal$$subscribe(parent, child, onFulfillment, onRejection);
980
+ }
981
+
982
+ return child;
983
+ },
984
+
985
+ /**
986
+ `catch` is simply sugar for `then(undefined, onRejection)` which makes it the same
987
+ as the catch block of a try/catch statement.
988
+
989
+ ```js
990
+ function findAuthor(){
991
+ throw new Error('couldn't find that author');
992
+ }
993
+
994
+ // synchronous
995
+ try {
996
+ findAuthor();
997
+ } catch(reason) {
998
+ // something went wrong
999
+ }
1000
+
1001
+ // async with promises
1002
+ findAuthor().catch(function(reason){
1003
+ // something went wrong
1004
+ });
1005
+ ```
1006
+
1007
+ @method catch
1008
+ @param {Function} onRejection
1009
+ Useful for tooling.
1010
+ @return {Promise}
1011
+ */
1012
+ 'catch': function(onRejection) {
1013
+ return this.then(null, onRejection);
1014
+ }
1015
+ };
1016
+ function lib$es6$promise$polyfill$$polyfill() {
1017
+ var local;
1018
+
1019
+ if (typeof global !== 'undefined') {
1020
+ local = global;
1021
+ } else if (typeof self !== 'undefined') {
1022
+ local = self;
1023
+ } else {
1024
+ try {
1025
+ local = Function('return this')();
1026
+ } catch (e) {
1027
+ throw new Error('polyfill failed because global object is unavailable in this environment');
1028
+ }
1029
+ }
1030
+
1031
+ var P = local.Promise;
1032
+
1033
+ if (P && Object.prototype.toString.call(P.resolve()) === '[object Promise]' && !P.cast) {
1034
+ return;
1035
+ }
1036
+
1037
+ local.Promise = lib$es6$promise$promise$$default;
1038
+ }
1039
+ var lib$es6$promise$polyfill$$default = lib$es6$promise$polyfill$$polyfill;
1040
+
1041
+ var lib$es6$promise$umd$$ES6Promise = {
1042
+ 'Promise': lib$es6$promise$promise$$default,
1043
+ 'polyfill': lib$es6$promise$polyfill$$default
1044
+ };
1045
+
1046
+ /* global define:true module:true window: true */
1047
+ if (typeof define === 'function' && define['amd']) {
1048
+ define(function() { return lib$es6$promise$umd$$ES6Promise; });
1049
+ } else if (typeof module !== 'undefined' && module['exports']) {
1050
+ module['exports'] = lib$es6$promise$umd$$ES6Promise;
1051
+ } else if (typeof this !== 'undefined') {
1052
+ this['ES6Promise'] = lib$es6$promise$umd$$ES6Promise;
1053
+ }
1054
+
1055
+ lib$es6$promise$polyfill$$default();
1056
+ }).call(this);
1057
+
1058
+
1059
+ }).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
1060
+ },{"_process":1}],3:[function(require,module,exports){
1061
+ /*!
1062
+ * EventEmitter v4.2.11 - git.io/ee
1063
+ * Unlicense - http://unlicense.org/
1064
+ * Oliver Caldwell - http://oli.me.uk/
1065
+ * @preserve
1066
+ */
1067
+
1068
+ ;(function () {
1069
+ 'use strict';
1070
+
1071
+ /**
1072
+ * Class for managing events.
1073
+ * Can be extended to provide event functionality in other classes.
1074
+ *
1075
+ * @class EventEmitter Manages event registering and emitting.
1076
+ */
1077
+ function EventEmitter() {}
1078
+
1079
+ // Shortcuts to improve speed and size
1080
+ var proto = EventEmitter.prototype;
1081
+ var exports = this;
1082
+ var originalGlobalValue = exports.EventEmitter;
1083
+
1084
+ /**
1085
+ * Finds the index of the listener for the event in its storage array.
1086
+ *
1087
+ * @param {Function[]} listeners Array of listeners to search through.
1088
+ * @param {Function} listener Method to look for.
1089
+ * @return {Number} Index of the specified listener, -1 if not found
1090
+ * @api private
1091
+ */
1092
+ function indexOfListener(listeners, listener) {
1093
+ var i = listeners.length;
1094
+ while (i--) {
1095
+ if (listeners[i].listener === listener) {
1096
+ return i;
1097
+ }
1098
+ }
1099
+
1100
+ return -1;
1101
+ }
1102
+
1103
+ /**
1104
+ * Alias a method while keeping the context correct, to allow for overwriting of target method.
1105
+ *
1106
+ * @param {String} name The name of the target method.
1107
+ * @return {Function} The aliased method
1108
+ * @api private
1109
+ */
1110
+ function alias(name) {
1111
+ return function aliasClosure() {
1112
+ return this[name].apply(this, arguments);
1113
+ };
1114
+ }
1115
+
1116
+ /**
1117
+ * Returns the listener array for the specified event.
1118
+ * Will initialise the event object and listener arrays if required.
1119
+ * Will return an object if you use a regex search. The object contains keys for each matched event. So /ba[rz]/ might return an object containing bar and baz. But only if you have either defined them with defineEvent or added some listeners to them.
1120
+ * Each property in the object response is an array of listener functions.
1121
+ *
1122
+ * @param {String|RegExp} evt Name of the event to return the listeners from.
1123
+ * @return {Function[]|Object} All listener functions for the event.
1124
+ */
1125
+ proto.getListeners = function getListeners(evt) {
1126
+ var events = this._getEvents();
1127
+ var response;
1128
+ var key;
1129
+
1130
+ // Return a concatenated array of all matching events if
1131
+ // the selector is a regular expression.
1132
+ if (evt instanceof RegExp) {
1133
+ response = {};
1134
+ for (key in events) {
1135
+ if (events.hasOwnProperty(key) && evt.test(key)) {
1136
+ response[key] = events[key];
1137
+ }
1138
+ }
1139
+ }
1140
+ else {
1141
+ response = events[evt] || (events[evt] = []);
1142
+ }
1143
+
1144
+ return response;
1145
+ };
1146
+
1147
+ /**
1148
+ * Takes a list of listener objects and flattens it into a list of listener functions.
1149
+ *
1150
+ * @param {Object[]} listeners Raw listener objects.
1151
+ * @return {Function[]} Just the listener functions.
1152
+ */
1153
+ proto.flattenListeners = function flattenListeners(listeners) {
1154
+ var flatListeners = [];
1155
+ var i;
1156
+
1157
+ for (i = 0; i < listeners.length; i += 1) {
1158
+ flatListeners.push(listeners[i].listener);
1159
+ }
1160
+
1161
+ return flatListeners;
1162
+ };
1163
+
1164
+ /**
1165
+ * Fetches the requested listeners via getListeners but will always return the results inside an object. This is mainly for internal use but others may find it useful.
1166
+ *
1167
+ * @param {String|RegExp} evt Name of the event to return the listeners from.
1168
+ * @return {Object} All listener functions for an event in an object.
1169
+ */
1170
+ proto.getListenersAsObject = function getListenersAsObject(evt) {
1171
+ var listeners = this.getListeners(evt);
1172
+ var response;
1173
+
1174
+ if (listeners instanceof Array) {
1175
+ response = {};
1176
+ response[evt] = listeners;
1177
+ }
1178
+
1179
+ return response || listeners;
1180
+ };
1181
+
1182
+ /**
1183
+ * Adds a listener function to the specified event.
1184
+ * The listener will not be added if it is a duplicate.
1185
+ * If the listener returns true then it will be removed after it is called.
1186
+ * If you pass a regular expression as the event name then the listener will be added to all events that match it.
1187
+ *
1188
+ * @param {String|RegExp} evt Name of the event to attach the listener to.
1189
+ * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling.
1190
+ * @return {Object} Current instance of EventEmitter for chaining.
1191
+ */
1192
+ proto.addListener = function addListener(evt, listener) {
1193
+ var listeners = this.getListenersAsObject(evt);
1194
+ var listenerIsWrapped = typeof listener === 'object';
1195
+ var key;
1196
+
1197
+ for (key in listeners) {
1198
+ if (listeners.hasOwnProperty(key) && indexOfListener(listeners[key], listener) === -1) {
1199
+ listeners[key].push(listenerIsWrapped ? listener : {
1200
+ listener: listener,
1201
+ once: false
1202
+ });
1203
+ }
1204
+ }
1205
+
1206
+ return this;
1207
+ };
1208
+
1209
+ /**
1210
+ * Alias of addListener
1211
+ */
1212
+ proto.on = alias('addListener');
1213
+
1214
+ /**
1215
+ * Semi-alias of addListener. It will add a listener that will be
1216
+ * automatically removed after its first execution.
1217
+ *
1218
+ * @param {String|RegExp} evt Name of the event to attach the listener to.
1219
+ * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling.
1220
+ * @return {Object} Current instance of EventEmitter for chaining.
1221
+ */
1222
+ proto.addOnceListener = function addOnceListener(evt, listener) {
1223
+ return this.addListener(evt, {
1224
+ listener: listener,
1225
+ once: true
1226
+ });
1227
+ };
1228
+
1229
+ /**
1230
+ * Alias of addOnceListener.
1231
+ */
1232
+ proto.once = alias('addOnceListener');
1233
+
1234
+ /**
1235
+ * Defines an event name. This is required if you want to use a regex to add a listener to multiple events at once. If you don't do this then how do you expect it to know what event to add to? Should it just add to every possible match for a regex? No. That is scary and bad.
1236
+ * You need to tell it what event names should be matched by a regex.
1237
+ *
1238
+ * @param {String} evt Name of the event to create.
1239
+ * @return {Object} Current instance of EventEmitter for chaining.
1240
+ */
1241
+ proto.defineEvent = function defineEvent(evt) {
1242
+ this.getListeners(evt);
1243
+ return this;
1244
+ };
1245
+
1246
+ /**
1247
+ * Uses defineEvent to define multiple events.
1248
+ *
1249
+ * @param {String[]} evts An array of event names to define.
1250
+ * @return {Object} Current instance of EventEmitter for chaining.
1251
+ */
1252
+ proto.defineEvents = function defineEvents(evts) {
1253
+ for (var i = 0; i < evts.length; i += 1) {
1254
+ this.defineEvent(evts[i]);
1255
+ }
1256
+ return this;
1257
+ };
1258
+
1259
+ /**
1260
+ * Removes a listener function from the specified event.
1261
+ * When passed a regular expression as the event name, it will remove the listener from all events that match it.
1262
+ *
1263
+ * @param {String|RegExp} evt Name of the event to remove the listener from.
1264
+ * @param {Function} listener Method to remove from the event.
1265
+ * @return {Object} Current instance of EventEmitter for chaining.
1266
+ */
1267
+ proto.removeListener = function removeListener(evt, listener) {
1268
+ var listeners = this.getListenersAsObject(evt);
1269
+ var index;
1270
+ var key;
1271
+
1272
+ for (key in listeners) {
1273
+ if (listeners.hasOwnProperty(key)) {
1274
+ index = indexOfListener(listeners[key], listener);
1275
+
1276
+ if (index !== -1) {
1277
+ listeners[key].splice(index, 1);
1278
+ }
1279
+ }
1280
+ }
1281
+
1282
+ return this;
1283
+ };
1284
+
1285
+ /**
1286
+ * Alias of removeListener
1287
+ */
1288
+ proto.off = alias('removeListener');
1289
+
1290
+ /**
1291
+ * Adds listeners in bulk using the manipulateListeners method.
1292
+ * If you pass an object as the second argument you can add to multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. You can also pass it an event name and an array of listeners to be added.
1293
+ * You can also pass it a regular expression to add the array of listeners to all events that match it.
1294
+ * Yeah, this function does quite a bit. That's probably a bad thing.
1295
+ *
1296
+ * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add to multiple events at once.
1297
+ * @param {Function[]} [listeners] An optional array of listener functions to add.
1298
+ * @return {Object} Current instance of EventEmitter for chaining.
1299
+ */
1300
+ proto.addListeners = function addListeners(evt, listeners) {
1301
+ // Pass through to manipulateListeners
1302
+ return this.manipulateListeners(false, evt, listeners);
1303
+ };
1304
+
1305
+ /**
1306
+ * Removes listeners in bulk using the manipulateListeners method.
1307
+ * If you pass an object as the second argument you can remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays.
1308
+ * You can also pass it an event name and an array of listeners to be removed.
1309
+ * You can also pass it a regular expression to remove the listeners from all events that match it.
1310
+ *
1311
+ * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to remove from multiple events at once.
1312
+ * @param {Function[]} [listeners] An optional array of listener functions to remove.
1313
+ * @return {Object} Current instance of EventEmitter for chaining.
1314
+ */
1315
+ proto.removeListeners = function removeListeners(evt, listeners) {
1316
+ // Pass through to manipulateListeners
1317
+ return this.manipulateListeners(true, evt, listeners);
1318
+ };
1319
+
1320
+ /**
1321
+ * Edits listeners in bulk. The addListeners and removeListeners methods both use this to do their job. You should really use those instead, this is a little lower level.
1322
+ * The first argument will determine if the listeners are removed (true) or added (false).
1323
+ * If you pass an object as the second argument you can add/remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays.
1324
+ * You can also pass it an event name and an array of listeners to be added/removed.
1325
+ * You can also pass it a regular expression to manipulate the listeners of all events that match it.
1326
+ *
1327
+ * @param {Boolean} remove True if you want to remove listeners, false if you want to add.
1328
+ * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add/remove from multiple events at once.
1329
+ * @param {Function[]} [listeners] An optional array of listener functions to add/remove.
1330
+ * @return {Object} Current instance of EventEmitter for chaining.
1331
+ */
1332
+ proto.manipulateListeners = function manipulateListeners(remove, evt, listeners) {
1333
+ var i;
1334
+ var value;
1335
+ var single = remove ? this.removeListener : this.addListener;
1336
+ var multiple = remove ? this.removeListeners : this.addListeners;
1337
+
1338
+ // If evt is an object then pass each of its properties to this method
1339
+ if (typeof evt === 'object' && !(evt instanceof RegExp)) {
1340
+ for (i in evt) {
1341
+ if (evt.hasOwnProperty(i) && (value = evt[i])) {
1342
+ // Pass the single listener straight through to the singular method
1343
+ if (typeof value === 'function') {
1344
+ single.call(this, i, value);
1345
+ }
1346
+ else {
1347
+ // Otherwise pass back to the multiple function
1348
+ multiple.call(this, i, value);
1349
+ }
1350
+ }
1351
+ }
1352
+ }
1353
+ else {
1354
+ // So evt must be a string
1355
+ // And listeners must be an array of listeners
1356
+ // Loop over it and pass each one to the multiple method
1357
+ i = listeners.length;
1358
+ while (i--) {
1359
+ single.call(this, evt, listeners[i]);
1360
+ }
1361
+ }
1362
+
1363
+ return this;
1364
+ };
1365
+
1366
+ /**
1367
+ * Removes all listeners from a specified event.
1368
+ * If you do not specify an event then all listeners will be removed.
1369
+ * That means every event will be emptied.
1370
+ * You can also pass a regex to remove all events that match it.
1371
+ *
1372
+ * @param {String|RegExp} [evt] Optional name of the event to remove all listeners for. Will remove from every event if not passed.
1373
+ * @return {Object} Current instance of EventEmitter for chaining.
1374
+ */
1375
+ proto.removeEvent = function removeEvent(evt) {
1376
+ var type = typeof evt;
1377
+ var events = this._getEvents();
1378
+ var key;
1379
+
1380
+ // Remove different things depending on the state of evt
1381
+ if (type === 'string') {
1382
+ // Remove all listeners for the specified event
1383
+ delete events[evt];
1384
+ }
1385
+ else if (evt instanceof RegExp) {
1386
+ // Remove all events matching the regex.
1387
+ for (key in events) {
1388
+ if (events.hasOwnProperty(key) && evt.test(key)) {
1389
+ delete events[key];
1390
+ }
1391
+ }
1392
+ }
1393
+ else {
1394
+ // Remove all listeners in all events
1395
+ delete this._events;
1396
+ }
1397
+
1398
+ return this;
1399
+ };
1400
+
1401
+ /**
1402
+ * Alias of removeEvent.
1403
+ *
1404
+ * Added to mirror the node API.
1405
+ */
1406
+ proto.removeAllListeners = alias('removeEvent');
1407
+
1408
+ /**
1409
+ * Emits an event of your choice.
1410
+ * When emitted, every listener attached to that event will be executed.
1411
+ * If you pass the optional argument array then those arguments will be passed to every listener upon execution.
1412
+ * Because it uses `apply`, your array of arguments will be passed as if you wrote them out separately.
1413
+ * So they will not arrive within the array on the other side, they will be separate.
1414
+ * You can also pass a regular expression to emit to all events that match it.
1415
+ *
1416
+ * @param {String|RegExp} evt Name of the event to emit and execute listeners for.
1417
+ * @param {Array} [args] Optional array of arguments to be passed to each listener.
1418
+ * @return {Object} Current instance of EventEmitter for chaining.
1419
+ */
1420
+ proto.emitEvent = function emitEvent(evt, args) {
1421
+ var listenersMap = this.getListenersAsObject(evt);
1422
+ var listeners;
1423
+ var listener;
1424
+ var i;
1425
+ var key;
1426
+ var response;
1427
+
1428
+ for (key in listenersMap) {
1429
+ if (listenersMap.hasOwnProperty(key)) {
1430
+ listeners = listenersMap[key].slice(0);
1431
+ i = listeners.length;
1432
+
1433
+ while (i--) {
1434
+ // If the listener returns true then it shall be removed from the event
1435
+ // The function is executed either with a basic call or an apply if there is an args array
1436
+ listener = listeners[i];
1437
+
1438
+ if (listener.once === true) {
1439
+ this.removeListener(evt, listener.listener);
1440
+ }
1441
+
1442
+ response = listener.listener.apply(this, args || []);
1443
+
1444
+ if (response === this._getOnceReturnValue()) {
1445
+ this.removeListener(evt, listener.listener);
1446
+ }
1447
+ }
1448
+ }
1449
+ }
1450
+
1451
+ return this;
1452
+ };
1453
+
1454
+ /**
1455
+ * Alias of emitEvent
1456
+ */
1457
+ proto.trigger = alias('emitEvent');
1458
+
1459
+ /**
1460
+ * Subtly different from emitEvent in that it will pass its arguments on to the listeners, as opposed to taking a single array of arguments to pass on.
1461
+ * As with emitEvent, you can pass a regex in place of the event name to emit to all events that match it.
1462
+ *
1463
+ * @param {String|RegExp} evt Name of the event to emit and execute listeners for.
1464
+ * @param {...*} Optional additional arguments to be passed to each listener.
1465
+ * @return {Object} Current instance of EventEmitter for chaining.
1466
+ */
1467
+ proto.emit = function emit(evt) {
1468
+ var args = Array.prototype.slice.call(arguments, 1);
1469
+ return this.emitEvent(evt, args);
1470
+ };
1471
+
1472
+ /**
1473
+ * Sets the current value to check against when executing listeners. If a
1474
+ * listeners return value matches the one set here then it will be removed
1475
+ * after execution. This value defaults to true.
1476
+ *
1477
+ * @param {*} value The new value to check for when executing listeners.
1478
+ * @return {Object} Current instance of EventEmitter for chaining.
1479
+ */
1480
+ proto.setOnceReturnValue = function setOnceReturnValue(value) {
1481
+ this._onceReturnValue = value;
1482
+ return this;
1483
+ };
1484
+
1485
+ /**
1486
+ * Fetches the current value to check against when executing listeners. If
1487
+ * the listeners return value matches this one then it should be removed
1488
+ * automatically. It will return true by default.
1489
+ *
1490
+ * @return {*|Boolean} The current value to check for or the default, true.
1491
+ * @api private
1492
+ */
1493
+ proto._getOnceReturnValue = function _getOnceReturnValue() {
1494
+ if (this.hasOwnProperty('_onceReturnValue')) {
1495
+ return this._onceReturnValue;
1496
+ }
1497
+ else {
1498
+ return true;
1499
+ }
1500
+ };
1501
+
1502
+ /**
1503
+ * Fetches the events object and creates one if required.
1504
+ *
1505
+ * @return {Object} The events storage object.
1506
+ * @api private
1507
+ */
1508
+ proto._getEvents = function _getEvents() {
1509
+ return this._events || (this._events = {});
1510
+ };
1511
+
1512
+ /**
1513
+ * Reverts the global {@link EventEmitter} to its previous value and returns a reference to this version.
1514
+ *
1515
+ * @return {Function} Non conflicting EventEmitter class.
1516
+ */
1517
+ EventEmitter.noConflict = function noConflict() {
1518
+ exports.EventEmitter = originalGlobalValue;
1519
+ return EventEmitter;
1520
+ };
1521
+
1522
+ // Expose the class either via AMD, CommonJS or the global object
1523
+ if (typeof define === 'function' && define.amd) {
1524
+ define(function () {
1525
+ return EventEmitter;
1526
+ });
1527
+ }
1528
+ else if (typeof module === 'object' && module.exports){
1529
+ module.exports = EventEmitter;
1530
+ }
1531
+ else {
1532
+ exports.EventEmitter = EventEmitter;
1533
+ }
1534
+ }.call(this));
1535
+
1536
+ },{}],4:[function(require,module,exports){
1537
+ (function (global){
1538
+ var Promise = require('es6-promise').Promise;
1539
+ var EventEmitter = require('wolfy87-eventemitter');
1540
+
1541
+ /**
1542
+ * Oliver Caldwell
1543
+ * http://oli.me.uk/2013/06/01/prototypical-inheritance-done-right/
1544
+ */
1545
+ if (!Object.create) {
1546
+ Object.create = (function(){
1547
+ function F(){}
1548
+ return function(o){
1549
+ if (arguments.length != 1) {
1550
+ throw new Error('Object.create implementation only accepts one parameter.');
1551
+ }
1552
+ F.prototype = o;
1553
+ return new F()
1554
+ }
1555
+ })()
1556
+ }
1557
+
1558
+ function extend(destination, source) {
1559
+ destination.prototype = Object.create(source.prototype);
1560
+ destination.prototype.constructor = destination;
1561
+ return source.prototype;
1562
+ }
1563
+
1564
+ if (!Array.prototype.indexOf) {
1565
+ Array.prototype.indexOf=function(r){if(null==this)throw new TypeError;var t,e,n=Object(this),a=n.length>>>0;if(0===a)return-1;if(t=0,arguments.length>1&&(t=Number(arguments[1]),t!=t?t=0:0!=t&&1/0!=t&&t!=-1/0&&(t=(t>0||-1)*Math.floor(Math.abs(t)))),t>=a)return-1;for(e=t>=0?t:Math.max(a-Math.abs(t),0);a>e;e++)if(e in n&&n[e]===r)return e;return-1};
1566
+ }
1567
+
1568
+ function fieldValue(object, name) {
1569
+ try {return object[name];} catch (x) {return undefined;}
1570
+ }
1571
+
1572
+ /**
1573
+ * Mixes in the given objects into the target object by copying the properties.
1574
+ * @param deep if the copy must be deep
1575
+ * @param target the target object
1576
+ * @param objects the objects whose properties are copied into the target
1577
+ */
1578
+ function mixin(deep, target, objects) {
1579
+ var result = target || {};
1580
+ for (var i = 2; i < arguments.length; ++i) { // Skip first 2 parameters (deep and target), and loop over the others
1581
+ var object = arguments[i];
1582
+ if (object === undefined || object === null) {
1583
+ continue;
1584
+ }
1585
+ for (var propName in object) {
1586
+ var prop = fieldValue(object, propName);
1587
+ var targ = fieldValue(result, propName);
1588
+ if (prop === target) {
1589
+ continue; // Avoid infinite loops
1590
+ }
1591
+ if (prop === undefined) {
1592
+ continue; // Do not mixin undefined values
1593
+ }
1594
+ if (deep && typeof prop === 'object' && prop !== null) {
1595
+ if (prop instanceof Array) {
1596
+ result[propName] = mixin(deep, targ instanceof Array ? targ : [], prop);
1597
+ } else {
1598
+ var source = typeof targ === 'object' && !(targ instanceof Array) ? targ : {};
1599
+ result[propName] = mixin(deep, source, prop);
1600
+ }
1601
+ } else {
1602
+ result[propName] = prop;
1603
+ }
1604
+ }
1605
+ }
1606
+ return result;
1607
+ }
1608
+
1609
+ function endsWith(value, suffix) {
1610
+ return value.indexOf(suffix, value.length - suffix.length) !== -1;
1611
+ }
1612
+
1613
+ function startsWith(value, prefix) {
1614
+ return value.lastIndexOf(prefix, 0) === 0;
1615
+ }
1616
+
1617
+ function stripSlash(value) {
1618
+ if (value.substring(value.length - 1) == "/") {
1619
+ value = value.substring(0, value.length - 1);
1620
+ }
1621
+ return value;
1622
+ }
1623
+
1624
+ function isString(value) {
1625
+ if (value === undefined || value === null) {
1626
+ return false;
1627
+ }
1628
+ return typeof value === 'string' || value instanceof String;
1629
+ }
1630
+
1631
+ function isFunction(value) {
1632
+ if (value === undefined || value === null) {
1633
+ return false;
1634
+ }
1635
+ return typeof value === 'function';
1636
+ }
1637
+
1638
+ function log(level, args) {
1639
+ if (global.console) {
1640
+ var logger = global.console[level];
1641
+ if (isFunction(logger)) {
1642
+ logger.apply(global.console, args);
1643
+ }
1644
+ }
1645
+ }
1646
+
1647
+ function backoff(step, min, max) {
1648
+ var jitter = 0.5 * Math.random();
1649
+ var interval = min * Math.pow(2, step+1);
1650
+ if (interval > max) {
1651
+ interval = max
1652
+ }
1653
+ return Math.floor((1-jitter) * interval);
1654
+ }
1655
+
1656
+ function errorExists(data) {
1657
+ return "error" in data && data.error !== null && data.error !== "";
1658
+ }
1659
+
1660
+ function Centrifuge(options) {
1661
+ this._sockjs = false;
1662
+ this._status = 'disconnected';
1663
+ this._reconnect = true;
1664
+ this._reconnecting = false;
1665
+ this._transport = null;
1666
+ this._transportName = null;
1667
+ this._messageId = 0;
1668
+ this._clientID = null;
1669
+ this._subs = {};
1670
+ this._lastMessageID = {};
1671
+ this._messages = [];
1672
+ this._isBatching = false;
1673
+ this._isAuthBatching = false;
1674
+ this._authChannels = {};
1675
+ this._refreshTimeout = null;
1676
+ this._retries = 0;
1677
+ this._callbacks = {};
1678
+ this._latency = null;
1679
+ this._latencyStart = null;
1680
+ this._config = {
1681
+ retry: 1000,
1682
+ maxRetry: 20000,
1683
+ timeout: 5000,
1684
+ info: "",
1685
+ resubscribe: true,
1686
+ debug: false,
1687
+ insecure: false,
1688
+ server: null,
1689
+ privateChannelPrefix: "$",
1690
+ transports: [
1691
+ 'websocket',
1692
+ 'xdr-streaming',
1693
+ 'xhr-streaming',
1694
+ 'eventsource',
1695
+ 'iframe-eventsource',
1696
+ 'iframe-htmlfile',
1697
+ 'xdr-polling',
1698
+ 'xhr-polling',
1699
+ 'iframe-xhr-polling',
1700
+ 'jsonp-polling'
1701
+ ],
1702
+ refreshEndpoint: "/centrifuge/refresh/",
1703
+ refreshHeaders: {},
1704
+ refreshParams: {},
1705
+ refreshTransport: "ajax",
1706
+ authEndpoint: "/centrifuge/auth/",
1707
+ authHeaders: {},
1708
+ authParams: {},
1709
+ authTransport: "ajax"
1710
+ };
1711
+ if (options) {
1712
+ this.configure(options);
1713
+ }
1714
+ }
1715
+
1716
+ extend(Centrifuge, EventEmitter);
1717
+
1718
+ Centrifuge._authCallbacks = {};
1719
+ Centrifuge._nextAuthCallbackID = 1;
1720
+
1721
+ var centrifugeProto = Centrifuge.prototype;
1722
+
1723
+ centrifugeProto._jsonp = function (url, params, headers, data, callback) {
1724
+ if (headers.length > 0) {
1725
+ this._log("Only AJAX request allows to send custom headers, it's not possible with JSONP.");
1726
+ }
1727
+ self._debug("sending JSONP request to", url);
1728
+
1729
+ var callbackName = Centrifuge._nextAuthCallbackID.toString();
1730
+ Centrifuge._nextAuthCallbackID++;
1731
+
1732
+ var document = global.document;
1733
+ var script = document.createElement("script");
1734
+ Centrifuge._authCallbacks[callbackName] = function (data) {
1735
+ callback(false, data);
1736
+ delete Centrifuge[callbackName];
1737
+ };
1738
+
1739
+ var query = "";
1740
+ for (var i in params) {
1741
+ if (query.length > 0) {
1742
+ query += "&";
1743
+ }
1744
+ query += encodeURIComponent(i) + "=" + encodeURIComponent(params[i]);
1745
+ }
1746
+
1747
+ var callback_name = "Centrifuge._authCallbacks['" + callbackName + "']";
1748
+ script.src = this._config.authEndpoint +
1749
+ '?callback=' + encodeURIComponent(callback_name) +
1750
+ '&data=' + encodeURIComponent(JSON.stringify(data)) +
1751
+ '&' + query;
1752
+
1753
+ var head = document.getElementsByTagName("head")[0] || document.documentElement;
1754
+ head.insertBefore(script, head.firstChild);
1755
+ };
1756
+
1757
+ centrifugeProto._ajax = function (url, params, headers, data, callback) {
1758
+ var self = this;
1759
+ self._debug("sending AJAX request to", url);
1760
+
1761
+ var xhr = (global.XMLHttpRequest ? new global.XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"));
1762
+
1763
+ var query = "";
1764
+ for (var i in params) {
1765
+ if (query.length > 0) {
1766
+ query += "&";
1767
+ }
1768
+ query += encodeURIComponent(i) + "=" + encodeURIComponent(params[i]);
1769
+ }
1770
+ if (query.length > 0) {
1771
+ query = "?" + query;
1772
+ }
1773
+ xhr.open("POST", url + query, true);
1774
+
1775
+ // add request headers
1776
+ xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
1777
+ xhr.setRequestHeader("Content-Type", "application/json");
1778
+ for (var headerName in headers) {
1779
+ xhr.setRequestHeader(headerName, headers[headerName]);
1780
+ }
1781
+
1782
+ xhr.onreadystatechange = function () {
1783
+ if (xhr.readyState === 4) {
1784
+ if (xhr.status === 200) {
1785
+ var data, parsed = false;
1786
+
1787
+ try {
1788
+ data = JSON.parse(xhr.responseText);
1789
+ parsed = true;
1790
+ } catch (e) {
1791
+ callback(true, 'JSON returned was invalid, yet status code was 200. Data was: ' + xhr.responseText);
1792
+ }
1793
+
1794
+ if (parsed) { // prevents double execution.
1795
+ callback(false, data);
1796
+ }
1797
+ } else {
1798
+ self._log("Couldn't get auth info from application", xhr.status);
1799
+ callback(true, xhr.status);
1800
+ }
1801
+ }
1802
+ };
1803
+
1804
+ setTimeout(function() {
1805
+ // method == 'get' ? self.xhr.send() : self.xhr.send(JSON.stringify(ops.data));
1806
+ xhr.send(JSON.stringify(data));
1807
+ }, 20);
1808
+ return xhr;
1809
+ };
1810
+
1811
+ centrifugeProto._log = function () {
1812
+ log("info", arguments);
1813
+ };
1814
+
1815
+ centrifugeProto._debug = function () {
1816
+ if (this._config.debug === true) {
1817
+ log("debug", arguments);
1818
+ }
1819
+ };
1820
+
1821
+ centrifugeProto._configure = function (configuration) {
1822
+ this._debug('Configuring centrifuge object with', configuration);
1823
+
1824
+ if (!configuration) {
1825
+ configuration = {};
1826
+ }
1827
+
1828
+ this._config = mixin(false, this._config, configuration);
1829
+
1830
+ if (!this._config.url) {
1831
+ throw 'Missing required configuration parameter \'url\' specifying server URL';
1832
+ }
1833
+
1834
+ if (!this._config.user && this._config.user !== '') {
1835
+ if (!this._config.insecure) {
1836
+ throw 'Missing required configuration parameter \'user\' specifying user\'s unique ID in your application';
1837
+ } else {
1838
+ this._debug("user not found but this is OK for insecure mode - anonymous access will be used");
1839
+ this._config.user = "";
1840
+ }
1841
+ }
1842
+
1843
+ if (!this._config.timestamp) {
1844
+ if (!this._config.insecure) {
1845
+ throw 'Missing required configuration parameter \'timestamp\'';
1846
+ } else {
1847
+ this._debug("token not found but this is OK for insecure mode");
1848
+ }
1849
+ }
1850
+
1851
+ if (!this._config.token) {
1852
+ if (!this._config.insecure) {
1853
+ throw 'Missing required configuration parameter \'token\' specifying the sign of authorization request';
1854
+ } else {
1855
+ this._debug("timestamp not found but this is OK for insecure mode");
1856
+ }
1857
+ }
1858
+
1859
+ this._config.url = stripSlash(this._config.url);
1860
+
1861
+ if (endsWith(this._config.url, 'connection')) {
1862
+ this._debug("client will connect to SockJS endpoint");
1863
+ if (typeof SockJS === 'undefined') {
1864
+ throw 'include SockJS client library before Centrifuge javascript client library or use raw Websocket connection endpoint';
1865
+ }
1866
+ this._sockjs = true;
1867
+ } else if (endsWith(this._config.url, 'connection/websocket')) {
1868
+ this._debug("client will connect to raw Websocket endpoint");
1869
+ this._config.url = this._config.url.replace("http://", "ws://");
1870
+ this._config.url = this._config.url.replace("https://", "wss://");
1871
+ } else {
1872
+ this._debug("client will detect connection endpoint itself");
1873
+ if (typeof SockJS === 'undefined') {
1874
+ this._debug("no SockJS found, client will connect to raw Websocket endpoint");
1875
+ this._config.url += "/connection/websocket";
1876
+ this._config.url = this._config.url.replace("http://", "ws://");
1877
+ this._config.url = this._config.url.replace("https://", "wss://");
1878
+ } else {
1879
+ this._debug("SockJS found, client will connect to SockJS endpoint");
1880
+ this._config.url += "/connection";
1881
+ this._sockjs = true;
1882
+ }
1883
+ }
1884
+ };
1885
+
1886
+ centrifugeProto._setStatus = function (newStatus) {
1887
+ if (this._status !== newStatus) {
1888
+ this._debug('Status', this._status, '->', newStatus);
1889
+ this._status = newStatus;
1890
+ }
1891
+ };
1892
+
1893
+ centrifugeProto._isDisconnected = function () {
1894
+ return this._status === 'disconnected';
1895
+ };
1896
+
1897
+ centrifugeProto._isConnecting = function() {
1898
+ return this._status === 'connecting';
1899
+ };
1900
+
1901
+ centrifugeProto._isConnected = function () {
1902
+ return this._status === 'connected';
1903
+ };
1904
+
1905
+ centrifugeProto._nextMessageId = function () {
1906
+ return ++this._messageId;
1907
+ };
1908
+
1909
+ centrifugeProto._resetRetry = function() {
1910
+ this._debug("reset retries count to 0");
1911
+ this._retries = 0;
1912
+ };
1913
+
1914
+ centrifugeProto._getRetryInterval = function() {
1915
+ var interval = backoff(this._retries, this._config.retry, this._config.maxRetry);
1916
+ this._retries += 1;
1917
+ return interval;
1918
+ };
1919
+
1920
+ centrifugeProto._clearConnectedState = function (reconnect) {
1921
+ self._clientID = null;
1922
+
1923
+ // fire errbacks of registered calls.
1924
+ for (var uid in this._callbacks) {
1925
+ var callbacks = this._callbacks[uid];
1926
+ var errback = callbacks["errback"];
1927
+ if (!errback) {
1928
+ continue;
1929
+ }
1930
+ errback(this._createErrorObject("disconnected", "retry"));
1931
+ }
1932
+ this._callbacks = {};
1933
+
1934
+ // fire unsubscribe events
1935
+ for (var channel in this._subs) {
1936
+ var sub = this._subs[channel];
1937
+ if (reconnect) {
1938
+ if (sub._isSuccess()) {
1939
+ sub._triggerUnsubscribe();
1940
+ }
1941
+ sub._setSubscribing();
1942
+ } else {
1943
+ sub._setUnsubscribed();
1944
+ }
1945
+ }
1946
+
1947
+ if (!this._config.resubscribe || !this._reconnect) {
1948
+ // completely clear connected state
1949
+ this._subs = {};
1950
+ }
1951
+ };
1952
+
1953
+ centrifugeProto._send = function (messages) {
1954
+ if (messages.length === 0) {
1955
+ return;
1956
+ }
1957
+ this._debug('Send', messages);
1958
+ this._transport.send(JSON.stringify(messages));
1959
+ };
1960
+
1961
+ centrifugeProto._connect = function (callback) {
1962
+
1963
+ if (this.isConnected()) {
1964
+ this._debug("connect called when already connected");
1965
+ return;
1966
+ }
1967
+
1968
+ this._setStatus('connecting');
1969
+ this._clientID = null;
1970
+ this._reconnect = true;
1971
+
1972
+ var self = this;
1973
+
1974
+ if (callback) {
1975
+ this.on('connect', callback);
1976
+ }
1977
+
1978
+ // detect transport to use - SockJS or raw Websocket
1979
+ if (this._sockjs === true) {
1980
+ var sockjsOptions = {
1981
+ "transports": this._config.transports
1982
+ };
1983
+ if (this._config.server !== null) {
1984
+ sockjsOptions['server'] = this._config.server;
1985
+ }
1986
+ this._transport = new SockJS(this._config.url, null, sockjsOptions);
1987
+ } else {
1988
+ this._transport = new WebSocket(this._config.url);
1989
+ }
1990
+
1991
+ this._transport.onopen = function () {
1992
+
1993
+ self._reconnecting = false;
1994
+
1995
+ if (self._sockjs) {
1996
+ self._transportName = self._transport._transport.transportName;
1997
+ } else {
1998
+ self._transportName = "raw-websocket";
1999
+ }
2000
+
2001
+ self._resetRetry();
2002
+
2003
+ if (!isString(self._config.user)) {
2004
+ self._log("user expected to be string");
2005
+ }
2006
+ if (!isString(self._config.info)) {
2007
+ self._log("info expected to be string");
2008
+ }
2009
+
2010
+ var msg = {
2011
+ 'method': 'connect',
2012
+ 'params': {
2013
+ 'user': self._config.user,
2014
+ 'info': self._config.info
2015
+ }
2016
+ };
2017
+
2018
+ if (!self._config.insecure) {
2019
+ // in insecure client mode we don't need timestamp and token.
2020
+ msg["params"]["timestamp"] = self._config.timestamp;
2021
+ msg["params"]["token"] = self._config.token;
2022
+ if (!isString(self._config.timestamp)) {
2023
+ self._log("timestamp expected to be string");
2024
+ }
2025
+ if (!isString(self._config.token)) {
2026
+ self._log("token expected to be string");
2027
+ }
2028
+ }
2029
+ self._addMessage(msg);
2030
+ self._latencyStart = new Date();
2031
+ };
2032
+
2033
+ this._transport.onerror = function (error) {
2034
+ self._debug("transport level error", error);
2035
+ };
2036
+
2037
+ this._transport.onclose = function (closeEvent) {
2038
+ var reason = "connection closed";
2039
+ if (closeEvent && "reason" in closeEvent && closeEvent["reason"]) {
2040
+ reason = closeEvent["reason"];
2041
+ }
2042
+ self._disconnect(reason, true, false);
2043
+ };
2044
+
2045
+ this._transport.onmessage = function (event) {
2046
+ var data;
2047
+ data = JSON.parse(event.data);
2048
+ self._debug('Received', data);
2049
+ self._receive(data);
2050
+ };
2051
+ };
2052
+
2053
+ centrifugeProto._disconnect = function (reason, shouldReconnect, closeTransport) {
2054
+ this._debug("disconnected:", reason, shouldReconnect);
2055
+ var reconnect = shouldReconnect || false;
2056
+ if (reconnect === false) {
2057
+ this._reconnect = false;
2058
+ }
2059
+
2060
+ this._clearConnectedState(shouldReconnect);
2061
+
2062
+ if (!this.isDisconnected()) {
2063
+ this._setStatus('disconnected');
2064
+ var disconnectContext = {
2065
+ "reason": reason,
2066
+ "reconnect": reconnect
2067
+ };
2068
+ if (this._reconnecting === false) {
2069
+ this.trigger('disconnect', [disconnectContext]);
2070
+ }
2071
+ }
2072
+
2073
+ if (closeTransport) {
2074
+ this._transport.close();
2075
+ }
2076
+
2077
+ var self = this;
2078
+ if (shouldReconnect === true && self._reconnect === true) {
2079
+ self._reconnecting = true;
2080
+ var interval = self._getRetryInterval();
2081
+ self._debug("reconnect after " + interval + " milliseconds");
2082
+ setTimeout(function () {
2083
+ if (self._reconnect === true) {
2084
+ self._connect.call(self);
2085
+ }
2086
+ }, interval);
2087
+ }
2088
+ };
2089
+
2090
+ centrifugeProto._refresh = function () {
2091
+ // ask web app for connection parameters - user ID,
2092
+ // timestamp, info and token
2093
+ var self = this;
2094
+ this._debug('refresh credentials');
2095
+
2096
+ var cb = function(error, data) {
2097
+ if (error === true) {
2098
+ // 403 or 500 - does not matter - if connection check activated then Centrifugo
2099
+ // will disconnect client eventually
2100
+ self._debug("error getting connect parameters", data);
2101
+ if (self._refreshTimeout) {
2102
+ clearTimeout(self._refreshTimeout);
2103
+ }
2104
+ self._refreshTimeout = setTimeout(function(){
2105
+ self._refresh.call(self);
2106
+ }, 3000);
2107
+ return;
2108
+ }
2109
+ self._config.user = data.user;
2110
+ self._config.timestamp = data.timestamp;
2111
+ self._config.info = data.info;
2112
+ self._config.token = data.token;
2113
+ if (self.isDisconnected()) {
2114
+ self._debug("credentials refreshed, connect from scratch");
2115
+ self._connect();
2116
+ } else {
2117
+ self._debug("send refreshed credentials");
2118
+ var msg = {
2119
+ "method": "refresh",
2120
+ "params": {
2121
+ 'user': self._config.user,
2122
+ 'timestamp': self._config.timestamp,
2123
+ 'info': self._config.info,
2124
+ 'token': self._config.token
2125
+ }
2126
+ };
2127
+ self._addMessage(msg);
2128
+ }
2129
+ };
2130
+
2131
+ var transport = this._config.refreshTransport.toLowerCase();
2132
+ if (transport === "ajax") {
2133
+ this._ajax(this._config.refreshEndpoint, this._config.refreshParams, this._config.refreshHeaders, {}, cb);
2134
+ } else if (transport === "jsonp") {
2135
+ this._jsonp(this._config.refreshEndpoint, this._config.refreshParams, this._config.refreshHeaders, {}, cb);
2136
+ } else {
2137
+ throw 'Unknown refresh transport ' + transport;
2138
+ }
2139
+ };
2140
+
2141
+ centrifugeProto._subscribe = function(sub) {
2142
+
2143
+ var channel = sub.channel;
2144
+
2145
+ if (!(channel in this._subs)) {
2146
+ this._subs[channel] = sub;
2147
+ }
2148
+
2149
+ if (!this.isConnected()) {
2150
+ // subscribe will be called later
2151
+ sub._setNew();
2152
+ return;
2153
+ }
2154
+
2155
+ sub._setSubscribing();
2156
+
2157
+ var msg = {
2158
+ "method": "subscribe",
2159
+ "params": {
2160
+ "channel": channel
2161
+ }
2162
+ };
2163
+
2164
+ // If channel name does not start with privateChannelPrefix - then we
2165
+ // can just send subscription message to Centrifuge. If channel name
2166
+ // starts with privateChannelPrefix - then this is a private channel
2167
+ // and we should ask web application backend for permission first.
2168
+ if (startsWith(channel, this._config.privateChannelPrefix)) {
2169
+ // private channel
2170
+ if (this._isAuthBatching) {
2171
+ this._authChannels[channel] = true;
2172
+ } else {
2173
+ this.startAuthBatching();
2174
+ this._subscribe(sub);
2175
+ this.stopAuthBatching();
2176
+ }
2177
+ } else {
2178
+ var recover = this._recover(channel);
2179
+ if (recover === true) {
2180
+ msg["params"]["recover"] = true;
2181
+ msg["params"]["last"] = this._getLastID(channel);
2182
+ }
2183
+ this._addMessage(msg);
2184
+ }
2185
+ };
2186
+
2187
+ centrifugeProto._unsubscribe = function(sub) {
2188
+ if (this.isConnected()) {
2189
+ // No need to unsubscribe in disconnected state - i.e. client already unsubscribed.
2190
+ var msg = {
2191
+ "method": "unsubscribe",
2192
+ "params": {
2193
+ "channel": sub.channel
2194
+ }
2195
+ };
2196
+ this._addMessage(msg);
2197
+ }
2198
+ };
2199
+
2200
+ centrifugeProto._getSub = function(channel) {
2201
+ var sub = this._subs[channel];
2202
+ if (!sub) {
2203
+ return null;
2204
+ }
2205
+ return sub;
2206
+ };
2207
+
2208
+ centrifugeProto._connectResponse = function (message) {
2209
+
2210
+ if (this.isConnected()) {
2211
+ return;
2212
+ }
2213
+
2214
+ if (!errorExists(message)) {
2215
+
2216
+ if (this._latencyStart !== null) {
2217
+ this._latency = (new Date()).getTime() - this._latencyStart.getTime();
2218
+ this._latencyStart = null;
2219
+ }
2220
+
2221
+ if (!message.body) {
2222
+ return;
2223
+ }
2224
+ if (message.body.expires) {
2225
+ var isExpired = message.body.expired;
2226
+ if (isExpired) {
2227
+ this._refresh();
2228
+ return;
2229
+ }
2230
+ }
2231
+ this._clientID = message.body.client;
2232
+ this._setStatus('connected');
2233
+
2234
+ if (this._refreshTimeout) {
2235
+ clearTimeout(this._refreshTimeout);
2236
+ }
2237
+ if (message.body.expires) {
2238
+ var self = this;
2239
+ this._refreshTimeout = setTimeout(function() {
2240
+ self._refresh.call(self);
2241
+ }, message.body.ttl * 1000);
2242
+ }
2243
+
2244
+ if (this._config.resubscribe) {
2245
+ this.startBatching();
2246
+ this.startAuthBatching();
2247
+ for (var channel in this._subs) {
2248
+ var sub = this._subs[channel];
2249
+ this._subscribe(sub);
2250
+ }
2251
+ this.stopAuthBatching();
2252
+ this.stopBatching(true);
2253
+ }
2254
+
2255
+ var connectContext = {
2256
+ "client": message.body.client,
2257
+ "transport": this._transportName,
2258
+ "latency": this._latency
2259
+ };
2260
+ this.trigger('connect', [connectContext]);
2261
+ } else {
2262
+ this.trigger('error', [{"message": message}]);
2263
+ }
2264
+ };
2265
+
2266
+ centrifugeProto._disconnectResponse = function (message) {
2267
+ if (!errorExists(message)) {
2268
+ var shouldReconnect = false;
2269
+ if ("reconnect" in message.body) {
2270
+ shouldReconnect = message.body["reconnect"];
2271
+ }
2272
+ var reason = "";
2273
+ if ("reason" in message.body) {
2274
+ reason = message.body["reason"];
2275
+ }
2276
+ this._disconnect(reason, shouldReconnect, true);
2277
+ } else {
2278
+ this.trigger('error', [{"message": message}]);
2279
+ }
2280
+ };
2281
+
2282
+ centrifugeProto._subscribeResponse = function (message) {
2283
+ var body = message.body;
2284
+ if (body === null) {
2285
+ return;
2286
+ }
2287
+ var channel = body.channel;
2288
+
2289
+ var sub = this._getSub(channel);
2290
+ if (!sub) {
2291
+ return;
2292
+ }
2293
+
2294
+ if (!sub._isSubscribing()) {
2295
+ return;
2296
+ }
2297
+
2298
+ if (!errorExists(message)) {
2299
+ sub._setSubscribeSuccess();
2300
+ var messages = body["messages"];
2301
+ if (messages && messages.length > 0) {
2302
+ // handle missed messages
2303
+ for (var i in messages.reverse()) {
2304
+ this._messageResponse({body: messages[i]});
2305
+ }
2306
+ } else {
2307
+ if ("last" in body) {
2308
+ // no missed messages found so set last message id from body.
2309
+ this._lastMessageID[channel] = body["last"];
2310
+ }
2311
+ }
2312
+ } else {
2313
+ this.trigger('error', [{"message": message}]);
2314
+ sub._setSubscribeError(this._errorObjectFromMessage(message));
2315
+ }
2316
+ };
2317
+
2318
+ centrifugeProto._unsubscribeResponse = function (message) {
2319
+ var uid = message.uid;
2320
+ var body = message.body;
2321
+ var channel = body.channel;
2322
+
2323
+ var sub = this._getSub(channel);
2324
+ if (!sub) {
2325
+ return;
2326
+ }
2327
+
2328
+ if (!errorExists(message)) {
2329
+ if (!uid) {
2330
+ // unsubscribe command from server – unsubscribe all current subs
2331
+ sub._setUnsubscribed();
2332
+ }
2333
+ // ignore client initiated successful unsubscribe responses as we
2334
+ // already unsubscribed on client level.
2335
+ } else {
2336
+ this.trigger('error', [{"message": message}]);
2337
+ }
2338
+ };
2339
+
2340
+ centrifugeProto._publishResponse = function (message) {
2341
+ var uid = message.uid;
2342
+ var body = message.body;
2343
+ if (!(uid in this._callbacks)) {
2344
+ return;
2345
+ }
2346
+ var callbacks = this._callbacks[uid];
2347
+ delete this._callbacks[uid];
2348
+ if (!errorExists(message)) {
2349
+ var callback = callbacks["callback"];
2350
+ if (!callback) {
2351
+ return;
2352
+ }
2353
+ callback(body);
2354
+ } else {
2355
+ var errback = callbacks["errback"];
2356
+ if (!errback) {
2357
+ return;
2358
+ }
2359
+ errback(this._errorObjectFromMessage(message));
2360
+ this.trigger('error', [{"message": message}]);
2361
+ }
2362
+ };
2363
+
2364
+ centrifugeProto._presenceResponse = function (message) {
2365
+ var uid = message.uid;
2366
+ var body = message.body;
2367
+ if (!(uid in this._callbacks)) {
2368
+ return;
2369
+ }
2370
+ var callbacks = this._callbacks[uid];
2371
+ delete this._callbacks[uid];
2372
+ if (!errorExists(message)) {
2373
+ var callback = callbacks["callback"];
2374
+ if (!callback) {
2375
+ return;
2376
+ }
2377
+ callback(body);
2378
+ } else {
2379
+ var errback = callbacks["errback"];
2380
+ if (!errback) {
2381
+ return;
2382
+ }
2383
+ errback(this._errorObjectFromMessage(message));
2384
+ this.trigger('error', [{"message": message}]);
2385
+ }
2386
+ };
2387
+
2388
+ centrifugeProto._historyResponse = function (message) {
2389
+ var uid = message.uid;
2390
+ var body = message.body;
2391
+ if (!(uid in this._callbacks)) {
2392
+ return;
2393
+ }
2394
+ var callbacks = this._callbacks[uid];
2395
+ delete this._callbacks[uid];
2396
+ if (!errorExists(message)) {
2397
+ var callback = callbacks["callback"];
2398
+ if (!callback) {
2399
+ return;
2400
+ }
2401
+ callback(body);
2402
+ } else {
2403
+ var errback = callbacks["errback"];
2404
+ if (!errback) {
2405
+ return;
2406
+ }
2407
+ errback(this._errorObjectFromMessage(message));
2408
+ this.trigger('error', [{"message": message}]);
2409
+ }
2410
+ };
2411
+
2412
+ centrifugeProto._joinResponse = function(message) {
2413
+ var body = message.body;
2414
+ var channel = body.channel;
2415
+
2416
+ var sub = this._getSub(channel);
2417
+ if (!sub) {
2418
+ return;
2419
+ }
2420
+ sub.trigger('join', [body]);
2421
+ };
2422
+
2423
+ centrifugeProto._leaveResponse = function(message) {
2424
+ var body = message.body;
2425
+ var channel = body.channel;
2426
+
2427
+ var sub = this._getSub(channel);
2428
+ if (!sub) {
2429
+ return;
2430
+ }
2431
+ sub.trigger('leave', [body]);
2432
+ };
2433
+
2434
+ centrifugeProto._messageResponse = function (message) {
2435
+ var body = message.body;
2436
+ var channel = body.channel;
2437
+
2438
+ // keep last uid received from channel.
2439
+ this._lastMessageID[channel] = body["uid"];
2440
+
2441
+ var sub = this._getSub(channel);
2442
+ if (!sub) {
2443
+ return;
2444
+ }
2445
+ sub.trigger('message', [body]);
2446
+ };
2447
+
2448
+ centrifugeProto._refreshResponse = function (message) {
2449
+ if (this._refreshTimeout) {
2450
+ clearTimeout(this._refreshTimeout);
2451
+ }
2452
+ if (!errorExists(message)) {
2453
+ if (message.body.expires) {
2454
+ var self = this;
2455
+ var isExpired = message.body.expired;
2456
+ if (isExpired) {
2457
+ self._refreshTimeout = setTimeout(function () {
2458
+ self._refresh.call(self);
2459
+ }, 3000 + Math.round(Math.random() * 1000));
2460
+ return;
2461
+ }
2462
+ this._clientID = message.body.client;
2463
+ self._refreshTimeout = setTimeout(function () {
2464
+ self._refresh.call(self);
2465
+ }, message.body.ttl * 1000);
2466
+ }
2467
+ } else {
2468
+ this.trigger('error', [{"message": message}]);
2469
+ }
2470
+ };
2471
+
2472
+ centrifugeProto._dispatchMessage = function(message) {
2473
+ if (message === undefined || message === null) {
2474
+ this._debug("dispatch: got undefined or null message");
2475
+ return;
2476
+ }
2477
+
2478
+ var method = message.method;
2479
+
2480
+ if (!method) {
2481
+ this._debug("dispatch: got message with empty method");
2482
+ return;
2483
+ }
2484
+
2485
+ switch (method) {
2486
+ case 'connect':
2487
+ this._connectResponse(message);
2488
+ break;
2489
+ case 'disconnect':
2490
+ this._disconnectResponse(message);
2491
+ break;
2492
+ case 'subscribe':
2493
+ this._subscribeResponse(message);
2494
+ break;
2495
+ case 'unsubscribe':
2496
+ this._unsubscribeResponse(message);
2497
+ break;
2498
+ case 'publish':
2499
+ this._publishResponse(message);
2500
+ break;
2501
+ case 'presence':
2502
+ this._presenceResponse(message);
2503
+ break;
2504
+ case 'history':
2505
+ this._historyResponse(message);
2506
+ break;
2507
+ case 'join':
2508
+ this._joinResponse(message);
2509
+ break;
2510
+ case 'leave':
2511
+ this._leaveResponse(message);
2512
+ break;
2513
+ case 'ping':
2514
+ break;
2515
+ case 'refresh':
2516
+ this._refreshResponse(message);
2517
+ break;
2518
+ case 'message':
2519
+ this._messageResponse(message);
2520
+ break;
2521
+ default:
2522
+ this._debug("dispatch: got message with unknown method" + method);
2523
+ break;
2524
+ }
2525
+ };
2526
+
2527
+ centrifugeProto._receive = function (data) {
2528
+ if (Object.prototype.toString.call(data) === Object.prototype.toString.call([])) {
2529
+ // array of responses received
2530
+ for (var i in data) {
2531
+ if (data.hasOwnProperty(i)) {
2532
+ var msg = data[i];
2533
+ this._dispatchMessage(msg);
2534
+ }
2535
+ }
2536
+ } else if (Object.prototype.toString.call(data) === Object.prototype.toString.call({})) {
2537
+ // one response received
2538
+ this._dispatchMessage(data);
2539
+ }
2540
+ };
2541
+
2542
+ centrifugeProto._flush = function() {
2543
+ var messages = this._messages.slice(0);
2544
+ this._messages = [];
2545
+ this._send(messages);
2546
+ };
2547
+
2548
+ centrifugeProto._ping = function () {
2549
+ var msg = {
2550
+ "method": "ping",
2551
+ "params": {}
2552
+ };
2553
+ this._addMessage(msg);
2554
+ };
2555
+
2556
+ centrifugeProto._recover = function(channel) {
2557
+ return channel in this._lastMessageID;
2558
+ };
2559
+
2560
+ centrifugeProto._getLastID = function(channel) {
2561
+ var lastUID = this._lastMessageID[channel];
2562
+ if (lastUID) {
2563
+ this._debug("last uid found and sent for channel", channel);
2564
+ return lastUID;
2565
+ } else {
2566
+ this._debug("no last uid found for channel", channel);
2567
+ return "";
2568
+ }
2569
+ };
2570
+
2571
+ centrifugeProto._createErrorObject = function(err, advice) {
2572
+ var errObject = {
2573
+ "error": err
2574
+ };
2575
+ if (advice) {
2576
+ errObject["advice"] = advice;
2577
+ }
2578
+ return errObject;
2579
+ };
2580
+
2581
+ centrifugeProto._errorObjectFromMessage = function(message) {
2582
+ var err = message.error;
2583
+ var advice = message["advice"];
2584
+ return this._createErrorObject(err, advice);
2585
+ };
2586
+
2587
+ centrifugeProto._registerCall = function(uid, callback, errback) {
2588
+ var self = this;
2589
+ this._callbacks[uid] = {
2590
+ "callback": callback,
2591
+ "errback": errback
2592
+ };
2593
+ setTimeout(function() {
2594
+ delete self._callbacks[uid];
2595
+ if (isFunction(errback)) {
2596
+ errback(self._createErrorObject("timeout", "retry"));
2597
+ }
2598
+ }, this._config.timeout);
2599
+ };
2600
+
2601
+ centrifugeProto._addMessage = function (message) {
2602
+ var uid = '' + this._nextMessageId();
2603
+ message.uid = uid;
2604
+ if (this._isBatching === true) {
2605
+ this._messages.push(message);
2606
+ } else {
2607
+ this._send([message]);
2608
+ }
2609
+ return uid;
2610
+ };
2611
+
2612
+ centrifugeProto.getClientId = function () {
2613
+ return this._clientID;
2614
+ };
2615
+
2616
+ centrifugeProto.isConnected = centrifugeProto._isConnected;
2617
+
2618
+ centrifugeProto.isDisconnected = centrifugeProto._isDisconnected;
2619
+
2620
+ centrifugeProto.configure = function (configuration) {
2621
+ this._configure.call(this, configuration);
2622
+ };
2623
+
2624
+ centrifugeProto.connect = centrifugeProto._connect;
2625
+
2626
+ centrifugeProto.disconnect = function() {
2627
+ this._disconnect("client", false, true);
2628
+ };
2629
+
2630
+ centrifugeProto.ping = centrifugeProto._ping;
2631
+
2632
+ centrifugeProto.startBatching = function () {
2633
+ // start collecting messages without sending them to Centrifuge until flush
2634
+ // method called
2635
+ this._isBatching = true;
2636
+ };
2637
+
2638
+ centrifugeProto.stopBatching = function(flush) {
2639
+ // stop collecting messages
2640
+ flush = flush || false;
2641
+ this._isBatching = false;
2642
+ if (flush === true) {
2643
+ this.flush();
2644
+ }
2645
+ };
2646
+
2647
+ centrifugeProto.flush = function() {
2648
+ // send batched messages to Centrifuge
2649
+ this._flush();
2650
+ };
2651
+
2652
+ centrifugeProto.startAuthBatching = function() {
2653
+ // start collecting private channels to create bulk authentication
2654
+ // request to authEndpoint when stopAuthBatching will be called
2655
+ this._isAuthBatching = true;
2656
+ };
2657
+
2658
+ centrifugeProto.stopAuthBatching = function() {
2659
+ // create request to authEndpoint with collected private channels
2660
+ // to ask if this client can subscribe on each channel
2661
+ this._isAuthBatching = false;
2662
+ var authChannels = this._authChannels;
2663
+ this._authChannels = {};
2664
+ var channels = [];
2665
+
2666
+ for (var channel in authChannels) {
2667
+ var sub = this._getSub(channel);
2668
+ if (!sub) {
2669
+ continue;
2670
+ }
2671
+ channels.push(channel);
2672
+ }
2673
+
2674
+ if (channels.length == 0) {
2675
+ return;
2676
+ }
2677
+
2678
+ var data = {
2679
+ "client": this.getClientId(),
2680
+ "channels": channels
2681
+ };
2682
+
2683
+ var self = this;
2684
+
2685
+ var cb = function(error, data) {
2686
+ if (error === true) {
2687
+ self._debug("authorization request failed");
2688
+ for (var i in channels) {
2689
+ var channel = channels[i];
2690
+ self._subscribeResponse({
2691
+ "error": "authorization request failed",
2692
+ "advice": "fix",
2693
+ "body": {
2694
+ "channel": channel
2695
+ }
2696
+ });
2697
+ }
2698
+ return;
2699
+ }
2700
+
2701
+ // try to send all subscriptions in one request.
2702
+ var batch = false;
2703
+ if (!self._isBatching) {
2704
+ self.startBatching();
2705
+ batch = true;
2706
+ }
2707
+
2708
+ for (var i in channels) {
2709
+ var channel = channels[i];
2710
+ var channelResponse = data[channel];
2711
+ if (!channelResponse) {
2712
+ // subscription:error
2713
+ self._subscribeResponse({
2714
+ "error": "channel not found in authorization response",
2715
+ "advice": "fix",
2716
+ "body": {
2717
+ "channel": channel
2718
+ }
2719
+ });
2720
+ continue;
2721
+ }
2722
+ if (!channelResponse.status || channelResponse.status === 200) {
2723
+ var msg = {
2724
+ "method": "subscribe",
2725
+ "params": {
2726
+ "channel": channel,
2727
+ "client": self.getClientId(),
2728
+ "info": channelResponse.info,
2729
+ "sign": channelResponse.sign
2730
+ }
2731
+ };
2732
+ var recover = self._recover(channel);
2733
+ if (recover === true) {
2734
+ msg["params"]["recover"] = true;
2735
+ msg["params"]["last"] = self._getLastID(channel);
2736
+ }
2737
+ self._addMessage(msg);
2738
+ } else {
2739
+ self._subscribeResponse({
2740
+ "error": channelResponse.status,
2741
+ "body": {
2742
+ "channel": channel
2743
+ }
2744
+ });
2745
+ }
2746
+ }
2747
+
2748
+ if (batch) {
2749
+ self.stopBatching(true);
2750
+ }
2751
+
2752
+ };
2753
+
2754
+ var transport = this._config.authTransport.toLowerCase();
2755
+ if (transport === "ajax") {
2756
+ this._ajax(this._config.authEndpoint, this._config.authParams, this._config.authHeaders, data, cb);
2757
+ } else if (transport === "jsonp") {
2758
+ this._jsonp(this._config.authEndpoint, this._config.authParams, this._config.authHeaders, data, cb);
2759
+ } else {
2760
+ throw 'Unknown auth transport ' + transport;
2761
+ }
2762
+ };
2763
+
2764
+ centrifugeProto.subscribe = function (channel, events) {
2765
+ if (arguments.length < 1) {
2766
+ throw 'Illegal arguments number: required 1, got ' + arguments.length;
2767
+ }
2768
+ if (!isString(channel)) {
2769
+ throw 'Illegal argument type: channel must be a string';
2770
+ }
2771
+ if (!this._config.resubscribe && !this.isConnected()) {
2772
+ throw 'Can not only subscribe in connected state when resubscribe option is off';
2773
+ }
2774
+
2775
+ var currentSub = this._getSub(channel);
2776
+
2777
+ if (currentSub !== null) {
2778
+ currentSub._setEvents(events);
2779
+ if (currentSub._isUnsubscribed()) {
2780
+ currentSub.subscribe();
2781
+ }
2782
+ return currentSub;
2783
+ } else {
2784
+ var sub = new Sub(this, channel, events);
2785
+ this._subs[channel] = sub;
2786
+ sub.subscribe();
2787
+ return sub;
2788
+ }
2789
+ };
2790
+
2791
+ var _STATE_NEW = 0;
2792
+ var _STATE_SUBSCRIBING = 1;
2793
+ var _STATE_SUCCESS = 2;
2794
+ var _STATE_ERROR = 3;
2795
+ var _STATE_UNSUBSCRIBED = 4;
2796
+
2797
+ function Sub(centrifuge, channel, events) {
2798
+ this._status = _STATE_NEW;
2799
+ this._error = null;
2800
+ this._centrifuge = centrifuge;
2801
+ this.channel = channel;
2802
+ this._setEvents(events);
2803
+ this._isResubscribe = false;
2804
+ this._ready = false;
2805
+ this._promise = null;
2806
+ this._initializePromise();
2807
+ }
2808
+
2809
+ extend(Sub, EventEmitter);
2810
+
2811
+ var subProto = Sub.prototype;
2812
+
2813
+ subProto._initializePromise = function() {
2814
+ this._ready = false;
2815
+ var self = this;
2816
+ this._promise = new Promise(function(resolve, reject) {
2817
+ self._resolve = function(value) {
2818
+ self._ready = true;
2819
+ resolve(value);
2820
+ };
2821
+ self._reject = function(err) {
2822
+ self._ready = true;
2823
+ reject(err);
2824
+ };
2825
+ });
2826
+ };
2827
+
2828
+ subProto._setEvents = function(events) {
2829
+ if (!events) {
2830
+ return;
2831
+ }
2832
+ if (isFunction(events)) {
2833
+ this.on("message", events);
2834
+ } else if (Object.prototype.toString.call(events) === Object.prototype.toString.call({})) {
2835
+ var knownEvents = [
2836
+ "message", "join", "leave", "unsubscribe",
2837
+ "subscribe", "error"
2838
+ ];
2839
+ for (var i in knownEvents) {
2840
+ var ev = knownEvents[i];
2841
+ if (ev in events) {
2842
+ this.on(ev, events[ev]);
2843
+ }
2844
+ }
2845
+ }
2846
+ };
2847
+
2848
+ subProto._isNew = function() {
2849
+ return this._status === _STATE_NEW;
2850
+ };
2851
+
2852
+ subProto._isUnsubscribed = function() {
2853
+ return this._status === _STATE_UNSUBSCRIBED;
2854
+ };
2855
+
2856
+ subProto._isSubscribing = function() {
2857
+ return this._status === _STATE_SUBSCRIBING;
2858
+ };
2859
+
2860
+ subProto._isReady = function() {
2861
+ return this._status === _STATE_SUCCESS || this._status === _STATE_ERROR;
2862
+ };
2863
+
2864
+ subProto._isSuccess = function() {
2865
+ return this._status === _STATE_SUCCESS;
2866
+ };
2867
+
2868
+ subProto._isError = function() {
2869
+ return this._status === _STATE_ERROR;
2870
+ };
2871
+
2872
+ subProto._setNew = function() {
2873
+ this._status = _STATE_NEW;
2874
+ };
2875
+
2876
+ subProto._setSubscribing = function() {
2877
+ if (this._ready === true) {
2878
+ // new promise for this subscription
2879
+ this._initializePromise();
2880
+ this._isResubscribe = true;
2881
+ }
2882
+ this._status = _STATE_SUBSCRIBING;
2883
+ };
2884
+
2885
+ subProto._setSubscribeSuccess = function() {
2886
+ if (this._status == _STATE_SUCCESS) {
2887
+ return;
2888
+ }
2889
+ this._status = _STATE_SUCCESS;
2890
+ var successContext = this._getSubscribeSuccessContext();
2891
+ this.trigger("subscribe", [successContext]);
2892
+ this._resolve(successContext);
2893
+ };
2894
+
2895
+ subProto._setSubscribeError = function(err) {
2896
+ if (this._status == _STATE_ERROR) {
2897
+ return;
2898
+ }
2899
+ this._status = _STATE_ERROR;
2900
+ this._error = err;
2901
+ var errContext = this._getSubscribeErrorContext();
2902
+ this.trigger("error", [errContext]);
2903
+ this._reject(errContext);
2904
+ };
2905
+
2906
+ subProto._triggerUnsubscribe = function() {
2907
+ var unsubscribeContext = {
2908
+ "channel": this.channel
2909
+ };
2910
+ this.trigger("unsubscribe", [unsubscribeContext]);
2911
+ };
2912
+
2913
+ subProto._setUnsubscribed = function() {
2914
+ if (this._status == _STATE_UNSUBSCRIBED) {
2915
+ return;
2916
+ }
2917
+ this._status = _STATE_UNSUBSCRIBED;
2918
+ this._triggerUnsubscribe();
2919
+ };
2920
+
2921
+ subProto._getSubscribeSuccessContext = function() {
2922
+ return {
2923
+ "channel": this.channel,
2924
+ "isResubscribe": this._isResubscribe
2925
+ };
2926
+ };
2927
+
2928
+ subProto._getSubscribeErrorContext = function() {
2929
+ var subscribeErrorContext = this._error;
2930
+ subscribeErrorContext["channel"] = this.channel;
2931
+ subscribeErrorContext["isResubscribe"] = this._isResubscribe;
2932
+ return subscribeErrorContext;
2933
+ };
2934
+
2935
+ subProto.ready = function(callback, errback) {
2936
+ if (this._ready) {
2937
+ if (this._isSuccess()) {
2938
+ callback(this._getSubscribeSuccessContext());
2939
+ } else {
2940
+ errback(this._getSubscribeErrorContext());
2941
+ }
2942
+ }
2943
+ };
2944
+
2945
+ subProto.subscribe = function() {
2946
+ if (this._status == _STATE_SUCCESS) {
2947
+ return;
2948
+ }
2949
+ this._centrifuge._subscribe(this);
2950
+ return this;
2951
+ };
2952
+
2953
+ subProto.unsubscribe = function () {
2954
+ this._setUnsubscribed();
2955
+ this._centrifuge._unsubscribe(this);
2956
+ };
2957
+
2958
+ subProto.publish = function (data) {
2959
+ var self = this;
2960
+ return new Promise(function(resolve, reject) {
2961
+ if (self._isUnsubscribed()) {
2962
+ reject(self._centrifuge._createErrorObject("subscription unsubscribed", "fix"));
2963
+ return;
2964
+ }
2965
+ self._promise.then(function(){
2966
+ if (!self._centrifuge.isConnected()) {
2967
+ reject(self._centrifuge._createErrorObject("disconnected", "retry"));
2968
+ return;
2969
+ }
2970
+ var msg = {
2971
+ "method": "publish",
2972
+ "params": {
2973
+ "channel": self.channel,
2974
+ "data": data
2975
+ }
2976
+ };
2977
+ var uid = self._centrifuge._addMessage(msg);
2978
+ self._centrifuge._registerCall(uid, resolve, reject);
2979
+ }, function(err){
2980
+ reject(err);
2981
+ });
2982
+ });
2983
+ };
2984
+
2985
+ subProto.presence = function() {
2986
+ var self = this;
2987
+ return new Promise(function(resolve, reject) {
2988
+ if (self._isUnsubscribed()) {
2989
+ reject(self._centrifuge._createErrorObject("subscription unsubscribed", "fix"));
2990
+ return;
2991
+ }
2992
+ self._promise.then(function(){
2993
+ if (!self._centrifuge.isConnected()) {
2994
+ reject(self._centrifuge._createErrorObject("disconnected", "retry"));
2995
+ return;
2996
+ }
2997
+ var msg = {
2998
+ "method": "presence",
2999
+ "params": {
3000
+ "channel": self.channel
3001
+ }
3002
+ };
3003
+ var uid = self._centrifuge._addMessage(msg);
3004
+ self._centrifuge._registerCall(uid, resolve, reject);
3005
+ }, function(err){
3006
+ reject(err);
3007
+ });
3008
+ });
3009
+ };
3010
+
3011
+ subProto.history = function() {
3012
+ var self = this;
3013
+ return new Promise(function(resolve, reject) {
3014
+ if (self._isUnsubscribed()) {
3015
+ reject(self._centrifuge._createErrorObject("subscription unsubscribed", "fix"));
3016
+ return;
3017
+ }
3018
+ self._promise.then(function(){
3019
+ if (!self._centrifuge.isConnected()) {
3020
+ reject(self._centrifuge._createErrorObject("disconnected", "retry"));
3021
+ return;
3022
+ }
3023
+ var msg = {
3024
+ "method": "history",
3025
+ "params": {
3026
+ "channel": self.channel
3027
+ }
3028
+ };
3029
+ var uid = self._centrifuge._addMessage(msg);
3030
+ self._centrifuge._registerCall(uid, resolve, reject);
3031
+ }, function(err){
3032
+ reject(err);
3033
+ });
3034
+ });
3035
+ };
3036
+
3037
+ module.exports = Centrifuge;
3038
+
3039
+
3040
+
3041
+ }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
3042
+ },{"es6-promise":2,"wolfy87-eventemitter":3}]},{},[4])(4)
3043
+ });