feat(js): Dynamic Actioncable WebSocket URL

Allow createWebSocketURL fn to accept a function to generate the websocket URL rather than a string.
This commit is contained in:
Ryan Castner 2019-03-07 22:14:20 -05:00
parent a62683f3e4
commit c7ca85ef31
2 changed files with 17 additions and 3 deletions

View File

@ -30,14 +30,20 @@ export function getConfig(name) {
}
export function createWebSocketURL(url) {
if (url && !/^wss?:/i.test(url)) {
let webSocketURL
if (typeof url === 'function') {
webSocketURL = url()
} else {
webSocketURL = url
}
if (webSocketURL && !/^wss?:/i.test(webSocketURL)) {
const a = document.createElement("a")
a.href = url
a.href = webSocketURL
// Fix populating Location properties in IE. Otherwise, protocol will be blank.
a.href = a.href
a.protocol = a.protocol.replace("http", "ws")
return a.href
} else {
return url
return webSocketURL
}
}

View File

@ -41,5 +41,13 @@ module("ActionCable", () => {
assert.equal(consumer.url, testURL)
})
test("uses function to generate URL", assert => {
const generateURL = () => {
return testURL
}
const consumer = ActionCable.createConsumer(generateURL)
assert.equal(consumer.url, testURL)
})
})
})