Skip to content

Commit

Permalink
Accept function for socket and channel params. Closes phoenixframewor…
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismccord committed May 30, 2018
1 parent 8fcb7c1 commit 379a059
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 49 deletions.
53 changes: 21 additions & 32 deletions assets/js/phoenix.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,16 @@ const TRANSPORTS = {
websocket: "websocket"
}

// wraps value in closure or returns closure
let closure = (value) => {
if(typeof(value) === "function"){
return value
} else {
let closure = function(){ return value }
return closure
}
}

/**
* Initializes the Push
* @param {Channel} channel - The Channel
Expand All @@ -227,7 +237,7 @@ class Push {
constructor(channel, event, payload, timeout){
this.channel = channel
this.event = event
this.payload = payload || {}
this.payload = payload || function(){ return {} }
this.receivedResp = null
this.timeout = timeout
this.timeoutTimer = null
Expand All @@ -254,7 +264,7 @@ class Push {
this.channel.socket.push({
topic: this.channel.topic,
event: this.event,
payload: this.payload,
payload: this.payload(),
ref: this.ref,
join_ref: this.channel.joinRef()
})
Expand All @@ -274,16 +284,6 @@ class Push {
return this
}

/**
* Updates the Push payload for subsequent resends
*
* @param {Object} payload
* @returns {Push}
*/
updatePayload(payload) {
this.payload = payload;
}

