phoenix-socket
Advanced tools
Comparing version 1.0.4 to 1.1.0
@@ -36,3 +36,3 @@ "use strict"; | ||
// events are listened for, messages are pushed to the server, and | ||
// the channel is joined with ok/error matches, and `after` hook: | ||
// the channel is joined with ok/error/timeout matches: | ||
// | ||
@@ -42,6 +42,6 @@ // let channel = socket.channel("rooms:123", {token: roomToken}) | ||
// $input.onEnter( e => { | ||
// channel.push("new_msg", {body: e.target.val}) | ||
// channel.push("new_msg", {body: e.target.val}, 10000) | ||
// .receive("ok", (msg) => console.log("created message", msg) ) | ||
// .receive("error", (reasons) => console.log("create failed", reasons) ) | ||
// .after(10000, () => console.log("Networking issue. Still waiting...") ) | ||
// .receive("timeout", () => console.log("Networking issue...") ) | ||
// }) | ||
@@ -51,3 +51,3 @@ // channel.join() | ||
// .receive("error", ({reason}) => console.log("failed join", reason) ) | ||
// .after(10000, () => console.log("Networking issue. Still waiting...") ) | ||
// .receive("timeout", () => console.log("Networking issue. Still waiting...") ) | ||
// | ||
@@ -69,4 +69,4 @@ // | ||
// receive responses from the push. Additionally, we can use | ||
// `after(millsec, callback)` to abort waiting for our `receive` hooks and | ||
// take action after some period of waiting. | ||
// `receive("timeout", callback)` to abort waiting for our other `receive` hooks | ||
// and take action after some period of waiting. | ||
// | ||
@@ -106,2 +106,3 @@ // | ||
var SOCKET_STATES = { connecting: 0, open: 1, closing: 2, closed: 3 }; | ||
var DEFAULT_TIMEOUT = 10000; | ||
var CHANNEL_STATES = { | ||
@@ -132,5 +133,6 @@ closed: "closed", | ||
// payload - The payload, for example `{user_id: 123}` | ||
// timeout - The push timeout in milliseconds | ||
// | ||
function Push(channel, event, payload) { | ||
function Push(channel, event, payload, timeout) { | ||
_classCallCheck(this, Push); | ||
@@ -142,3 +144,4 @@ | ||
this.receivedResp = null; | ||
this.afterHook = null; | ||
this.timeout = timeout; | ||
this.timeoutTimer = null; | ||
this.recHooks = []; | ||
@@ -149,19 +152,19 @@ this.sent = false; | ||
_createClass(Push, [{ | ||
key: "resend", | ||
value: function resend(timeout) { | ||
this.timeout = timeout; | ||
this.cancelRefEvent(); | ||
this.ref = null; | ||
this.refEvent = null; | ||
this.receivedResp = null; | ||
this.sent = false; | ||
this.send(); | ||
} | ||
}, { | ||
key: "send", | ||
value: function send() { | ||
var _this = this; | ||
var ref = this.channel.socket.makeRef(); | ||
this.refEvent = this.channel.replyEventName(ref); | ||
this.receivedResp = null; | ||
this.sent = false; | ||
this.channel.on(this.refEvent, function (payload) { | ||
_this.receivedResp = payload; | ||
_this.matchReceive(payload); | ||
_this.cancelRefEvent(); | ||
_this.cancelAfter(); | ||
}); | ||
this.startAfter(); | ||
if (this.hasReceived("timeout")) { | ||
return; | ||
} | ||
this.startTimeout(); | ||
this.sent = true; | ||
@@ -172,3 +175,3 @@ this.channel.socket.push({ | ||
payload: this.payload, | ||
ref: ref | ||
ref: this.ref | ||
}); | ||
@@ -179,3 +182,3 @@ } | ||
value: function receive(status, callback) { | ||
if (this.receivedResp && this.receivedResp.status === status) { | ||
if (this.hasReceived(status)) { | ||
callback(this.receivedResp.response); | ||
@@ -187,15 +190,2 @@ } | ||
} | ||
}, { | ||
key: "after", | ||
value: function after(ms, callback) { | ||
if (this.afterHook) { | ||
throw "only a single after hook can be applied to a push"; | ||
} | ||
var timer = null; | ||
if (this.sent) { | ||
timer = setTimeout(callback, ms); | ||
} | ||
this.afterHook = { ms: ms, callback: callback, timer: timer }; | ||
return this; | ||
} | ||
@@ -220,27 +210,45 @@ // private | ||
value: function cancelRefEvent() { | ||
if (!this.refEvent) { | ||
return; | ||
} | ||
this.channel.off(this.refEvent); | ||
} | ||
}, { | ||
key: "cancelAfter", | ||
value: function cancelAfter() { | ||
if (!this.afterHook) { | ||
return; | ||
} | ||
clearTimeout(this.afterHook.timer); | ||
this.afterHook.timer = null; | ||
key: "cancelTimeout", | ||
value: function cancelTimeout() { | ||
clearTimeout(this.timeoutTimer); | ||
this.timeoutTimer = null; | ||
} | ||
}, { | ||
key: "startAfter", | ||
value: function startAfter() { | ||
var _this2 = this; | ||
key: "startTimeout", | ||
value: function startTimeout() { | ||
var _this = this; | ||
if (!this.afterHook) { | ||
if (this.timeoutTimer) { | ||
return; | ||
} | ||
var callback = function callback() { | ||
_this2.cancelRefEvent(); | ||
_this2.afterHook.callback(); | ||
}; | ||
this.afterHook.timer = setTimeout(callback, this.afterHook.ms); | ||
this.ref = this.channel.socket.makeRef(); | ||
this.refEvent = this.channel.replyEventName(this.ref); | ||
this.channel.on(this.refEvent, function (payload) { | ||
_this.cancelRefEvent(); | ||
_this.cancelTimeout(); | ||
_this.receivedResp = payload; | ||
_this.matchReceive(payload); | ||
}); | ||
this.timeoutTimer = setTimeout(function () { | ||
_this.trigger("timeout", {}); | ||
}, this.timeout); | ||
} | ||
}, { | ||
key: "hasReceived", | ||
value: function hasReceived(status) { | ||
return this.receivedResp && this.receivedResp.status === status; | ||
} | ||
}, { | ||
key: "trigger", | ||
value: function trigger(status, response) { | ||
this.channel.trigger(this.refEvent, { status: status, response: response }); | ||
} | ||
}]); | ||
@@ -253,3 +261,3 @@ | ||
function Channel(topic, params, socket) { | ||
var _this3 = this; | ||
var _this2 = this; | ||
@@ -263,24 +271,38 @@ _classCallCheck(this, Channel); | ||
this.bindings = []; | ||
this.timeout = this.socket.timeout; | ||
this.joinedOnce = false; | ||
this.joinPush = new Push(this, CHANNEL_EVENTS.join, this.params); | ||
this.joinPush = new Push(this, CHANNEL_EVENTS.join, this.params, this.timeout); | ||
this.pushBuffer = []; | ||
this.rejoinTimer = new Timer(function () { | ||
return _this3.rejoinUntilConnected(); | ||
return _this2.rejoinUntilConnected(); | ||
}, this.socket.reconnectAfterMs); | ||
this.joinPush.receive("ok", function () { | ||
_this3.state = CHANNEL_STATES.joined; | ||
_this3.rejoinTimer.reset(); | ||
_this2.state = CHANNEL_STATES.joined; | ||
_this2.rejoinTimer.reset(); | ||
_this2.pushBuffer.forEach(function (pushEvent) { | ||
return pushEvent.send(); | ||
}); | ||
_this2.pushBuffer = []; | ||
}); | ||
this.onClose(function () { | ||
_this3.socket.log("channel", "close " + _this3.topic); | ||
_this3.state = CHANNEL_STATES.closed; | ||
_this3.socket.remove(_this3); | ||
_this2.socket.log("channel", "close " + _this2.topic); | ||
_this2.state = CHANNEL_STATES.closed; | ||
_this2.socket.remove(_this2); | ||
}); | ||
this.onError(function (reason) { | ||
_this3.socket.log("channel", "error " + _this3.topic, reason); | ||
_this3.state = CHANNEL_STATES.errored; | ||
_this3.rejoinTimer.setTimeout(); | ||
_this2.socket.log("channel", "error " + _this2.topic, reason); | ||
_this2.state = CHANNEL_STATES.errored; | ||
_this2.rejoinTimer.setTimeout(); | ||
}); | ||
this.joinPush.receive("timeout", function () { | ||
if (_this2.state !== CHANNEL_STATES.joining) { | ||
return; | ||
} | ||
_this2.socket.log("channel", "timeout " + _this2.topic, _this2.joinPush.timeout); | ||
_this2.state = CHANNEL_STATES.errored; | ||
_this2.rejoinTimer.setTimeout(); | ||
}); | ||
this.on(CHANNEL_EVENTS.reply, function (payload, ref) { | ||
_this3.trigger(_this3.replyEventName(ref), payload); | ||
_this2.trigger(_this2.replyEventName(ref), payload); | ||
}); | ||
@@ -300,2 +322,4 @@ } | ||
value: function join() { | ||
var timeout = arguments.length <= 0 || arguments[0] === undefined ? this.timeout : arguments[0]; | ||
if (this.joinedOnce) { | ||
@@ -306,3 +330,3 @@ throw "tried to join multiple times. 'join' can only be called a single time per channel instance"; | ||
} | ||
this.sendJoin(); | ||
this.rejoin(timeout); | ||
return this.joinPush; | ||
@@ -342,9 +366,12 @@ } | ||
value: function push(event, payload) { | ||
var timeout = arguments.length <= 2 || arguments[2] === undefined ? this.timeout : arguments[2]; | ||
if (!this.joinedOnce) { | ||
throw "tried to push '" + event + "' to '" + this.topic + "' before joining. Use channel.join() before pushing events"; | ||
} | ||
var pushEvent = new Push(this, event, payload); | ||
var pushEvent = new Push(this, event, payload, timeout); | ||
if (this.canPush()) { | ||
pushEvent.send(); | ||
} else { | ||
pushEvent.startTimeout(); | ||
this.pushBuffer.push(pushEvent); | ||
@@ -372,8 +399,22 @@ } | ||
value: function leave() { | ||
var _this4 = this; | ||
var _this3 = this; | ||
return this.push(CHANNEL_EVENTS.leave).receive("ok", function () { | ||
_this4.socket.log("channel", "leave " + _this4.topic); | ||
_this4.trigger(CHANNEL_EVENTS.close, "leave"); | ||
var timeout = arguments.length <= 0 || arguments[0] === undefined ? this.timeout : arguments[0]; | ||
var onClose = function onClose() { | ||
_this3.socket.log("channel", "leave " + _this3.topic); | ||
_this3.trigger(CHANNEL_EVENTS.close, "leave"); | ||
}; | ||
var leavePush = new Push(this, CHANNEL_EVENTS.leave, {}, timeout); | ||
leavePush.receive("ok", function () { | ||
return onClose(); | ||
}).receive("timeout", function () { | ||
return onClose(); | ||
}); | ||
leavePush.send(); | ||
if (!this.canPush()) { | ||
leavePush.trigger("ok", {}); | ||
} | ||
return leavePush; | ||
} | ||
@@ -398,5 +439,5 @@ | ||
key: "sendJoin", | ||
value: function sendJoin() { | ||
value: function sendJoin(timeout) { | ||
this.state = CHANNEL_STATES.joining; | ||
this.joinPush.send(); | ||
this.joinPush.resend(timeout); | ||
} | ||
@@ -406,7 +447,4 @@ }, { | ||
value: function rejoin() { | ||
this.sendJoin(); | ||
this.pushBuffer.forEach(function (pushEvent) { | ||
return pushEvent.send(); | ||
}); | ||
this.pushBuffer = []; | ||
var timeout = arguments.length <= 0 || arguments[0] === undefined ? this.timeout : arguments[0]; | ||
this.sendJoin(timeout); | ||
} | ||
@@ -443,2 +481,4 @@ }, { | ||
// Defaults to WebSocket with automatic LongPoll fallback. | ||
// timeout - The default timeout in milliseconds to trigger push timeouts. | ||
// Defaults `DEFAULT_TIMEOUT` | ||
// heartbeatIntervalMs - The millisec interval to send a heartbeat message | ||
@@ -464,3 +504,3 @@ // reconnectAfterMs - The optional function that returns the millsec | ||
function Socket(endPoint) { | ||
var _this5 = this; | ||
var _this4 = this; | ||
@@ -475,6 +515,7 @@ var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
this.ref = 0; | ||
this.timeout = opts.timeout || DEFAULT_TIMEOUT; | ||
this.transport = opts.transport || window.WebSocket || LongPoll; | ||
this.heartbeatIntervalMs = opts.heartbeatIntervalMs || 30000; | ||
this.reconnectAfterMs = opts.reconnectAfterMs || function (tries) { | ||
return [1000, 5000, 10000][tries - 1] || 10000; | ||
return [1000, 2000, 5000, 10000][tries - 1] || 10000; | ||
}; | ||
@@ -486,4 +527,4 @@ this.logger = opts.logger || function () {}; // noop | ||
this.reconnectTimer = new Timer(function () { | ||
_this5.disconnect(function () { | ||
return _this5.connect(); | ||
_this4.disconnect(function () { | ||
return _this4.connect(); | ||
}); | ||
@@ -531,3 +572,3 @@ }, this.reconnectAfterMs); | ||
value: function connect(params) { | ||
var _this6 = this; | ||
var _this5 = this; | ||
@@ -545,12 +586,12 @@ if (params) { | ||
this.conn.onopen = function () { | ||
return _this6.onConnOpen(); | ||
return _this5.onConnOpen(); | ||
}; | ||
this.conn.onerror = function (error) { | ||
return _this6.onConnError(error); | ||
return _this5.onConnError(error); | ||
}; | ||
this.conn.onmessage = function (event) { | ||
return _this6.onConnMessage(event); | ||
return _this5.onConnMessage(event); | ||
}; | ||
this.conn.onclose = function (event) { | ||
return _this6.onConnClose(event); | ||
return _this5.onConnClose(event); | ||
}; | ||
@@ -597,3 +638,3 @@ } | ||
value: function onConnOpen() { | ||
var _this7 = this; | ||
var _this6 = this; | ||
@@ -606,3 +647,3 @@ this.log("transport", "connected to " + this.endPointURL(), this.transport.prototype); | ||
this.heartbeatTimer = setInterval(function () { | ||
return _this7.sendHeartbeat(); | ||
return _this6.sendHeartbeat(); | ||
}, this.heartbeatIntervalMs); | ||
@@ -679,3 +720,3 @@ } | ||
value: function push(data) { | ||
var _this8 = this; | ||
var _this7 = this; | ||
@@ -688,3 +729,3 @@ var topic = data.topic; | ||
var callback = function callback() { | ||
return _this8.conn.send(JSON.stringify(data)); | ||
return _this7.conn.send(JSON.stringify(data)); | ||
}; | ||
@@ -716,2 +757,5 @@ this.log("push", topic + " " + event + " (" + ref + ")", payload); | ||
value: function sendHeartbeat() { | ||
if (!this.isConnected()) { | ||
return; | ||
} | ||
this.push({ topic: "phoenix", event: "heartbeat", payload: {}, ref: this.makeRef() }); | ||
@@ -795,3 +839,3 @@ } | ||
value: function poll() { | ||
var _this9 = this; | ||
var _this8 = this; | ||
@@ -808,3 +852,3 @@ if (!(this.readyState === SOCKET_STATES.open || this.readyState === SOCKET_STATES.connecting)) { | ||
_this9.token = token; | ||
_this8.token = token; | ||
} else { | ||
@@ -817,18 +861,18 @@ var status = 0; | ||
messages.forEach(function (msg) { | ||
return _this9.onmessage({ data: JSON.stringify(msg) }); | ||
return _this8.onmessage({ data: JSON.stringify(msg) }); | ||
}); | ||
_this9.poll(); | ||
_this8.poll(); | ||
break; | ||
case 204: | ||
_this9.poll(); | ||
_this8.poll(); | ||
break; | ||
case 410: | ||
_this9.readyState = SOCKET_STATES.open; | ||
_this9.onopen(); | ||
_this9.poll(); | ||
_this8.readyState = SOCKET_STATES.open; | ||
_this8.onopen(); | ||
_this8.poll(); | ||
break; | ||
case 0: | ||
case 500: | ||
_this9.onerror(); | ||
_this9.closeAndRetry(); | ||
_this8.onerror(); | ||
_this8.closeAndRetry(); | ||
break; | ||
@@ -843,8 +887,8 @@ default: | ||
value: function send(body) { | ||
var _this10 = this; | ||
var _this9 = this; | ||
Ajax.request("POST", this.endpointURL(), "application/json", body, this.timeout, this.onerror.bind(this, "timeout"), function (resp) { | ||
if (!resp || resp.status !== 200) { | ||
_this10.onerror(status); | ||
_this10.closeAndRetry(); | ||
_this9.onerror(status); | ||
_this9.closeAndRetry(); | ||
} | ||
@@ -884,3 +928,3 @@ }); | ||
value: function xdomainRequest(req, method, endPoint, body, timeout, ontimeout, callback) { | ||
var _this11 = this; | ||
var _this10 = this; | ||
@@ -890,3 +934,3 @@ req.timeout = timeout; | ||
req.onload = function () { | ||
var response = _this11.parseJSON(req.responseText); | ||
var response = _this10.parseJSON(req.responseText); | ||
callback && callback(response); | ||
@@ -906,3 +950,3 @@ }; | ||
value: function xhrRequest(req, method, endPoint, accept, body, timeout, ontimeout, callback) { | ||
var _this12 = this; | ||
var _this11 = this; | ||
@@ -916,4 +960,4 @@ req.timeout = timeout; | ||
req.onreadystatechange = function () { | ||
if (req.readyState === _this12.states.complete && callback) { | ||
var response = _this12.parseJSON(req.responseText); | ||
if (req.readyState === _this11.states.complete && callback) { | ||
var response = _this11.parseJSON(req.responseText); | ||
callback(response); | ||
@@ -1014,3 +1058,3 @@ } | ||
})(function () { | ||
var _this13 = this; | ||
var _this12 = this; | ||
@@ -1020,4 +1064,4 @@ clearTimeout(this.timer); | ||
this.timer = setTimeout(function () { | ||
_this13.tries = _this13.tries + 1; | ||
_this13.callback(); | ||
_this12.tries = _this12.tries + 1; | ||
_this12.callback(); | ||
}, this.timerCalc(this.tries + 1)); | ||
@@ -1024,0 +1068,0 @@ }) |
{ | ||
"name": "phoenix-socket", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"description": "Socket API for accessing Phoenix Framework's Channels", | ||
@@ -5,0 +5,0 @@ "main": "dist/socket.js", |
@@ -24,3 +24,3 @@ // Phoenix Channels JavaScript client | ||
// events are listened for, messages are pushed to the server, and | ||
// the channel is joined with ok/error matches, and `after` hook: | ||
// the channel is joined with ok/error/timeout matches: | ||
// | ||
@@ -30,6 +30,6 @@ // let channel = socket.channel("rooms:123", {token: roomToken}) | ||
// $input.onEnter( e => { | ||
// channel.push("new_msg", {body: e.target.val}) | ||
// channel.push("new_msg", {body: e.target.val}, 10000) | ||
// .receive("ok", (msg) => console.log("created message", msg) ) | ||
// .receive("error", (reasons) => console.log("create failed", reasons) ) | ||
// .after(10000, () => console.log("Networking issue. Still waiting...") ) | ||
// .receive("timeout", () => console.log("Networking issue...") ) | ||
// }) | ||
@@ -39,3 +39,3 @@ // channel.join() | ||
// .receive("error", ({reason}) => console.log("failed join", reason) ) | ||
// .after(10000, () => console.log("Networking issue. Still waiting...") ) | ||
// .receive("timeout", () => console.log("Networking issue. Still waiting...") ) | ||
// | ||
@@ -57,4 +57,4 @@ // | ||
// receive responses from the push. Additionally, we can use | ||
// `after(millsec, callback)` to abort waiting for our `receive` hooks and | ||
// take action after some period of waiting. | ||
// `receive("timeout", callback)` to abort waiting for our other `receive` hooks | ||
// and take action after some period of waiting. | ||
// | ||
@@ -94,2 +94,3 @@ // | ||
const SOCKET_STATES = {connecting: 0, open: 1, closing: 2, closed: 3} | ||
const DEFAULT_TIMEOUT = 10000 | ||
const CHANNEL_STATES = { | ||
@@ -120,4 +121,5 @@ closed: "closed", | ||
// payload - The payload, for example `{user_id: 123}` | ||
// timeout - The push timeout in milliseconds | ||
// | ||
constructor(channel, event, payload){ | ||
constructor(channel, event, payload, timeout){ | ||
this.channel = channel | ||
@@ -127,3 +129,4 @@ this.event = event | ||
this.receivedResp = null | ||
this.afterHook = null | ||
this.timeout = timeout | ||
this.timeoutTimer = null | ||
this.recHooks = [] | ||
@@ -133,16 +136,14 @@ this.sent = false | ||
send(){ | ||
const ref = this.channel.socket.makeRef() | ||
this.refEvent = this.channel.replyEventName(ref) | ||
resend(timeout){ | ||
this.timeout = timeout | ||
this.cancelRefEvent() | ||
this.ref = null | ||
this.refEvent = null | ||
this.receivedResp = null | ||
this.sent = false | ||
this.send() | ||
} | ||
this.channel.on(this.refEvent, payload => { | ||
this.receivedResp = payload | ||
this.matchReceive(payload) | ||
this.cancelRefEvent() | ||
this.cancelAfter() | ||
}) | ||
this.startAfter() | ||
send(){ if(this.hasReceived("timeout")){ return } | ||
this.startTimeout() | ||
this.sent = true | ||
@@ -153,3 +154,3 @@ this.channel.socket.push({ | ||
payload: this.payload, | ||
ref: ref | ||
ref: this.ref | ||
}) | ||
@@ -159,3 +160,3 @@ } | ||
receive(status, callback){ | ||
if(this.receivedResp && this.receivedResp.status === status){ | ||
if(this.hasReceived(status)){ | ||
callback(this.receivedResp.response) | ||
@@ -168,11 +169,3 @@ } | ||
after(ms, callback){ | ||
if(this.afterHook){ throw(`only a single after hook can be applied to a push`) } | ||
let timer = null | ||
if(this.sent){ timer = setTimeout(callback, ms) } | ||
this.afterHook = {ms: ms, callback: callback, timer: timer} | ||
return this | ||
} | ||
// private | ||
@@ -185,16 +178,34 @@ | ||
cancelRefEvent(){ this.channel.off(this.refEvent) } | ||
cancelRefEvent(){ if(!this.refEvent){ return } | ||
this.channel.off(this.refEvent) | ||
} | ||
cancelAfter(){ if(!this.afterHook){ return } | ||
clearTimeout(this.afterHook.timer) | ||
this.afterHook.timer = null | ||
cancelTimeout(){ | ||
clearTimeout(this.timeoutTimer) | ||
this.timeoutTimer = null | ||
} | ||
startAfter(){ if(!this.afterHook){ return } | ||
let callback = () => { | ||
startTimeout(){ if(this.timeoutTimer){ return } | ||
this.ref = this.channel.socket.makeRef() | ||
this.refEvent = this.channel.replyEventName(this.ref) | ||
this.channel.on(this.refEvent, payload => { | ||
this.cancelRefEvent() | ||
this.afterHook.callback() | ||
} | ||
this.afterHook.timer = setTimeout(callback, this.afterHook.ms) | ||
this.cancelTimeout() | ||
this.receivedResp = payload | ||
this.matchReceive(payload) | ||
}) | ||
this.timeoutTimer = setTimeout(() => { | ||
this.trigger("timeout", {}) | ||
}, this.timeout) | ||
} | ||
hasReceived(status){ | ||
return this.receivedResp && this.receivedResp.status === status | ||
} | ||
trigger(status, response){ | ||
this.channel.trigger(this.refEvent, {status, response}) | ||
} | ||
} | ||
@@ -209,4 +220,5 @@ | ||
this.bindings = [] | ||
this.timeout = this.socket.timeout | ||
this.joinedOnce = false | ||
this.joinPush = new Push(this, CHANNEL_EVENTS.join, this.params) | ||
this.joinPush = new Push(this, CHANNEL_EVENTS.join, this.params, this.timeout) | ||
this.pushBuffer = [] | ||
@@ -220,2 +232,4 @@ this.rejoinTimer = new Timer( | ||
this.rejoinTimer.reset() | ||
this.pushBuffer.forEach( pushEvent => pushEvent.send() ) | ||
this.pushBuffer = [] | ||
}) | ||
@@ -232,2 +246,9 @@ this.onClose( () => { | ||
}) | ||
this.joinPush.receive("timeout", () => { | ||
if(this.state !== CHANNEL_STATES.joining){ return } | ||
this.socket.log("channel", `timeout ${this.topic}`, this.joinPush.timeout) | ||
this.state = CHANNEL_STATES.errored | ||
this.rejoinTimer.setTimeout() | ||
}) | ||
this.on(CHANNEL_EVENTS.reply, (payload, ref) => { | ||
@@ -245,3 +266,3 @@ this.trigger(this.replyEventName(ref), payload) | ||
join(){ | ||
join(timeout = this.timeout){ | ||
if(this.joinedOnce){ | ||
@@ -252,3 +273,3 @@ throw(`tried to join multiple times. 'join' can only be called a single time per channel instance`) | ||
} | ||
this.sendJoin() | ||
this.rejoin(timeout) | ||
return this.joinPush | ||
@@ -269,10 +290,11 @@ } | ||
push(event, payload){ | ||
push(event, payload, timeout = this.timeout){ | ||
if(!this.joinedOnce){ | ||
throw(`tried to push '${event}' to '${this.topic}' before joining. Use channel.join() before pushing events`) | ||
} | ||
let pushEvent = new Push(this, event, payload) | ||
let pushEvent = new Push(this, event, payload, timeout) | ||
if(this.canPush()){ | ||
pushEvent.send() | ||
} else { | ||
pushEvent.startTimeout() | ||
this.pushBuffer.push(pushEvent) | ||
@@ -296,7 +318,14 @@ } | ||
// | ||
leave(){ | ||
return this.push(CHANNEL_EVENTS.leave).receive("ok", () => { | ||
leave(timeout = this.timeout){ | ||
let onClose = () => { | ||
this.socket.log("channel", `leave ${this.topic}`) | ||
this.trigger(CHANNEL_EVENTS.close, "leave") | ||
}) | ||
} | ||
let leavePush = new Push(this, CHANNEL_EVENTS.leave, {}, timeout) | ||
leavePush.receive("ok", () => onClose() ) | ||
.receive("timeout", () => onClose() ) | ||
leavePush.send() | ||
if(!this.canPush()){ leavePush.trigger("ok", {}) } | ||
return leavePush | ||
} | ||
@@ -313,12 +342,8 @@ | ||
sendJoin(){ | ||
sendJoin(timeout){ | ||
this.state = CHANNEL_STATES.joining | ||
this.joinPush.send() | ||
this.joinPush.resend(timeout) | ||
} | ||
rejoin(){ | ||
this.sendJoin() | ||
this.pushBuffer.forEach( pushEvent => pushEvent.send() ) | ||
this.pushBuffer = [] | ||
} | ||
rejoin(timeout = this.timeout){ this.sendJoin(timeout) } | ||
@@ -344,2 +369,4 @@ trigger(triggerEvent, payload, ref){ | ||
// Defaults to WebSocket with automatic LongPoll fallback. | ||
// timeout - The default timeout in milliseconds to trigger push timeouts. | ||
// Defaults `DEFAULT_TIMEOUT` | ||
// heartbeatIntervalMs - The millisec interval to send a heartbeat message | ||
@@ -368,6 +395,7 @@ // reconnectAfterMs - The optional function that returns the millsec | ||
this.ref = 0 | ||
this.timeout = opts.timeout || DEFAULT_TIMEOUT | ||
this.transport = opts.transport || window.WebSocket || LongPoll | ||
this.heartbeatIntervalMs = opts.heartbeatIntervalMs || 30000 | ||
this.reconnectAfterMs = opts.reconnectAfterMs || function(tries){ | ||
return [1000, 5000, 10000][tries - 1] || 10000 | ||
return [1000, 2000, 5000, 10000][tries - 1] || 10000 | ||
} | ||
@@ -503,3 +531,3 @@ this.logger = opts.logger || function(){} // noop | ||
sendHeartbeat(){ | ||
sendHeartbeat(){ if(!this.isConnected()){ return } | ||
this.push({topic: "phoenix", event: "heartbeat", payload: {}, ref: this.makeRef()}) | ||
@@ -506,0 +534,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
54799
1582