From c7ca85ef31587e6ca02440a3cc0a8c4524c9a4d7 Mon Sep 17 00:00:00 2001 From: Ryan Castner Date: Thu, 7 Mar 2019 22:14:20 -0500 Subject: feat(js): Dynamic Actioncable WebSocket URL Allow createWebSocketURL fn to accept a function to generate the websocket URL rather than a string. --- actioncable/app/javascript/action_cable/index.js | 12 +++++++++--- actioncable/test/javascript/src/unit/action_cable_test.js | 8 ++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) (limited to 'actioncable') diff --git a/actioncable/app/javascript/action_cable/index.js b/actioncable/app/javascript/action_cable/index.js index 659418396f..1fd391a2c9 100644 --- a/actioncable/app/javascript/action_cable/index.js +++ b/actioncable/app/javascript/action_cable/index.js @@ -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 } } diff --git a/actioncable/test/javascript/src/unit/action_cable_test.js b/actioncable/test/javascript/src/unit/action_cable_test.js index 83426fa32e..c9d34abc6d 100644 --- a/actioncable/test/javascript/src/unit/action_cable_test.js +++ b/actioncable/test/javascript/src/unit/action_cable_test.js @@ -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) + }) }) }) -- cgit v1.2.3 From 9333f86adba465fc1cac0fce9a1399f609781f23 Mon Sep 17 00:00:00 2001 From: Ryan Castner Date: Sat, 9 Mar 2019 14:55:54 -0500 Subject: address pr feedback change if statement to ternary, use const for consistency, add spacing after ternary expression --- actioncable/app/javascript/action_cable/index.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'actioncable') diff --git a/actioncable/app/javascript/action_cable/index.js b/actioncable/app/javascript/action_cable/index.js index 1fd391a2c9..e9892ee821 100644 --- a/actioncable/app/javascript/action_cable/index.js +++ b/actioncable/app/javascript/action_cable/index.js @@ -30,12 +30,8 @@ export function getConfig(name) { } export function createWebSocketURL(url) { - let webSocketURL - if (typeof url === 'function') { - webSocketURL = url() - } else { - webSocketURL = url - } + const webSocketURL = typeof url === 'function' ? url() : url; + if (webSocketURL && !/^wss?:/i.test(webSocketURL)) { const a = document.createElement("a") a.href = webSocketURL -- cgit v1.2.3