/**
* @private
*/
Expand Down Expand Up @@ -355,14 +355,14 @@ class Push {
/**
*
* @param {string} topic
* @param {Object} params
* @param {(Object|function)} params
* @param {Socket} socket
*/
export class Channel {
constructor(topic, params, socket) {
this.state = CHANNEL_STATES.closed
this.topic = topic
this.params = params || {}
this.params = closure(params || {})
this.socket = socket
this.bindings = []
this.bindingRef = 0
Expand Down Expand Up @@ -393,7 +393,7 @@ export class Channel {
})
this.joinPush.receive("timeout", () => { if(!this.isJoining()){ return }
this.socket.log("channel", `timeout ${this.topic} (${this.joinRef()})`, this.joinPush.timeout)
let leavePush = new Push(this, CHANNEL_EVENTS.leave, {}, this.timeout)
let leavePush = new Push(this, CHANNEL_EVENTS.leave, closure({}), this.timeout)
leavePush.send()
this.state = CHANNEL_STATES.errored
this.joinPush.reset()
Expand Down Expand Up @@ -429,17 +429,6 @@ export class Channel {
}
}

/**
* Updates the params passed as the second argument to `new Channel("topic", params, socket)`
* Any subsequent reconnects on the channel will send the updated params to the `join` callback on the sever
*
* @param {Object} params
*/
updateJoinParams(params = {}) {
this.params = params;
this.joinPush.updatePayload(params);
}

/**
* Hook into channel close
* @param {Function} callback
Expand Down Expand Up @@ -504,7 +493,7 @@ export class Channel {
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, timeout)
let pushEvent = new Push(this, event, function(){ return payload }, timeout)
if(this.canPush()){
pushEvent.send()
} else {
Expand Down Expand Up @@ -537,7 +526,7 @@ export class Channel {
this.socket.log("channel", `leave ${this.topic}`)
this.trigger(CHANNEL_EVENTS.close, "leave")
}
let leavePush = new Push(this, CHANNEL_EVENTS.leave, {}, timeout)
let leavePush = new Push(this, CHANNEL_EVENTS.leave, closure({}), timeout)
leavePush.receive("ok", () => onClose() )
.receive("timeout", () => onClose() )
leavePush.send()
Expand Down Expand Up @@ -705,7 +694,7 @@ const Serializer = {
*
* Defaults to 20s (double the server long poll timer).
*
* @param {Object} [opts.params] - The optional params to pass when connecting
* @param {{Object|function)} [opts.params] - The optional params to pass when connecting
*
*
*/
Expand All @@ -732,7 +721,7 @@ export class Socket {
}
this.logger = opts.logger || function(){} // noop
this.longpollerTimeout = opts.longpollerTimeout || 20000
this.params = opts.params || {}
this.params = closure(opts.params || {})
this.endPoint = `${endPoint}/${TRANSPORTS.websocket}`
this.heartbeatTimer = null
this.pendingHeartbeatRef = null
Expand All @@ -755,7 +744,7 @@ export class Socket {
*/
endPointURL(){
let uri = Ajax.appendParams(
Ajax.appendParams(this.endPoint, this.params), {vsn: VSN})
Ajax.appendParams(this.endPoint, this.params()), {vsn: VSN})
if(uri.charAt(0) !== "/"){ return uri }
if(uri.charAt(1) === "/"){ return `${this.protocol()}:${uri}` }

Expand Down Expand Up @@ -783,7 +772,7 @@ export class Socket {
connect(params){
if(params){
console && console.log("passing params to connect is deprecated. Instead pass :params to the Socket constructor")
this.params = params
this.params = closure(params)
}
if(this.conn){ return }

Expand Down
60 changes: 60 additions & 0 deletions assets/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 27 additions & 14 deletions assets/test/channel_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,60 @@ describe("constructor", () => {

assert.equal(channel.state, "closed")
assert.equal(channel.topic, "topic")
assert.deepEqual(channel.params, { one: "two" })
assert.deepEqual(channel.params(), { one: "two" })
assert.deepEqual(channel.socket, socket)
assert.equal(channel.timeout, 1234)
assert.equal(channel.joinedOnce, false)
assert.ok(channel.joinPush)
assert.deepEqual(channel.pushBuffer, [])
})

it("sets up joinPush object", () => {
it("sets up joinPush objec with literal params", () => {
channel = new Channel("topic", { one: "two" }, socket)
const joinPush = channel.joinPush

assert.deepEqual(joinPush.channel, channel)
assert.deepEqual(joinPush.payload, { one: "two" })
assert.deepEqual(joinPush.payload(), { one: "two" })
assert.equal(joinPush.event, "phx_join")
assert.equal(joinPush.timeout, 1234)
})

it("sets up joinPush objec with closure params", () => {
channel = new Channel("topic", function(){ return({one: "two"}) }, socket)
const joinPush = channel.joinPush

assert.deepEqual(joinPush.channel, channel)
assert.deepEqual(joinPush.payload(), { one: "two" })
assert.equal(joinPush.event, "phx_join")
assert.equal(joinPush.timeout, 1234)
})

})

describe("updateJoinParams", () => {
describe("updating join params", () => {
beforeEach(() => {
socket = { timeout: 1234 }
})

it("can update the join params", () => {
channel = new Channel("topic", { one: "two" }, socket)
let counter = 0
let params = function(){ return({value: counter}) }

channel = new Channel("topic", params, socket)
const joinPush = channel.joinPush

assert.deepEqual(joinPush.channel, channel)
assert.deepEqual(joinPush.payload, { one: "two" })
assert.deepEqual(joinPush.payload(), { value: 0 })
assert.equal(joinPush.event, "phx_join")
assert.equal(joinPush.timeout, 1234)

channel.updateJoinParams({ three: "four" })
counter++

assert.deepEqual(joinPush.channel, channel)
assert.deepEqual(joinPush.payload, { three: "four" })
assert.deepEqual(channel.params, { three: "four" })
assert.deepEqual(joinPush.payload(), { value: 1 })
assert.deepEqual(channel.params(), { value: 1 })
assert.equal(joinPush.event, "phx_join")
assert.equal(joinPush.timeout, 1234)

});
});

Expand Down Expand Up @@ -759,7 +772,7 @@ describe("on", () => {

assert.ok(!ignoredSpy.called)
})

it("generates unique refs for callbacks", () => {
const ref1 = channel.on("event1", () => 0)
const ref2 = channel.on("event2", () => 0)
Expand Down Expand Up @@ -793,19 +806,19 @@ describe("off", () => {
assert.ok(!spy2.called)
assert.ok(spy3.called)
})

it("removes callback by its ref", () => {
const spy1 = sinon.spy()
const spy2 = sinon.spy()

const ref1 = channel.on("event", spy1)
const ref2 = channel.on("event", spy2)

channel.off("event", ref1)
channel.trigger("event", {}, defaultRef)

assert.ok(!spy1.called)
assert.ok(spy2.called)
assert.ok(spy2.called)
})
})

Expand Down
12 changes: 10 additions & 2 deletions assets/test/socket_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ describe("constructor", () => {
assert.equal(typeof socket.reconnectAfterMs, "function")
})

it("supports closure or literal params", () => {
socket = new Socket("/socket", {params: {one: "two"}})
assert.deepEqual(socket.params(), {one: "two"})

socket = new Socket("/socket", {params: function(){ return({three: "four"}) }})
assert.deepEqual(socket.params(), {three: "four"})
})

it("overrides some defaults with options", () => {
const customTransport = function transport() {}
const customLogger = function logger() {}
Expand All @@ -54,7 +62,7 @@ describe("constructor", () => {
assert.equal(socket.transport, customTransport)
assert.equal(socket.logger, customLogger)
assert.equal(socket.reconnectAfterMs, customReconnect)
assert.deepEqual(socket.params, {one: "two"})
assert.deepEqual(socket.params(), {one: "two"})
})

describe("with Websocket", () => {
Expand Down Expand Up @@ -370,7 +378,7 @@ describe("channel", () => {

assert.deepStrictEqual(channel.socket, socket)
assert.equal(channel.topic, "topic")
assert.deepEqual(channel.params, {one: "two"})
assert.deepEqual(channel.params(), {one: "two"})
})

it("adds channel to sockets channels list", () => {
Expand Down
Loading

0 comments on commit 379a059

Please sign in to comment.