Skip to content

Commit

Permalink
Merge pull request phoenixframework#808 from garbles/master
Browse files Browse the repository at this point in the history
Prefer SOCKET_STATES over this.states in phoenix.js
  • Loading branch information
chrismccord committed Apr 25, 2015
2 parents a31a526 + d75b839 commit 9cb5059
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
18 changes: 8 additions & 10 deletions priv/static/phoenix.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,6 @@ var Socket = exports.Socket = (function () {

_classCallCheck(this, Socket);

this.states = SOCKET_STATES;
this.stateChangeCallbacks = { open: [], close: [], error: [], message: [] };
this.flushEveryMs = 50;
this.reconnectTimer = null;
Expand Down Expand Up @@ -510,11 +509,11 @@ var Socket = exports.Socket = (function () {

Socket.prototype.connectionState = function connectionState() {
switch (this.conn && this.conn.readyState) {
case this.states.connecting:
case SOCKET_STATES.connecting:
return "connecting";
case this.states.open:
case SOCKET_STATES.open:
return "open";
case this.states.closing:
case SOCKET_STATES.closing:
return "closing";
default:
return "closed";
Expand Down Expand Up @@ -622,10 +621,9 @@ var LongPoller = exports.LongPoller = (function () {
this.onerror = function () {}; // noop
this.onmessage = function () {}; // noop
this.onclose = function () {}; // noop
this.states = SOCKET_STATES;
this.upgradeEndpoint = this.normalizeEndpoint(endPoint);
this.pollEndpoint = this.upgradeEndpoint + (/\/$/.test(endPoint) ? "poll" : "/poll");
this.readyState = this.states.connecting;
this.readyState = SOCKET_STATES.connecting;

this.poll();
}
Expand All @@ -640,7 +638,7 @@ var LongPoller = exports.LongPoller = (function () {

LongPoller.prototype.closeAndRetry = function closeAndRetry() {
this.close();
this.readyState = this.states.connecting;
this.readyState = SOCKET_STATES.connecting;
};

LongPoller.prototype.ontimeout = function ontimeout() {
Expand All @@ -651,7 +649,7 @@ var LongPoller = exports.LongPoller = (function () {
LongPoller.prototype.poll = function poll() {
var _this = this;

if (!(this.readyState === this.states.open || this.readyState === this.states.connecting)) {
if (!(this.readyState === SOCKET_STATES.open || this.readyState === SOCKET_STATES.connecting)) {
return;
}

Expand Down Expand Up @@ -679,7 +677,7 @@ var LongPoller = exports.LongPoller = (function () {
_this.poll();
break;
case 410:
_this.readyState = _this.states.open;
_this.readyState = SOCKET_STATES.open;
_this.onopen();
_this.poll();
break;
Expand All @@ -706,7 +704,7 @@ var LongPoller = exports.LongPoller = (function () {
};

LongPoller.prototype.close = function close(code, reason) {
this.readyState = this.states.closed;
this.readyState = SOCKET_STATES.closed;
this.onclose();
};

Expand Down
20 changes: 9 additions & 11 deletions web/static/js/phoenix.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ export class Socket {
// For IE8 support use an ES5-shim (https://github.com/es-shims/es5-shim)
//
constructor(endPoint, opts = {}){
this.states = SOCKET_STATES
this.stateChangeCallbacks = {open: [], close: [], error: [], message: []}
this.flushEveryMs = 50
this.reconnectTimer = null
Expand Down Expand Up @@ -290,10 +289,10 @@ export class Socket {

connectionState(){
switch(this.conn && this.conn.readyState){
case this.states.connecting: return "connecting"
case this.states.open: return "open"
case this.states.closing: return "closing"
default: return "closed"
case SOCKET_STATES.connecting: return "connecting"
case SOCKET_STATES.open: return "open"
case SOCKET_STATES.closing: return "closing"
default: return "closed"
}
}

Expand Down Expand Up @@ -367,10 +366,9 @@ export class LongPoller {
this.onerror = function(){} // noop
this.onmessage = function(){} // noop
this.onclose = function(){} // noop
this.states = SOCKET_STATES
this.upgradeEndpoint = this.normalizeEndpoint(endPoint)
this.pollEndpoint = this.upgradeEndpoint + (/\/$/.test(endPoint) ? "poll" : "/poll")
this.readyState = this.states.connecting
this.readyState = SOCKET_STATES.connecting

this.poll()
}
Expand All @@ -385,7 +383,7 @@ export class LongPoller {

closeAndRetry(){
this.close()
this.readyState = this.states.connecting
this.readyState = SOCKET_STATES.connecting
}

ontimeout(){
Expand All @@ -394,7 +392,7 @@ export class LongPoller {
}

poll(){
if(!(this.readyState === this.states.open || this.readyState === this.states.connecting)){ return }
if(!(this.readyState === SOCKET_STATES.open || this.readyState === SOCKET_STATES.connecting)){ return }

Ajax.request("GET", this.endpointURL(), "application/json", null, this.timeout, this.ontimeout.bind(this), (resp) => {
if(resp){
Expand All @@ -414,7 +412,7 @@ export class LongPoller {
this.poll()
break
case 410:
this.readyState = this.states.open
this.readyState = SOCKET_STATES.open
this.onopen()
this.poll()
break
Expand All @@ -438,7 +436,7 @@ export class LongPoller {
}

close(code, reason){
this.readyState = this.states.closed
this.readyState = SOCKET_STATES.closed
this.onclose()
}
}
Expand Down

0 comments on commit 9cb5059

Please sign in to comment.