vendor/assets/javascripts/centrifuge.js in centrifuge-1.1.0 vs vendor/assets/javascripts/centrifuge.js in centrifuge-1.1.1

- old
+ new

@@ -1,3042 +1,6084 @@ -(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); \ No newline at end of file +!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){ +// shim for using process in browser + +var process = module.exports = {}; + +process.nextTick = (function () { + var canSetImmediate = typeof window !== 'undefined' + && window.setImmediate; + var canMutationObserver = typeof window !== 'undefined' + && window.MutationObserver; + var canPost = typeof window !== 'undefined' + && window.postMessage && window.addEventListener + ; + + if (canSetImmediate) { + return function (f) { return window.setImmediate(f) }; + } + + var queue = []; + + if (canMutationObserver) { + var hiddenDiv = document.createElement("div"); + var observer = new MutationObserver(function () { + var queueList = queue.slice(); + queue.length = 0; + queueList.forEach(function (fn) { + fn(); + }); + }); + + observer.observe(hiddenDiv, { attributes: true }); + + return function nextTick(fn) { + if (!queue.length) { + hiddenDiv.setAttribute('yes', 'no'); + } + queue.push(fn); + }; + } + + if (canPost) { + window.addEventListener('message', function (ev) { + var source = ev.source; + if ((source === window || source === null) && ev.data === 'process-tick') { + ev.stopPropagation(); + if (queue.length > 0) { + var fn = queue.shift(); + fn(); + } + } + }, true); + + return function nextTick(fn) { + queue.push(fn); + window.postMessage('process-tick', '*'); + }; + } + + return function nextTick(fn) { + setTimeout(fn, 0); + }; +})(); + +process.title = 'browser'; +process.browser = true; +process.env = {}; +process.argv = []; + +function noop() {} + +process.on = noop; +process.addListener = noop; +process.once = noop; +process.off = noop; +process.removeListener = noop; +process.removeAllListeners = noop; +process.emit = noop; + +process.binding = function (name) { + throw new Error('process.binding is not supported'); +}; + +// TODO(shtylman) +process.cwd = function () { return '/' }; +process.chdir = function (dir) { + throw new Error('process.chdir is not supported'); +}; + +},{}],2:[function(require,module,exports){ +(function (process,global){ +/*! + * @overview es6-promise - a tiny implementation of Promises/A+. + * @copyright Copyright (c) 2014 Yehuda Katz, Tom Dale, Stefan Penner and contributors (Conversion to ES6 API by Jake Archibald) + * @license Licensed under MIT license + * See https://raw.githubusercontent.com/jakearchibald/es6-promise/master/LICENSE + * @version 3.0.2 + */ + +(function() { + "use strict"; + function lib$es6$promise$utils$$objectOrFunction(x) { + return typeof x === 'function' || (typeof x === 'object' && x !== null); + } + + function lib$es6$promise$utils$$isFunction(x) { + return typeof x === 'function'; + } + + function lib$es6$promise$utils$$isMaybeThenable(x) { + return typeof x === 'object' && x !== null; + } + + var lib$es6$promise$utils$$_isArray; + if (!Array.isArray) { + lib$es6$promise$utils$$_isArray = function (x) { + return Object.prototype.toString.call(x) === '[object Array]'; + }; + } else { + lib$es6$promise$utils$$_isArray = Array.isArray; + } + + var lib$es6$promise$utils$$isArray = lib$es6$promise$utils$$_isArray; + var lib$es6$promise$asap$$len = 0; + var lib$es6$promise$asap$$toString = {}.toString; + var lib$es6$promise$asap$$vertxNext; + var lib$es6$promise$asap$$customSchedulerFn; + + var lib$es6$promise$asap$$asap = function asap(callback, arg) { + lib$es6$promise$asap$$queue[lib$es6$promise$asap$$len] = callback; + lib$es6$promise$asap$$queue[lib$es6$promise$asap$$len + 1] = arg; + lib$es6$promise$asap$$len += 2; + if (lib$es6$promise$asap$$len === 2) { + // If len is 2, that means that we need to schedule an async flush. + // If additional callbacks are queued before the queue is flushed, they + // will be processed by this flush that we are scheduling. + if (lib$es6$promise$asap$$customSchedulerFn) { + lib$es6$promise$asap$$customSchedulerFn(lib$es6$promise$asap$$flush); + } else { + lib$es6$promise$asap$$scheduleFlush(); + } + } + } + + function lib$es6$promise$asap$$setScheduler(scheduleFn) { + lib$es6$promise$asap$$customSchedulerFn = scheduleFn; + } + + function lib$es6$promise$asap$$setAsap(asapFn) { + lib$es6$promise$asap$$asap = asapFn; + } + + var lib$es6$promise$asap$$browserWindow = (typeof window !== 'undefined') ? window : undefined; + var lib$es6$promise$asap$$browserGlobal = lib$es6$promise$asap$$browserWindow || {}; + var lib$es6$promise$asap$$BrowserMutationObserver = lib$es6$promise$asap$$browserGlobal.MutationObserver || lib$es6$promise$asap$$browserGlobal.WebKitMutationObserver; + var lib$es6$promise$asap$$isNode = typeof process !== 'undefined' && {}.toString.call(process) === '[object process]'; + + // test for web worker but not in IE10 + var lib$es6$promise$asap$$isWorker = typeof Uint8ClampedArray !== 'undefined' && + typeof importScripts !== 'undefined' && + typeof MessageChannel !== 'undefined'; + + // node + function lib$es6$promise$asap$$useNextTick() { + // node version 0.10.x displays a deprecation warning when nextTick is used recursively + // see https://github.com/cujojs/when/issues/410 for details + return function() { + process.nextTick(lib$es6$promise$asap$$flush); + }; + } + + // vertx + function lib$es6$promise$asap$$useVertxTimer() { + return function() { + lib$es6$promise$asap$$vertxNext(lib$es6$promise$asap$$flush); + }; + } + + function lib$es6$promise$asap$$useMutationObserver() { + var iterations = 0; + var observer = new lib$es6$promise$asap$$BrowserMutationObserver(lib$es6$promise$asap$$flush); + var node = document.createTextNode(''); + observer.observe(node, { characterData: true }); + + return function() { + node.data = (iterations = ++iterations % 2); + }; + } + + // web worker + function lib$es6$promise$asap$$useMessageChannel() { + var channel = new MessageChannel(); + channel.port1.onmessage = lib$es6$promise$asap$$flush; + return function () { + channel.port2.postMessage(0); + }; + } + + function lib$es6$promise$asap$$useSetTimeout() { + return function() { + setTimeout(lib$es6$promise$asap$$flush, 1); + }; + } + + var lib$es6$promise$asap$$queue = new Array(1000); + function lib$es6$promise$asap$$flush() { + for (var i = 0; i < lib$es6$promise$asap$$len; i+=2) { + var callback = lib$es6$promise$asap$$queue[i]; + var arg = lib$es6$promise$asap$$queue[i+1]; + + callback(arg); + + lib$es6$promise$asap$$queue[i] = undefined; + lib$es6$promise$asap$$queue[i+1] = undefined; + } + + lib$es6$promise$asap$$len = 0; + } + + function lib$es6$promise$asap$$attemptVertx() { + try { + var r = require; + var vertx = r('vertx'); + lib$es6$promise$asap$$vertxNext = vertx.runOnLoop || vertx.runOnContext; + return lib$es6$promise$asap$$useVertxTimer(); + } catch(e) { + return lib$es6$promise$asap$$useSetTimeout(); + } + } + + var lib$es6$promise$asap$$scheduleFlush; + // Decide what async method to use to triggering processing of queued callbacks: + if (lib$es6$promise$asap$$isNode) { + lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useNextTick(); + } else if (lib$es6$promise$asap$$BrowserMutationObserver) { + lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useMutationObserver(); + } else if (lib$es6$promise$asap$$isWorker) { + lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useMessageChannel(); + } else if (lib$es6$promise$asap$$browserWindow === undefined && typeof require === 'function') { + lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$attemptVertx(); + } else { + lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useSetTimeout(); + } + + function lib$es6$promise$$internal$$noop() {} + + var lib$es6$promise$$internal$$PENDING = void 0; + var lib$es6$promise$$internal$$FULFILLED = 1; + var lib$es6$promise$$internal$$REJECTED = 2; + + var lib$es6$promise$$internal$$GET_THEN_ERROR = new lib$es6$promise$$internal$$ErrorObject(); + + function lib$es6$promise$$internal$$selfFulfillment() { + return new TypeError("You cannot resolve a promise with itself"); + } + + function lib$es6$promise$$internal$$cannotReturnOwn() { + return new TypeError('A promises callback cannot return that same promise.'); + } + + function lib$es6$promise$$internal$$getThen(promise) { + try { + return promise.then; + } catch(error) { + lib$es6$promise$$internal$$GET_THEN_ERROR.error = error; + return lib$es6$promise$$internal$$GET_THEN_ERROR; + } + } + + function lib$es6$promise$$internal$$tryThen(then, value, fulfillmentHandler, rejectionHandler) { + try { + then.call(value, fulfillmentHandler, rejectionHandler); + } catch(e) { + return e; + } + } + + function lib$es6$promise$$internal$$handleForeignThenable(promise, thenable, then) { + lib$es6$promise$asap$$asap(function(promise) { + var sealed = false; + var error = lib$es6$promise$$internal$$tryThen(then, thenable, function(value) { + if (sealed) { return; } + sealed = true; + if (thenable !== value) { + lib$es6$promise$$internal$$resolve(promise, value); + } else { + lib$es6$promise$$internal$$fulfill(promise, value); + } + }, function(reason) { + if (sealed) { return; } + sealed = true; + + lib$es6$promise$$internal$$reject(promise, reason); + }, 'Settle: ' + (promise._label || ' unknown promise')); + + if (!sealed && error) { + sealed = true; + lib$es6$promise$$internal$$reject(promise, error); + } + }, promise); + } + + function lib$es6$promise$$internal$$handleOwnThenable(promise, thenable) { + if (thenable._state === lib$es6$promise$$internal$$FULFILLED) { + lib$es6$promise$$internal$$fulfill(promise, thenable._result); + } else if (thenable._state === lib$es6$promise$$internal$$REJECTED) { + lib$es6$promise$$internal$$reject(promise, thenable._result); + } else { + lib$es6$promise$$internal$$subscribe(thenable, undefined, function(value) { + lib$es6$promise$$internal$$resolve(promise, value); + }, function(reason) { + lib$es6$promise$$internal$$reject(promise, reason); + }); + } + } + + function lib$es6$promise$$internal$$handleMaybeThenable(promise, maybeThenable) { + if (maybeThenable.constructor === promise.constructor) { + lib$es6$promise$$internal$$handleOwnThenable(promise, maybeThenable); + } else { + var then = lib$es6$promise$$internal$$getThen(maybeThenable); + + if (then === lib$es6$promise$$internal$$GET_THEN_ERROR) { + lib$es6$promise$$internal$$reject(promise, lib$es6$promise$$internal$$GET_THEN_ERROR.error); + } else if (then === undefined) { + lib$es6$promise$$internal$$fulfill(promise, maybeThenable); + } else if (lib$es6$promise$utils$$isFunction(then)) { + lib$es6$promise$$internal$$handleForeignThenable(promise, maybeThenable, then); + } else { + lib$es6$promise$$internal$$fulfill(promise, maybeThenable); + } + } + } + + function lib$es6$promise$$internal$$resolve(promise, value) { + if (promise === value) { + lib$es6$promise$$internal$$reject(promise, lib$es6$promise$$internal$$selfFulfillment()); + } else if (lib$es6$promise$utils$$objectOrFunction(value)) { + lib$es6$promise$$internal$$handleMaybeThenable(promise, value); + } else { + lib$es6$promise$$internal$$fulfill(promise, value); + } + } + + function lib$es6$promise$$internal$$publishRejection(promise) { + if (promise._onerror) { + promise._onerror(promise._result); + } + + lib$es6$promise$$internal$$publish(promise); + } + + function lib$es6$promise$$internal$$fulfill(promise, value) { + if (promise._state !== lib$es6$promise$$internal$$PENDING) { return; } + + promise._result = value; + promise._state = lib$es6$promise$$internal$$FULFILLED; + + if (promise._subscribers.length !== 0) { + lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publish, promise); + } + } + + function lib$es6$promise$$internal$$reject(promise, reason) { + if (promise._state !== lib$es6$promise$$internal$$PENDING) { return; } + promise._state = lib$es6$promise$$internal$$REJECTED; + promise._result = reason; + + lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publishRejection, promise); + } + + function lib$es6$promise$$internal$$subscribe(parent, child, onFulfillment, onRejection) { + var subscribers = parent._subscribers; + var length = subscribers.length; + + parent._onerror = null; + + subscribers[length] = child; + subscribers[length + lib$es6$promise$$internal$$FULFILLED] = onFulfillment; + subscribers[length + lib$es6$promise$$internal$$REJECTED] = onRejection; + + if (length === 0 && parent._state) { + lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publish, parent); + } + } + + function lib$es6$promise$$internal$$publish(promise) { + var subscribers = promise._subscribers; + var settled = promise._state; + + if (subscribers.length === 0) { return; } + + var child, callback, detail = promise._result; + + for (var i = 0; i < subscribers.length; i += 3) { + child = subscribers[i]; + callback = subscribers[i + settled]; + + if (child) { + lib$es6$promise$$internal$$invokeCallback(settled, child, callback, detail); + } else { + callback(detail); + } + } + + promise._subscribers.length = 0; + } + + function lib$es6$promise$$internal$$ErrorObject() { + this.error = null; + } + + var lib$es6$promise$$internal$$TRY_CATCH_ERROR = new lib$es6$promise$$internal$$ErrorObject(); + + function lib$es6$promise$$internal$$tryCatch(callback, detail) { + try { + return callback(detail); + } catch(e) { + lib$es6$promise$$internal$$TRY_CATCH_ERROR.error = e; + return lib$es6$promise$$internal$$TRY_CATCH_ERROR; + } + } + + function lib$es6$promise$$internal$$invokeCallback(settled, promise, callback, detail) { + var hasCallback = lib$es6$promise$utils$$isFunction(callback), + value, error, succeeded, failed; + + if (hasCallback) { + value = lib$es6$promise$$internal$$tryCatch(callback, detail); + + if (value === lib$es6$promise$$internal$$TRY_CATCH_ERROR) { + failed = true; + error = value.error; + value = null; + } else { + succeeded = true; + } + + if (promise === value) { + lib$es6$promise$$internal$$reject(promise, lib$es6$promise$$internal$$cannotReturnOwn()); + return; + } + + } else { + value = detail; + succeeded = true; + } + + if (promise._state !== lib$es6$promise$$internal$$PENDING) { + // noop + } else if (hasCallback && succeeded) { + lib$es6$promise$$internal$$resolve(promise, value); + } else if (failed) { + lib$es6$promise$$internal$$reject(promise, error); + } else if (settled === lib$es6$promise$$internal$$FULFILLED) { + lib$es6$promise$$internal$$fulfill(promise, value); + } else if (settled === lib$es6$promise$$internal$$REJECTED) { + lib$es6$promise$$internal$$reject(promise, value); + } + } + + function lib$es6$promise$$internal$$initializePromise(promise, resolver) { + try { + resolver(function resolvePromise(value){ + lib$es6$promise$$internal$$resolve(promise, value); + }, function rejectPromise(reason) { + lib$es6$promise$$internal$$reject(promise, reason); + }); + } catch(e) { + lib$es6$promise$$internal$$reject(promise, e); + } + } + + function lib$es6$promise$enumerator$$Enumerator(Constructor, input) { + var enumerator = this; + + enumerator._instanceConstructor = Constructor; + enumerator.promise = new Constructor(lib$es6$promise$$internal$$noop); + + if (enumerator._validateInput(input)) { + enumerator._input = input; + enumerator.length = input.length; + enumerator._remaining = input.length; + + enumerator._init(); + + if (enumerator.length === 0) { + lib$es6$promise$$internal$$fulfill(enumerator.promise, enumerator._result); + } else { + enumerator.length = enumerator.length || 0; + enumerator._enumerate(); + if (enumerator._remaining === 0) { + lib$es6$promise$$internal$$fulfill(enumerator.promise, enumerator._result); + } + } + } else { + lib$es6$promise$$internal$$reject(enumerator.promise, enumerator._validationError()); + } + } + + lib$es6$promise$enumerator$$Enumerator.prototype._validateInput = function(input) { + return lib$es6$promise$utils$$isArray(input); + }; + + lib$es6$promise$enumerator$$Enumerator.prototype._validationError = function() { + return new Error('Array Methods must be provided an Array'); + }; + + lib$es6$promise$enumerator$$Enumerator.prototype._init = function() { + this._result = new Array(this.length); + }; + + var lib$es6$promise$enumerator$$default = lib$es6$promise$enumerator$$Enumerator; + + lib$es6$promise$enumerator$$Enumerator.prototype._enumerate = function() { + var enumerator = this; + + var length = enumerator.length; + var promise = enumerator.promise; + var input = enumerator._input; + + for (var i = 0; promise._state === lib$es6$promise$$internal$$PENDING && i < length; i++) { + enumerator._eachEntry(input[i], i); + } + }; + + lib$es6$promise$enumerator$$Enumerator.prototype._eachEntry = function(entry, i) { + var enumerator = this; + var c = enumerator._instanceConstructor; + + if (lib$es6$promise$utils$$isMaybeThenable(entry)) { + if (entry.constructor === c && entry._state !== lib$es6$promise$$internal$$PENDING) { + entry._onerror = null; + enumerator._settledAt(entry._state, i, entry._result); + } else { + enumerator._willSettleAt(c.resolve(entry), i); + } + } else { + enumerator._remaining--; + enumerator._result[i] = entry; + } + }; + + lib$es6$promise$enumerator$$Enumerator.prototype._settledAt = function(state, i, value) { + var enumerator = this; + var promise = enumerator.promise; + + if (promise._state === lib$es6$promise$$internal$$PENDING) { + enumerator._remaining--; + + if (state === lib$es6$promise$$internal$$REJECTED) { + lib$es6$promise$$internal$$reject(promise, value); + } else { + enumerator._result[i] = value; + } + } + + if (enumerator._remaining === 0) { + lib$es6$promise$$internal$$fulfill(promise, enumerator._result); + } + }; + + lib$es6$promise$enumerator$$Enumerator.prototype._willSettleAt = function(promise, i) { + var enumerator = this; + + lib$es6$promise$$internal$$subscribe(promise, undefined, function(value) { + enumerator._settledAt(lib$es6$promise$$internal$$FULFILLED, i, value); + }, function(reason) { + enumerator._settledAt(lib$es6$promise$$internal$$REJECTED, i, reason); + }); + }; + function lib$es6$promise$promise$all$$all(entries) { + return new lib$es6$promise$enumerator$$default(this, entries).promise; + } + var lib$es6$promise$promise$all$$default = lib$es6$promise$promise$all$$all; + function lib$es6$promise$promise$race$$race(entries) { + /*jshint validthis:true */ + var Constructor = this; + + var promise = new Constructor(lib$es6$promise$$internal$$noop); + + if (!lib$es6$promise$utils$$isArray(entries)) { + lib$es6$promise$$internal$$reject(promise, new TypeError('You must pass an array to race.')); + return promise; + } + + var length = entries.length; + + function onFulfillment(value) { + lib$es6$promise$$internal$$resolve(promise, value); + } + + function onRejection(reason) { + lib$es6$promise$$internal$$reject(promise, reason); + } + + for (var i = 0; promise._state === lib$es6$promise$$internal$$PENDING && i < length; i++) { + lib$es6$promise$$internal$$subscribe(Constructor.resolve(entries[i]), undefined, onFulfillment, onRejection); + } + + return promise; + } + var lib$es6$promise$promise$race$$default = lib$es6$promise$promise$race$$race; + function lib$es6$promise$promise$resolve$$resolve(object) { + /*jshint validthis:true */ + var Constructor = this; + + if (object && typeof object === 'object' && object.constructor === Constructor) { + return object; + } + + var promise = new Constructor(lib$es6$promise$$internal$$noop); + lib$es6$promise$$internal$$resolve(promise, object); + return promise; + } + var lib$es6$promise$promise$resolve$$default = lib$es6$promise$promise$resolve$$resolve; + function lib$es6$promise$promise$reject$$reject(reason) { + /*jshint validthis:true */ + var Constructor = this; + var promise = new Constructor(lib$es6$promise$$internal$$noop); + lib$es6$promise$$internal$$reject(promise, reason); + return promise; + } + var lib$es6$promise$promise$reject$$default = lib$es6$promise$promise$reject$$reject; + + var lib$es6$promise$promise$$counter = 0; + + function lib$es6$promise$promise$$needsResolver() { + throw new TypeError('You must pass a resolver function as the first argument to the promise constructor'); + } + + function lib$es6$promise$promise$$needsNew() { + throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function."); + } + + var lib$es6$promise$promise$$default = lib$es6$promise$promise$$Promise; + /** + Promise objects represent the eventual result of an asynchronous operation. The + primary way of interacting with a promise is through its `then` method, which + registers callbacks to receive either a promise's eventual value or the reason + why the promise cannot be fulfilled. + + Terminology + ----------- + + - `promise` is an object or function with a `then` method whose behavior conforms to this specification. + - `thenable` is an object or function that defines a `then` method. + - `value` is any legal JavaScript value (including undefined, a thenable, or a promise). + - `exception` is a value that is thrown using the throw statement. + - `reason` is a value that indicates why a promise was rejected. + - `settled` the final resting state of a promise, fulfilled or rejected. + + A promise can be in one of three states: pending, fulfilled, or rejected. + + Promises that are fulfilled have a fulfillment value and are in the fulfilled + state. Promises that are rejected have a rejection reason and are in the + rejected state. A fulfillment value is never a thenable. + + Promises can also be said to *resolve* a value. If this value is also a + promise, then the original promise's settled state will match the value's + settled state. So a promise that *resolves* a promise that rejects will + itself reject, and a promise that *resolves* a promise that fulfills will + itself fulfill. + + + Basic Usage: + ------------ + + ```js + var promise = new Promise(function(resolve, reject) { + // on success + resolve(value); + + // on failure + reject(reason); + }); + + promise.then(function(value) { + // on fulfillment + }, function(reason) { + // on rejection + }); + ``` + + Advanced Usage: + --------------- + + Promises shine when abstracting away asynchronous interactions such as + `XMLHttpRequest`s. + + ```js + function getJSON(url) { + return new Promise(function(resolve, reject){ + var xhr = new XMLHttpRequest(); + + xhr.open('GET', url); + xhr.onreadystatechange = handler; + xhr.responseType = 'json'; + xhr.setRequestHeader('Accept', 'application/json'); + xhr.send(); + + function handler() { + if (this.readyState === this.DONE) { + if (this.status === 200) { + resolve(this.response); + } else { + reject(new Error('getJSON: `' + url + '` failed with status: [' + this.status + ']')); + } + } + }; + }); + } + + getJSON('/posts.json').then(function(json) { + // on fulfillment + }, function(reason) { + // on rejection + }); + ``` + + Unlike callbacks, promises are great composable primitives. + + ```js + Promise.all([ + getJSON('/posts'), + getJSON('/comments') + ]).then(function(values){ + values[0] // => postsJSON + values[1] // => commentsJSON + + return values; + }); + ``` + + @class Promise + @param {function} resolver + Useful for tooling. + @constructor + */ + function lib$es6$promise$promise$$Promise(resolver) { + this._id = lib$es6$promise$promise$$counter++; + this._state = undefined; + this._result = undefined; + this._subscribers = []; + + if (lib$es6$promise$$internal$$noop !== resolver) { + if (!lib$es6$promise$utils$$isFunction(resolver)) { + lib$es6$promise$promise$$needsResolver(); + } + + if (!(this instanceof lib$es6$promise$promise$$Promise)) { + lib$es6$promise$promise$$needsNew(); + } + + lib$es6$promise$$internal$$initializePromise(this, resolver); + } + } + + lib$es6$promise$promise$$Promise.all = lib$es6$promise$promise$all$$default; + lib$es6$promise$promise$$Promise.race = lib$es6$promise$promise$race$$default; + lib$es6$promise$promise$$Promise.resolve = lib$es6$promise$promise$resolve$$default; + lib$es6$promise$promise$$Promise.reject = lib$es6$promise$promise$reject$$default; + lib$es6$promise$promise$$Promise._setScheduler = lib$es6$promise$asap$$setScheduler; + lib$es6$promise$promise$$Promise._setAsap = lib$es6$promise$asap$$setAsap; + lib$es6$promise$promise$$Promise._asap = lib$es6$promise$asap$$asap; + + lib$es6$promise$promise$$Promise.prototype = { + constructor: lib$es6$promise$promise$$Promise, + + /** + The primary way of interacting with a promise is through its `then` method, + which registers callbacks to receive either a promise's eventual value or the + reason why the promise cannot be fulfilled. + + ```js + findUser().then(function(user){ + // user is available + }, function(reason){ + // user is unavailable, and you are given the reason why + }); + ``` + + Chaining + -------- + + The return value of `then` is itself a promise. This second, 'downstream' + promise is resolved with the return value of the first promise's fulfillment + or rejection handler, or rejected if the handler throws an exception. + + ```js + findUser().then(function (user) { + return user.name; + }, function (reason) { + return 'default name'; + }).then(function (userName) { + // If `findUser` fulfilled, `userName` will be the user's name, otherwise it + // will be `'default name'` + }); + + findUser().then(function (user) { + throw new Error('Found user, but still unhappy'); + }, function (reason) { + throw new Error('`findUser` rejected and we're unhappy'); + }).then(function (value) { + // never reached + }, function (reason) { + // if `findUser` fulfilled, `reason` will be 'Found user, but still unhappy'. + // If `findUser` rejected, `reason` will be '`findUser` rejected and we're unhappy'. + }); + ``` + If the downstream promise does not specify a rejection handler, rejection reasons will be propagated further downstream. + + ```js + findUser().then(function (user) { + throw new PedagogicalException('Upstream error'); + }).then(function (value) { + // never reached + }).then(function (value) { + // never reached + }, function (reason) { + // The `PedgagocialException` is propagated all the way down to here + }); + ``` + + Assimilation + ------------ + + Sometimes the value you want to propagate to a downstream promise can only be + retrieved asynchronously. This can be achieved by returning a promise in the + fulfillment or rejection handler. The downstream promise will then be pending + until the returned promise is settled. This is called *assimilation*. + + ```js + findUser().then(function (user) { + return findCommentsByAuthor(user); + }).then(function (comments) { + // The user's comments are now available + }); + ``` + + If the assimliated promise rejects, then the downstream promise will also reject. + + ```js + findUser().then(function (user) { + return findCommentsByAuthor(user); + }).then(function (comments) { + // If `findCommentsByAuthor` fulfills, we'll have the value here + }, function (reason) { + // If `findCommentsByAuthor` rejects, we'll have the reason here + }); + ``` + + Simple Example + -------------- + + Synchronous Example + + ```javascript + var result; + + try { + result = findResult(); + // success + } catch(reason) { + // failure + } + ``` + + Errback Example + + ```js + findResult(function(result, err){ + if (err) { + // failure + } else { + // success + } + }); + ``` + + Promise Example; + + ```javascript + findResult().then(function(result){ + // success + }, function(reason){ + // failure + }); + ``` + + Advanced Example + -------------- + + Synchronous Example + + ```javascript + var author, books; + + try { + author = findAuthor(); + books = findBooksByAuthor(author); + // success + } catch(reason) { + // failure + } + ``` + + Errback Example + + ```js + + function foundBooks(books) { + + } + + function failure(reason) { + + } + + findAuthor(function(author, err){ + if (err) { + failure(err); + // failure + } else { + try { + findBoooksByAuthor(author, function(books, err) { + if (err) { + failure(err); + } else { + try { + foundBooks(books); + } catch(reason) { + failure(reason); + } + } + }); + } catch(error) { + failure(err); + } + // success + } + }); + ``` + + Promise Example; + + ```javascript + findAuthor(). + then(findBooksByAuthor). + then(function(books){ + // found books + }).catch(function(reason){ + // something went wrong + }); + ``` + + @method then + @param {Function} onFulfilled + @param {Function} onRejected + Useful for tooling. + @return {Promise} + */ + then: function(onFulfillment, onRejection) { + var parent = this; + var state = parent._state; + + if (state === lib$es6$promise$$internal$$FULFILLED && !onFulfillment || state === lib$es6$promise$$internal$$REJECTED && !onRejection) { + return this; + } + + var child = new this.constructor(lib$es6$promise$$internal$$noop); + var result = parent._result; + + if (state) { + var callback = arguments[state - 1]; + lib$es6$promise$asap$$asap(function(){ + lib$es6$promise$$internal$$invokeCallback(state, child, callback, result); + }); + } else { + lib$es6$promise$$internal$$subscribe(parent, child, onFulfillment, onRejection); + } + + return child; + }, + + /** + `catch` is simply sugar for `then(undefined, onRejection)` which makes it the same + as the catch block of a try/catch statement. + + ```js + function findAuthor(){ + throw new Error('couldn't find that author'); + } + + // synchronous + try { + findAuthor(); + } catch(reason) { + // something went wrong + } + + // async with promises + findAuthor().catch(function(reason){ + // something went wrong + }); + ``` + + @method catch + @param {Function} onRejection + Useful for tooling. + @return {Promise} + */ + 'catch': function(onRejection) { + return this.then(null, onRejection); + } + }; + function lib$es6$promise$polyfill$$polyfill() { + var local; + + if (typeof global !== 'undefined') { + local = global; + } else if (typeof self !== 'undefined') { + local = self; + } else { + try { + local = Function('return this')(); + } catch (e) { + throw new Error('polyfill failed because global object is unavailable in this environment'); + } + } + + var P = local.Promise; + + if (P && Object.prototype.toString.call(P.resolve()) === '[object Promise]' && !P.cast) { + return; + } + + local.Promise = lib$es6$promise$promise$$default; + } + var lib$es6$promise$polyfill$$default = lib$es6$promise$polyfill$$polyfill; + + var lib$es6$promise$umd$$ES6Promise = { + 'Promise': lib$es6$promise$promise$$default, + 'polyfill': lib$es6$promise$polyfill$$default + }; + + /* global define:true module:true window: true */ + if (typeof define === 'function' && define['amd']) { + define(function() { return lib$es6$promise$umd$$ES6Promise; }); + } else if (typeof module !== 'undefined' && module['exports']) { + module['exports'] = lib$es6$promise$umd$$ES6Promise; + } else if (typeof this !== 'undefined') { + this['ES6Promise'] = lib$es6$promise$umd$$ES6Promise; + } + + lib$es6$promise$polyfill$$default(); +}).call(this); + + +}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) +},{"_process":1}],3:[function(require,module,exports){ +/*! + * EventEmitter v4.2.11 - git.io/ee + * Unlicense - http://unlicense.org/ + * Oliver Caldwell - http://oli.me.uk/ + * @preserve + */ + +;(function () { + 'use strict'; + + /** + * Class for managing events. + * Can be extended to provide event functionality in other classes. + * + * @class EventEmitter Manages event registering and emitting. + */ + function EventEmitter() {} + + // Shortcuts to improve speed and size + var proto = EventEmitter.prototype; + var exports = this; + var originalGlobalValue = exports.EventEmitter; + + /** + * Finds the index of the listener for the event in its storage array. + * + * @param {Function[]} listeners Array of listeners to search through. + * @param {Function} listener Method to look for. + * @return {Number} Index of the specified listener, -1 if not found + * @api private + */ + function indexOfListener(listeners, listener) { + var i = listeners.length; + while (i--) { + if (listeners[i].listener === listener) { + return i; + } + } + + return -1; + } + + /** + * Alias a method while keeping the context correct, to allow for overwriting of target method. + * + * @param {String} name The name of the target method. + * @return {Function} The aliased method + * @api private + */ + function alias(name) { + return function aliasClosure() { + return this[name].apply(this, arguments); + }; + } + + /** + * Returns the listener array for the specified event. + * Will initialise the event object and listener arrays if required. + * 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. + * Each property in the object response is an array of listener functions. + * + * @param {String|RegExp} evt Name of the event to return the listeners from. + * @return {Function[]|Object} All listener functions for the event. + */ + proto.getListeners = function getListeners(evt) { + var events = this._getEvents(); + var response; + var key; + + // Return a concatenated array of all matching events if + // the selector is a regular expression. + if (evt instanceof RegExp) { + response = {}; + for (key in events) { + if (events.hasOwnProperty(key) && evt.test(key)) { + response[key] = events[key]; + } + } + } + else { + response = events[evt] || (events[evt] = []); + } + + return response; + }; + + /** + * Takes a list of listener objects and flattens it into a list of listener functions. + * + * @param {Object[]} listeners Raw listener objects. + * @return {Function[]} Just the listener functions. + */ + proto.flattenListeners = function flattenListeners(listeners) { + var flatListeners = []; + var i; + + for (i = 0; i < listeners.length; i += 1) { + flatListeners.push(listeners[i].listener); + } + + return flatListeners; + }; + + /** + * 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. + * + * @param {String|RegExp} evt Name of the event to return the listeners from. + * @return {Object} All listener functions for an event in an object. + */ + proto.getListenersAsObject = function getListenersAsObject(evt) { + var listeners = this.getListeners(evt); + var response; + + if (listeners instanceof Array) { + response = {}; + response[evt] = listeners; + } + + return response || listeners; + }; + + /** + * Adds a listener function to the specified event. + * The listener will not be added if it is a duplicate. + * If the listener returns true then it will be removed after it is called. + * If you pass a regular expression as the event name then the listener will be added to all events that match it. + * + * @param {String|RegExp} evt Name of the event to attach the listener to. + * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.addListener = function addListener(evt, listener) { + var listeners = this.getListenersAsObject(evt); + var listenerIsWrapped = typeof listener === 'object'; + var key; + + for (key in listeners) { + if (listeners.hasOwnProperty(key) && indexOfListener(listeners[key], listener) === -1) { + listeners[key].push(listenerIsWrapped ? listener : { + listener: listener, + once: false + }); + } + } + + return this; + }; + + /** + * Alias of addListener + */ + proto.on = alias('addListener'); + + /** + * Semi-alias of addListener. It will add a listener that will be + * automatically removed after its first execution. + * + * @param {String|RegExp} evt Name of the event to attach the listener to. + * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.addOnceListener = function addOnceListener(evt, listener) { + return this.addListener(evt, { + listener: listener, + once: true + }); + }; + + /** + * Alias of addOnceListener. + */ + proto.once = alias('addOnceListener'); + + /** + * 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. + * You need to tell it what event names should be matched by a regex. + * + * @param {String} evt Name of the event to create. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.defineEvent = function defineEvent(evt) { + this.getListeners(evt); + return this; + }; + + /** + * Uses defineEvent to define multiple events. + * + * @param {String[]} evts An array of event names to define. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.defineEvents = function defineEvents(evts) { + for (var i = 0; i < evts.length; i += 1) { + this.defineEvent(evts[i]); + } + return this; + }; + + /** + * Removes a listener function from the specified event. + * When passed a regular expression as the event name, it will remove the listener from all events that match it. + * + * @param {String|RegExp} evt Name of the event to remove the listener from. + * @param {Function} listener Method to remove from the event. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.removeListener = function removeListener(evt, listener) { + var listeners = this.getListenersAsObject(evt); + var index; + var key; + + for (key in listeners) { + if (listeners.hasOwnProperty(key)) { + index = indexOfListener(listeners[key], listener); + + if (index !== -1) { + listeners[key].splice(index, 1); + } + } + } + + return this; + }; + + /** + * Alias of removeListener + */ + proto.off = alias('removeListener'); + + /** + * Adds listeners in bulk using the manipulateListeners method. + * 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. + * You can also pass it a regular expression to add the array of listeners to all events that match it. + * Yeah, this function does quite a bit. That's probably a bad thing. + * + * @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. + * @param {Function[]} [listeners] An optional array of listener functions to add. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.addListeners = function addListeners(evt, listeners) { + // Pass through to manipulateListeners + return this.manipulateListeners(false, evt, listeners); + }; + + /** + * Removes listeners in bulk using the manipulateListeners method. + * 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. + * You can also pass it an event name and an array of listeners to be removed. + * You can also pass it a regular expression to remove the listeners from all events that match it. + * + * @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. + * @param {Function[]} [listeners] An optional array of listener functions to remove. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.removeListeners = function removeListeners(evt, listeners) { + // Pass through to manipulateListeners + return this.manipulateListeners(true, evt, listeners); + }; + + /** + * 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. + * The first argument will determine if the listeners are removed (true) or added (false). + * 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. + * You can also pass it an event name and an array of listeners to be added/removed. + * You can also pass it a regular expression to manipulate the listeners of all events that match it. + * + * @param {Boolean} remove True if you want to remove listeners, false if you want to add. + * @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. + * @param {Function[]} [listeners] An optional array of listener functions to add/remove. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.manipulateListeners = function manipulateListeners(remove, evt, listeners) { + var i; + var value; + var single = remove ? this.removeListener : this.addListener; + var multiple = remove ? this.removeListeners : this.addListeners; + + // If evt is an object then pass each of its properties to this method + if (typeof evt === 'object' && !(evt instanceof RegExp)) { + for (i in evt) { + if (evt.hasOwnProperty(i) && (value = evt[i])) { + // Pass the single listener straight through to the singular method + if (typeof value === 'function') { + single.call(this, i, value); + } + else { + // Otherwise pass back to the multiple function + multiple.call(this, i, value); + } + } + } + } + else { + // So evt must be a string + // And listeners must be an array of listeners + // Loop over it and pass each one to the multiple method + i = listeners.length; + while (i--) { + single.call(this, evt, listeners[i]); + } + } + + return this; + }; + + /** + * Removes all listeners from a specified event. + * If you do not specify an event then all listeners will be removed. + * That means every event will be emptied. + * You can also pass a regex to remove all events that match it. + * + * @param {String|RegExp} [evt] Optional name of the event to remove all listeners for. Will remove from every event if not passed. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.removeEvent = function removeEvent(evt) { + var type = typeof evt; + var events = this._getEvents(); + var key; + + // Remove different things depending on the state of evt + if (type === 'string') { + // Remove all listeners for the specified event + delete events[evt]; + } + else if (evt instanceof RegExp) { + // Remove all events matching the regex. + for (key in events) { + if (events.hasOwnProperty(key) && evt.test(key)) { + delete events[key]; + } + } + } + else { + // Remove all listeners in all events + delete this._events; + } + + return this; + }; + + /** + * Alias of removeEvent. + * + * Added to mirror the node API. + */ + proto.removeAllListeners = alias('removeEvent'); + + /** + * Emits an event of your choice. + * When emitted, every listener attached to that event will be executed. + * If you pass the optional argument array then those arguments will be passed to every listener upon execution. + * Because it uses `apply`, your array of arguments will be passed as if you wrote them out separately. + * So they will not arrive within the array on the other side, they will be separate. + * You can also pass a regular expression to emit to all events that match it. + * + * @param {String|RegExp} evt Name of the event to emit and execute listeners for. + * @param {Array} [args] Optional array of arguments to be passed to each listener. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.emitEvent = function emitEvent(evt, args) { + var listenersMap = this.getListenersAsObject(evt); + var listeners; + var listener; + var i; + var key; + var response; + + for (key in listenersMap) { + if (listenersMap.hasOwnProperty(key)) { + listeners = listenersMap[key].slice(0); + i = listeners.length; + + while (i--) { + // If the listener returns true then it shall be removed from the event + // The function is executed either with a basic call or an apply if there is an args array + listener = listeners[i]; + + if (listener.once === true) { + this.removeListener(evt, listener.listener); + } + + response = listener.listener.apply(this, args || []); + + if (response === this._getOnceReturnValue()) { + this.removeListener(evt, listener.listener); + } + } + } + } + + return this; + }; + + /** + * Alias of emitEvent + */ + proto.trigger = alias('emitEvent'); + + /** + * 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. + * As with emitEvent, you can pass a regex in place of the event name to emit to all events that match it. + * + * @param {String|RegExp} evt Name of the event to emit and execute listeners for. + * @param {...*} Optional additional arguments to be passed to each listener. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.emit = function emit(evt) { + var args = Array.prototype.slice.call(arguments, 1); + return this.emitEvent(evt, args); + }; + + /** + * Sets the current value to check against when executing listeners. If a + * listeners return value matches the one set here then it will be removed + * after execution. This value defaults to true. + * + * @param {*} value The new value to check for when executing listeners. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.setOnceReturnValue = function setOnceReturnValue(value) { + this._onceReturnValue = value; + return this; + }; + + /** + * Fetches the current value to check against when executing listeners. If + * the listeners return value matches this one then it should be removed + * automatically. It will return true by default. + * + * @return {*|Boolean} The current value to check for or the default, true. + * @api private + */ + proto._getOnceReturnValue = function _getOnceReturnValue() { + if (this.hasOwnProperty('_onceReturnValue')) { + return this._onceReturnValue; + } + else { + return true; + } + }; + + /** + * Fetches the events object and creates one if required. + * + * @return {Object} The events storage object. + * @api private + */ + proto._getEvents = function _getEvents() { + return this._events || (this._events = {}); + }; + + /** + * Reverts the global {@link EventEmitter} to its previous value and returns a reference to this version. + * + * @return {Function} Non conflicting EventEmitter class. + */ + EventEmitter.noConflict = function noConflict() { + exports.EventEmitter = originalGlobalValue; + return EventEmitter; + }; + + // Expose the class either via AMD, CommonJS or the global object + if (typeof define === 'function' && define.amd) { + define(function () { + return EventEmitter; + }); + } + else if (typeof module === 'object' && module.exports){ + module.exports = EventEmitter; + } + else { + exports.EventEmitter = EventEmitter; + } +}.call(this)); + +},{}],4:[function(require,module,exports){ +(function (global){ +var Promise = require('es6-promise').Promise; +var EventEmitter = require('wolfy87-eventemitter'); + +/** + * Oliver Caldwell + * http://oli.me.uk/2013/06/01/prototypical-inheritance-done-right/ + */ +if (!Object.create) { + Object.create = (function(){ + function F(){} + return function(o){ + if (arguments.length != 1) { + throw new Error('Object.create implementation only accepts one parameter.'); + } + F.prototype = o; + return new F() + } + })() +} + +function extend(destination, source) { + destination.prototype = Object.create(source.prototype); + destination.prototype.constructor = destination; + return source.prototype; +} + +if (!Array.prototype.indexOf) { + 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}; +} + +function fieldValue(object, name) { + try {return object[name];} catch (x) {return undefined;} +} + +/** + * Mixes in the given objects into the target object by copying the properties. + * @param deep if the copy must be deep + * @param target the target object + * @param objects the objects whose properties are copied into the target + */ +function mixin(deep, target, objects) { + var result = target || {}; + for (var i = 2; i < arguments.length; ++i) { // Skip first 2 parameters (deep and target), and loop over the others + var object = arguments[i]; + if (object === undefined || object === null) { + continue; + } + for (var propName in object) { + var prop = fieldValue(object, propName); + var targ = fieldValue(result, propName); + if (prop === target) { + continue; // Avoid infinite loops + } + if (prop === undefined) { + continue; // Do not mixin undefined values + } + if (deep && typeof prop === 'object' && prop !== null) { + if (prop instanceof Array) { + result[propName] = mixin(deep, targ instanceof Array ? targ : [], prop); + } else { + var source = typeof targ === 'object' && !(targ instanceof Array) ? targ : {}; + result[propName] = mixin(deep, source, prop); + } + } else { + result[propName] = prop; + } + } + } + return result; +} + +function endsWith(value, suffix) { + return value.indexOf(suffix, value.length - suffix.length) !== -1; +} + +function startsWith(value, prefix) { + return value.lastIndexOf(prefix, 0) === 0; +} + +function stripSlash(value) { + if (value.substring(value.length - 1) == "/") { + value = value.substring(0, value.length - 1); + } + return value; +} + +function isString(value) { + if (value === undefined || value === null) { + return false; + } + return typeof value === 'string' || value instanceof String; +} + +function isFunction(value) { + if (value === undefined || value === null) { + return false; + } + return typeof value === 'function'; +} + +function log(level, args) { + if (global.console) { + var logger = global.console[level]; + if (isFunction(logger)) { + logger.apply(global.console, args); + } + } +} + +function backoff(step, min, max) { + var jitter = 0.5 * Math.random(); + var interval = min * Math.pow(2, step+1); + if (interval > max) { + interval = max + } + return Math.floor((1-jitter) * interval); +} + +function errorExists(data) { + return "error" in data && data.error !== null && data.error !== ""; +} + +function Centrifuge(options) { + this._sockjs = false; + this._status = 'disconnected'; + this._reconnect = true; + this._reconnecting = false; + this._transport = null; + this._transportName = null; + this._messageId = 0; + this._clientID = null; + this._subs = {}; + this._lastMessageID = {}; + this._messages = []; + this._isBatching = false; + this._isAuthBatching = false; + this._authChannels = {}; + this._refreshTimeout = null; + this._retries = 0; + this._callbacks = {}; + this._latency = null; + this._latencyStart = null; + this._config = { + retry: 1000, + maxRetry: 20000, + timeout: 5000, + info: "", + resubscribe: true, + debug: false, + insecure: false, + server: null, + privateChannelPrefix: "$", + 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" + }; + if (options) { + this.configure(options); + } +} + +extend(Centrifuge, EventEmitter); + +Centrifuge._authCallbacks = {}; +Centrifuge._nextAuthCallbackID = 1; + +var centrifugeProto = Centrifuge.prototype; + +centrifugeProto._jsonp = function (url, params, headers, data, callback) { + if (headers.length > 0) { + this._log("Only AJAX request allows to send custom headers, it's not possible with JSONP."); + } + self._debug("sending JSONP request to", url); + + var callbackName = Centrifuge._nextAuthCallbackID.toString(); + Centrifuge._nextAuthCallbackID++; + + var document = global.document; + var script = document.createElement("script"); + Centrifuge._authCallbacks[callbackName] = function (data) { + callback(false, data); + delete Centrifuge[callbackName]; + }; + + var query = ""; + for (var i in params) { + if (query.length > 0) { + query += "&"; + } + query += encodeURIComponent(i) + "=" + encodeURIComponent(params[i]); + } + + var callback_name = "Centrifuge._authCallbacks['" + callbackName + "']"; + script.src = this._config.authEndpoint + + '?callback=' + encodeURIComponent(callback_name) + + '&data=' + encodeURIComponent(JSON.stringify(data)) + + '&' + query; + + var head = document.getElementsByTagName("head")[0] || document.documentElement; + head.insertBefore(script, head.firstChild); +}; + +centrifugeProto._ajax = function (url, params, headers, data, callback) { + var self = this; + self._debug("sending AJAX request to", url); + + var xhr = (global.XMLHttpRequest ? new global.XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP")); + + var query = ""; + for (var i in params) { + if (query.length > 0) { + query += "&"; + } + query += encodeURIComponent(i) + "=" + encodeURIComponent(params[i]); + } + if (query.length > 0) { + query = "?" + query; + } + xhr.open("POST", url + query, true); + + // add request headers + xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); + xhr.setRequestHeader("Content-Type", "application/json"); + for (var headerName in headers) { + xhr.setRequestHeader(headerName, headers[headerName]); + } + + xhr.onreadystatechange = function () { + if (xhr.readyState === 4) { + if (xhr.status === 200) { + var data, parsed = false; + + try { + data = JSON.parse(xhr.responseText); + parsed = true; + } catch (e) { + callback(true, 'JSON returned was invalid, yet status code was 200. Data was: ' + xhr.responseText); + } + + if (parsed) { // prevents double execution. + callback(false, data); + } + } else { + self._log("Couldn't get auth info from application", xhr.status); + callback(true, xhr.status); + } + } + }; + + setTimeout(function() { + // method == 'get' ? self.xhr.send() : self.xhr.send(JSON.stringify(ops.data)); + xhr.send(JSON.stringify(data)); + }, 20); + return xhr; +}; + +centrifugeProto._log = function () { + log("info", arguments); +}; + +centrifugeProto._debug = function () { + if (this._config.debug === true) { + log("debug", arguments); + } +}; + +centrifugeProto._configure = function (configuration) { + this._debug('Configuring centrifuge object with', configuration); + + if (!configuration) { + configuration = {}; + } + + this._config = mixin(false, this._config, configuration); + + if (!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'; + } else { + 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\''; + } else { + 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'; + } else { + this._debug("timestamp not found but this is OK for insecure mode"); + } + } + + this._config.url = stripSlash(this._config.url); + + if (endsWith(this._config.url, 'connection')) { + this._debug("client will connect to SockJS endpoint"); + if (typeof SockJS === 'undefined') { + throw 'include SockJS client library before Centrifuge javascript client library or use raw Websocket connection endpoint'; + } + this._sockjs = true; + } else if (endsWith(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://"); + } else { + this._debug("client will detect connection endpoint itself"); + if (typeof SockJS === 'undefined') { + 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://"); + } else { + this._debug("SockJS found, client will connect to SockJS endpoint"); + this._config.url += "/connection"; + this._sockjs = true; + } + } +}; + +centrifugeProto._setStatus = function (newStatus) { + if (this._status !== newStatus) { + this._debug('Status', this._status, '->', newStatus); + this._status = newStatus; + } +}; + +centrifugeProto._isDisconnected = function () { + return this._status === 'disconnected'; +}; + +centrifugeProto._isConnecting = function() { + return this._status === 'connecting'; +}; + +centrifugeProto._isConnected = function () { + return this._status === 'connected'; +}; + +centrifugeProto._nextMessageId = function () { + return ++this._messageId; +}; + +centrifugeProto._resetRetry = function() { + this._debug("reset retries count to 0"); + this._retries = 0; +}; + +centrifugeProto._getRetryInterval = function() { + var interval = backoff(this._retries, this._config.retry, this._config.maxRetry); + this._retries += 1; + return interval; +}; + +centrifugeProto._clearConnectedState = function (reconnect) { + self._clientID = null; + + // fire errbacks of registered calls. + for (var uid in this._callbacks) { + var callbacks = this._callbacks[uid]; + var errback = callbacks["errback"]; + if (!errback) { + continue; + } + errback(this._createErrorObject("disconnected", "retry")); + } + this._callbacks = {}; + + // fire unsubscribe events + for (var channel in this._subs) { + var sub = this._subs[channel]; + if (reconnect) { + if (sub._isSuccess()) { + sub._triggerUnsubscribe(); + } + sub._setSubscribing(); + } else { + sub._setUnsubscribed(); + } + } + + if (!this._config.resubscribe || !this._reconnect) { + // completely clear connected state + this._subs = {}; + } +}; + +centrifugeProto._send = function (messages) { + if (messages.length === 0) { + return; + } + this._debug('Send', messages); + this._transport.send(JSON.stringify(messages)); +}; + +centrifugeProto._connect = function (callback) { + + if (this.isConnected()) { + this._debug("connect called when already connected"); + return; + } + + this._setStatus('connecting'); + this._clientID = null; + this._reconnect = true; + + var self = this; + + if (callback) { + this.on('connect', callback); + } + + // detect transport to use - SockJS or raw Websocket + if (this._sockjs === true) { + var sockjsOptions = { + "transports": this._config.transports + }; + if (this._config.server !== null) { + sockjsOptions['server'] = this._config.server; + } + this._transport = new SockJS(this._config.url, null, sockjsOptions); + } else { + this._transport = new WebSocket(this._config.url); + } + + this._transport.onopen = function () { + + self._reconnecting = false; + + if (self._sockjs) { + self._transportName = self._transport._transport.transportName; + } else { + self._transportName = "raw-websocket"; + } + + self._resetRetry(); + + if (!isString(self._config.user)) { + self._log("user expected to be string"); + } + if (!isString(self._config.info)) { + self._log("info expected to be string"); + } + + var msg = { + 'method': 'connect', + 'params': { + 'user': self._config.user, + 'info': self._config.info + } + }; + + if (!self._config.insecure) { + // in insecure client mode we don't need timestamp and token. + msg["params"]["timestamp"] = self._config.timestamp; + msg["params"]["token"] = self._config.token; + if (!isString(self._config.timestamp)) { + self._log("timestamp expected to be string"); + } + if (!isString(self._config.token)) { + self._log("token expected to be string"); + } + } + self._addMessage(msg); + self._latencyStart = new Date(); + }; + + this._transport.onerror = function (error) { + self._debug("transport level error", error); + }; + + this._transport.onclose = function (closeEvent) { + var reason = "connection closed"; + if (closeEvent && "reason" in closeEvent && closeEvent["reason"]) { + reason = closeEvent["reason"]; + } + self._disconnect(reason, true, false); + }; + + this._transport.onmessage = function (event) { + var data; + data = JSON.parse(event.data); + self._debug('Received', data); + self._receive(data); + }; +}; + +centrifugeProto._disconnect = function (reason, shouldReconnect, closeTransport) { + this._debug("disconnected:", reason, shouldReconnect); + var reconnect = shouldReconnect || false; + if (reconnect === false) { + this._reconnect = false; + } + + this._clearConnectedState(shouldReconnect); + + if (!this.isDisconnected()) { + this._setStatus('disconnected'); + var disconnectContext = { + "reason": reason, + "reconnect": reconnect + }; + if (this._reconnecting === false) { + this.trigger('disconnect', [disconnectContext]); + } + } + + if (closeTransport) { + this._transport.close(); + } + + var self = this; + if (shouldReconnect === true && self._reconnect === true) { + self._reconnecting = true; + var interval = self._getRetryInterval(); + self._debug("reconnect after " + interval + " milliseconds"); + setTimeout(function () { + if (self._reconnect === true) { + self._connect.call(self); + } + }, interval); + } +}; + +centrifugeProto._refresh = function () { + // ask web app for connection parameters - user ID, + // timestamp, info and token + var self = this; + this._debug('refresh credentials'); + + var cb = function(error, data) { + if (error === true) { + // 403 or 500 - does not matter - if connection check activated then Centrifugo + // will disconnect client eventually + self._debug("error getting connect parameters", data); + if (self._refreshTimeout) { + clearTimeout(self._refreshTimeout); + } + self._refreshTimeout = setTimeout(function(){ + self._refresh.call(self); + }, 3000); + return; + } + self._config.user = data.user; + self._config.timestamp = data.timestamp; + self._config.info = data.info; + self._config.token = data.token; + if (self.isDisconnected()) { + self._debug("credentials refreshed, connect from scratch"); + self._connect(); + } else { + self._debug("send refreshed credentials"); + var msg = { + "method": "refresh", + "params": { + 'user': self._config.user, + 'timestamp': self._config.timestamp, + 'info': self._config.info, + 'token': self._config.token + } + }; + self._addMessage(msg); + } + }; + + var transport = this._config.refreshTransport.toLowerCase(); + if (transport === "ajax") { + this._ajax(this._config.refreshEndpoint, this._config.refreshParams, this._config.refreshHeaders, {}, cb); + } else if (transport === "jsonp") { + this._jsonp(this._config.refreshEndpoint, this._config.refreshParams, this._config.refreshHeaders, {}, cb); + } else { + throw 'Unknown refresh transport ' + transport; + } +}; + +centrifugeProto._subscribe = function(sub) { + + var channel = sub.channel; + + if (!(channel in this._subs)) { + this._subs[channel] = sub; + } + + if (!this.isConnected()) { + // subscribe will be called later + sub._setNew(); + return; + } + + sub._setSubscribing(); + + var msg = { + "method": "subscribe", + "params": { + "channel": channel + } + }; + + // If channel name does not start with privateChannelPrefix - then we + // can just send subscription message to Centrifuge. If channel name + // starts with privateChannelPrefix - then this is a private channel + // and we should ask web application backend for permission first. + if (startsWith(channel, this._config.privateChannelPrefix)) { + // private channel + if (this._isAuthBatching) { + this._authChannels[channel] = true; + } else { + this.startAuthBatching(); + this._subscribe(sub); + this.stopAuthBatching(); + } + } else { + var recover = this._recover(channel); + if (recover === true) { + msg["params"]["recover"] = true; + msg["params"]["last"] = this._getLastID(channel); + } + this._addMessage(msg); + } +}; + +centrifugeProto._unsubscribe = function(sub) { + if (this.isConnected()) { + // No need to unsubscribe in disconnected state - i.e. client already unsubscribed. + var msg = { + "method": "unsubscribe", + "params": { + "channel": sub.channel + } + }; + this._addMessage(msg); + } +}; + +centrifugeProto._getSub = function(channel) { + var sub = this._subs[channel]; + if (!sub) { + return null; + } + return sub; +}; + +centrifugeProto._connectResponse = function (message) { + + if (this.isConnected()) { + return; + } + + if (!errorExists(message)) { + + if (this._latencyStart !== null) { + this._latency = (new Date()).getTime() - this._latencyStart.getTime(); + this._latencyStart = null; + } + + if (!message.body) { + return; + } + if (message.body.expires) { + var isExpired = message.body.expired; + if (isExpired) { + this._refresh(); + return; + } + } + this._clientID = message.body.client; + this._setStatus('connected'); + + if (this._refreshTimeout) { + clearTimeout(this._refreshTimeout); + } + if (message.body.expires) { + var self = this; + this._refreshTimeout = setTimeout(function() { + self._refresh.call(self); + }, message.body.ttl * 1000); + } + + if (this._config.resubscribe) { + this.startBatching(); + this.startAuthBatching(); + for (var channel in this._subs) { + var sub = this._subs[channel]; + this._subscribe(sub); + } + this.stopAuthBatching(); + this.stopBatching(true); + } + + var connectContext = { + "client": message.body.client, + "transport": this._transportName, + "latency": this._latency + }; + this.trigger('connect', [connectContext]); + } else { + this.trigger('error', [{"message": message}]); + } +}; + +centrifugeProto._disconnectResponse = function (message) { + if (!errorExists(message)) { + var shouldReconnect = false; + if ("reconnect" in message.body) { + shouldReconnect = message.body["reconnect"]; + } + var reason = ""; + if ("reason" in message.body) { + reason = message.body["reason"]; + } + this._disconnect(reason, shouldReconnect, true); + } else { + this.trigger('error', [{"message": message}]); + } +}; + +centrifugeProto._subscribeResponse = function (message) { + var body = message.body; + if (body === null) { + return; + } + var channel = body.channel; + + var sub = this._getSub(channel); + if (!sub) { + return; + } + + if (!sub._isSubscribing()) { + return; + } + + if (!errorExists(message)) { + sub._setSubscribeSuccess(); + var messages = body["messages"]; + if (messages && messages.length > 0) { + // handle missed messages + for (var i in messages.reverse()) { + this._messageResponse({body: messages[i]}); + } + } else { + if ("last" in body) { + // no missed messages found so set last message id from body. + this._lastMessageID[channel] = body["last"]; + } + } + } else { + this.trigger('error', [{"message": message}]); + sub._setSubscribeError(this._errorObjectFromMessage(message)); + } +}; + +centrifugeProto._unsubscribeResponse = function (message) { + var uid = message.uid; + var body = message.body; + var channel = body.channel; + + var sub = this._getSub(channel); + if (!sub) { + return; + } + + if (!errorExists(message)) { + if (!uid) { + // unsubscribe command from server – unsubscribe all current subs + sub._setUnsubscribed(); + } + // ignore client initiated successful unsubscribe responses as we + // already unsubscribed on client level. + } else { + this.trigger('error', [{"message": message}]); + } +}; + +centrifugeProto._publishResponse = function (message) { + var uid = message.uid; + var body = message.body; + if (!(uid in this._callbacks)) { + return; + } + var callbacks = this._callbacks[uid]; + delete this._callbacks[uid]; + if (!errorExists(message)) { + var callback = callbacks["callback"]; + if (!callback) { + return; + } + callback(body); + } else { + var errback = callbacks["errback"]; + if (!errback) { + return; + } + errback(this._errorObjectFromMessage(message)); + this.trigger('error', [{"message": message}]); + } +}; + +centrifugeProto._presenceResponse = function (message) { + var uid = message.uid; + var body = message.body; + if (!(uid in this._callbacks)) { + return; + } + var callbacks = this._callbacks[uid]; + delete this._callbacks[uid]; + if (!errorExists(message)) { + var callback = callbacks["callback"]; + if (!callback) { + return; + } + callback(body); + } else { + var errback = callbacks["errback"]; + if (!errback) { + return; + } + errback(this._errorObjectFromMessage(message)); + this.trigger('error', [{"message": message}]); + } +}; + +centrifugeProto._historyResponse = function (message) { + var uid = message.uid; + var body = message.body; + if (!(uid in this._callbacks)) { + return; + } + var callbacks = this._callbacks[uid]; + delete this._callbacks[uid]; + if (!errorExists(message)) { + var callback = callbacks["callback"]; + if (!callback) { + return; + } + callback(body); + } else { + var errback = callbacks["errback"]; + if (!errback) { + return; + } + errback(this._errorObjectFromMessage(message)); + this.trigger('error', [{"message": message}]); + } +}; + +centrifugeProto._joinResponse = function(message) { + var body = message.body; + var channel = body.channel; + + var sub = this._getSub(channel); + if (!sub) { + return; + } + sub.trigger('join', [body]); +}; + +centrifugeProto._leaveResponse = function(message) { + var body = message.body; + var channel = body.channel; + + var sub = this._getSub(channel); + if (!sub) { + return; + } + sub.trigger('leave', [body]); +}; + +centrifugeProto._messageResponse = function (message) { + var body = message.body; + var channel = body.channel; + + // keep last uid received from channel. + this._lastMessageID[channel] = body["uid"]; + + var sub = this._getSub(channel); + if (!sub) { + return; + } + sub.trigger('message', [body]); +}; + +centrifugeProto._refreshResponse = function (message) { + if (this._refreshTimeout) { + clearTimeout(this._refreshTimeout); + } + if (!errorExists(message)) { + if (message.body.expires) { + var self = this; + var isExpired = message.body.expired; + if (isExpired) { + self._refreshTimeout = setTimeout(function () { + self._refresh.call(self); + }, 3000 + Math.round(Math.random() * 1000)); + return; + } + this._clientID = message.body.client; + self._refreshTimeout = setTimeout(function () { + self._refresh.call(self); + }, message.body.ttl * 1000); + } + } else { + this.trigger('error', [{"message": message}]); + } +}; + +centrifugeProto._dispatchMessage = function(message) { + if (message === undefined || message === null) { + this._debug("dispatch: got undefined or null message"); + return; + } + + var method = message.method; + + if (!method) { + this._debug("dispatch: got message with empty method"); + return; + } + + switch (method) { + case 'connect': + this._connectResponse(message); + break; + case 'disconnect': + this._disconnectResponse(message); + break; + case 'subscribe': + this._subscribeResponse(message); + break; + case 'unsubscribe': + this._unsubscribeResponse(message); + break; + case 'publish': + this._publishResponse(message); + break; + case 'presence': + this._presenceResponse(message); + break; + case 'history': + this._historyResponse(message); + break; + case 'join': + this._joinResponse(message); + break; + case 'leave': + this._leaveResponse(message); + break; + case 'ping': + break; + case 'refresh': + this._refreshResponse(message); + break; + case 'message': + this._messageResponse(message); + break; + default: + this._debug("dispatch: got message with unknown method" + method); + break; + } +}; + +centrifugeProto._receive = function (data) { + if (Object.prototype.toString.call(data) === Object.prototype.toString.call([])) { + // array of responses received + for (var i in data) { + if (data.hasOwnProperty(i)) { + var msg = data[i]; + this._dispatchMessage(msg); + } + } + } else if (Object.prototype.toString.call(data) === Object.prototype.toString.call({})) { + // one response received + this._dispatchMessage(data); + } +}; + +centrifugeProto._flush = function() { + var messages = this._messages.slice(0); + this._messages = []; + this._send(messages); +}; + +centrifugeProto._ping = function () { + var msg = { + "method": "ping", + "params": {} + }; + this._addMessage(msg); +}; + +centrifugeProto._recover = function(channel) { + return channel in this._lastMessageID; +}; + +centrifugeProto._getLastID = function(channel) { + var lastUID = this._lastMessageID[channel]; + if (lastUID) { + this._debug("last uid found and sent for channel", channel); + return lastUID; + } else { + this._debug("no last uid found for channel", channel); + return ""; + } +}; + +centrifugeProto._createErrorObject = function(err, advice) { + var errObject = { + "error": err + }; + if (advice) { + errObject["advice"] = advice; + } + return errObject; +}; + +centrifugeProto._errorObjectFromMessage = function(message) { + var err = message.error; + var advice = message["advice"]; + return this._createErrorObject(err, advice); +}; + +centrifugeProto._registerCall = function(uid, callback, errback) { + var self = this; + this._callbacks[uid] = { + "callback": callback, + "errback": errback + }; + setTimeout(function() { + delete self._callbacks[uid]; + if (isFunction(errback)) { + errback(self._createErrorObject("timeout", "retry")); + } + }, this._config.timeout); +}; + +centrifugeProto._addMessage = function (message) { + var uid = '' + this._nextMessageId(); + message.uid = uid; + if (this._isBatching === true) { + this._messages.push(message); + } else { + this._send([message]); + } + return uid; +}; + +centrifugeProto.getClientId = function () { + return this._clientID; +}; + +centrifugeProto.isConnected = centrifugeProto._isConnected; + +centrifugeProto.isDisconnected = centrifugeProto._isDisconnected; + +centrifugeProto.configure = function (configuration) { + this._configure.call(this, configuration); +}; + +centrifugeProto.connect = centrifugeProto._connect; + +centrifugeProto.disconnect = function() { + this._disconnect("client", false, true); +}; + +centrifugeProto.ping = centrifugeProto._ping; + +centrifugeProto.startBatching = function () { + // start collecting messages without sending them to Centrifuge until flush + // method called + this._isBatching = true; +}; + +centrifugeProto.stopBatching = function(flush) { + // stop collecting messages + flush = flush || false; + this._isBatching = false; + if (flush === true) { + this.flush(); + } +}; + +centrifugeProto.flush = function() { + // send batched messages to Centrifuge + this._flush(); +}; + +centrifugeProto.startAuthBatching = function() { + // start collecting private channels to create bulk authentication + // request to authEndpoint when stopAuthBatching will be called + this._isAuthBatching = true; +}; + +centrifugeProto.stopAuthBatching = function() { + // create request to authEndpoint with collected private channels + // to ask if this client can subscribe on each channel + this._isAuthBatching = false; + var authChannels = this._authChannels; + this._authChannels = {}; + var channels = []; + + for (var channel in authChannels) { + var sub = this._getSub(channel); + if (!sub) { + continue; + } + channels.push(channel); + } + + if (channels.length == 0) { + return; + } + + var data = { + "client": this.getClientId(), + "channels": channels + }; + + var self = this; + + var cb = function(error, data) { + if (error === true) { + self._debug("authorization request failed"); + for (var i in channels) { + var channel = channels[i]; + self._subscribeResponse({ + "error": "authorization request failed", + "advice": "fix", + "body": { + "channel": channel + } + }); + } + return; + } + + // try to send all subscriptions in one request. + var batch = false; + if (!self._isBatching) { + self.startBatching(); + batch = true; + } + + for (var i in channels) { + var channel = channels[i]; + var channelResponse = data[channel]; + if (!channelResponse) { + // subscription:error + self._subscribeResponse({ + "error": "channel not found in authorization response", + "advice": "fix", + "body": { + "channel": channel + } + }); + continue; + } + if (!channelResponse.status || channelResponse.status === 200) { + var msg = { + "method": "subscribe", + "params": { + "channel": channel, + "client": self.getClientId(), + "info": channelResponse.info, + "sign": channelResponse.sign + } + }; + var recover = self._recover(channel); + if (recover === true) { + msg["params"]["recover"] = true; + msg["params"]["last"] = self._getLastID(channel); + } + self._addMessage(msg); + } else { + self._subscribeResponse({ + "error": channelResponse.status, + "body": { + "channel": channel + } + }); + } + } + + if (batch) { + self.stopBatching(true); + } + + }; + + var transport = this._config.authTransport.toLowerCase(); + if (transport === "ajax") { + this._ajax(this._config.authEndpoint, this._config.authParams, this._config.authHeaders, data, cb); + } else if (transport === "jsonp") { + this._jsonp(this._config.authEndpoint, this._config.authParams, this._config.authHeaders, data, cb); + } else { + throw 'Unknown auth transport ' + transport; + } +}; + +centrifugeProto.subscribe = function (channel, events) { + if (arguments.length < 1) { + throw 'Illegal arguments number: required 1, got ' + arguments.length; + } + if (!isString(channel)) { + throw 'Illegal argument type: channel must be a string'; + } + if (!this._config.resubscribe && !this.isConnected()) { + throw 'Can not only subscribe in connected state when resubscribe option is off'; + } + + var currentSub = this._getSub(channel); + + if (currentSub !== null) { + currentSub._setEvents(events); + if (currentSub._isUnsubscribed()) { + currentSub.subscribe(); + } + return currentSub; + } else { + var sub = new Sub(this, channel, events); + this._subs[channel] = sub; + sub.subscribe(); + return sub; + } +}; + +var _STATE_NEW = 0; +var _STATE_SUBSCRIBING = 1; +var _STATE_SUCCESS = 2; +var _STATE_ERROR = 3; +var _STATE_UNSUBSCRIBED = 4; + +function Sub(centrifuge, channel, events) { + this._status = _STATE_NEW; + this._error = null; + this._centrifuge = centrifuge; + this.channel = channel; + this._setEvents(events); + this._isResubscribe = false; + this._ready = false; + this._promise = null; + this._initializePromise(); +} + +extend(Sub, EventEmitter); + +var subProto = Sub.prototype; + +subProto._initializePromise = function() { + this._ready = false; + var self = this; + this._promise = new Promise(function(resolve, reject) { + self._resolve = function(value) { + self._ready = true; + resolve(value); + }; + self._reject = function(err) { + self._ready = true; + reject(err); + }; + }); +}; + +subProto._setEvents = function(events) { + if (!events) { + return; + } + if (isFunction(events)) { + this.on("message", events); + } else if (Object.prototype.toString.call(events) === Object.prototype.toString.call({})) { + var knownEvents = [ + "message", "join", "leave", "unsubscribe", + "subscribe", "error" + ]; + for (var i in knownEvents) { + var ev = knownEvents[i]; + if (ev in events) { + this.on(ev, events[ev]); + } + } + } +}; + +subProto._isNew = function() { + return this._status === _STATE_NEW; +}; + +subProto._isUnsubscribed = function() { + return this._status === _STATE_UNSUBSCRIBED; +}; + +subProto._isSubscribing = function() { + return this._status === _STATE_SUBSCRIBING; +}; + +subProto._isReady = function() { + return this._status === _STATE_SUCCESS || this._status === _STATE_ERROR; +}; + +subProto._isSuccess = function() { + return this._status === _STATE_SUCCESS; +}; + +subProto._isError = function() { + return this._status === _STATE_ERROR; +}; + +subProto._setNew = function() { + this._status = _STATE_NEW; +}; + +subProto._setSubscribing = function() { + if (this._ready === true) { + // new promise for this subscription + this._initializePromise(); + this._isResubscribe = true; + } + this._status = _STATE_SUBSCRIBING; +}; + +subProto._setSubscribeSuccess = function() { + if (this._status == _STATE_SUCCESS) { + return; + } + this._status = _STATE_SUCCESS; + var successContext = this._getSubscribeSuccessContext(); + this.trigger("subscribe", [successContext]); + this._resolve(successContext); +}; + +subProto._setSubscribeError = function(err) { + if (this._status == _STATE_ERROR) { + return; + } + this._status = _STATE_ERROR; + this._error = err; + var errContext = this._getSubscribeErrorContext(); + this.trigger("error", [errContext]); + this._reject(errContext); +}; + +subProto._triggerUnsubscribe = function() { + var unsubscribeContext = { + "channel": this.channel + }; + this.trigger("unsubscribe", [unsubscribeContext]); +}; + +subProto._setUnsubscribed = function() { + if (this._status == _STATE_UNSUBSCRIBED) { + return; + } + this._status = _STATE_UNSUBSCRIBED; + this._triggerUnsubscribe(); +}; + +subProto._getSubscribeSuccessContext = function() { + return { + "channel": this.channel, + "isResubscribe": this._isResubscribe + }; +}; + +subProto._getSubscribeErrorContext = function() { + var subscribeErrorContext = this._error; + subscribeErrorContext["channel"] = this.channel; + subscribeErrorContext["isResubscribe"] = this._isResubscribe; + return subscribeErrorContext; +}; + +subProto.ready = function(callback, errback) { + if (this._ready) { + if (this._isSuccess()) { + callback(this._getSubscribeSuccessContext()); + } else { + errback(this._getSubscribeErrorContext()); + } + } +}; + +subProto.subscribe = function() { + if (this._status == _STATE_SUCCESS) { + return; + } + this._centrifuge._subscribe(this); + return this; +}; + +subProto.unsubscribe = function () { + this._setUnsubscribed(); + this._centrifuge._unsubscribe(this); +}; + +subProto.publish = function (data) { + var self = this; + return new Promise(function(resolve, reject) { + if (self._isUnsubscribed()) { + reject(self._centrifuge._createErrorObject("subscription unsubscribed", "fix")); + return; + } + self._promise.then(function(){ + if (!self._centrifuge.isConnected()) { + reject(self._centrifuge._createErrorObject("disconnected", "retry")); + return; + } + var msg = { + "method": "publish", + "params": { + "channel": self.channel, + "data": data + } + }; + var uid = self._centrifuge._addMessage(msg); + self._centrifuge._registerCall(uid, resolve, reject); + }, function(err){ + reject(err); + }); + }); +}; + +subProto.presence = function() { + var self = this; + return new Promise(function(resolve, reject) { + if (self._isUnsubscribed()) { + reject(self._centrifuge._createErrorObject("subscription unsubscribed", "fix")); + return; + } + self._promise.then(function(){ + if (!self._centrifuge.isConnected()) { + reject(self._centrifuge._createErrorObject("disconnected", "retry")); + return; + } + var msg = { + "method": "presence", + "params": { + "channel": self.channel + } + }; + var uid = self._centrifuge._addMessage(msg); + self._centrifuge._registerCall(uid, resolve, reject); + }, function(err){ + reject(err); + }); + }); +}; + +subProto.history = function() { + var self = this; + return new Promise(function(resolve, reject) { + if (self._isUnsubscribed()) { + reject(self._centrifuge._createErrorObject("subscription unsubscribed", "fix")); + return; + } + self._promise.then(function(){ + if (!self._centrifuge.isConnected()) { + reject(self._centrifuge._createErrorObject("disconnected", "retry")); + return; + } + var msg = { + "method": "history", + "params": { + "channel": self.channel + } + }; + var uid = self._centrifuge._addMessage(msg); + self._centrifuge._registerCall(uid, resolve, reject); + }, function(err){ + reject(err); + }); + }); +}; + +module.exports = Centrifuge; + + + +}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) +},{"es6-promise":2,"wolfy87-eventemitter":3}]},{},[4])(4) +});