diff options
4 files changed, 17 insertions, 12 deletions
diff --git a/actioncable/app/assets/javascripts/action_cable.js b/actioncable/app/assets/javascripts/action_cable.js index 029c7567ce..8349361405 100644 --- a/actioncable/app/assets/javascripts/action_cable.js +++ b/actioncable/app/assets/javascripts/action_cable.js @@ -477,15 +477,17 @@ return Consumer; }(); function createWebSocketURL(url) { - var webSocketURL = typeof url === "function" ? url() : url; - if (webSocketURL && !/^wss?:/i.test(webSocketURL)) { + if (typeof url === "function") { + url = url(); + } + if (url && !/^wss?:/i.test(url)) { var a = document.createElement("a"); - a.href = webSocketURL; + a.href = url; a.href = a.href; a.protocol = a.protocol.replace("http", "ws"); return a.href; } else { - return webSocketURL; + return url; } } function createConsumer() { @@ -505,8 +507,8 @@ exports.Subscription = Subscription; exports.Subscriptions = Subscriptions; exports.adapters = adapters; - exports.logger = logger; exports.createWebSocketURL = createWebSocketURL; + exports.logger = logger; exports.createConsumer = createConsumer; exports.getConfig = getConfig; Object.defineProperty(exports, "__esModule", { diff --git a/actioncable/app/javascript/action_cable/consumer.js b/actioncable/app/javascript/action_cable/consumer.js index 51f3b60980..e2e0dea8b5 100644 --- a/actioncable/app/javascript/action_cable/consumer.js +++ b/actioncable/app/javascript/action_cable/consumer.js @@ -58,16 +58,18 @@ export default class Consumer { } export function createWebSocketURL(url) { - const webSocketURL = typeof url === "function" ? url() : url + if (typeof url === "function") { + url = url() + } - if (webSocketURL && !/^wss?:/i.test(webSocketURL)) { + if (url && !/^wss?:/i.test(url)) { const a = document.createElement("a") - a.href = webSocketURL + a.href = url // 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 webSocketURL + return url } } diff --git a/actioncable/app/javascript/action_cable/index.js b/actioncable/app/javascript/action_cable/index.js index d484d99179..848b5631d6 100644 --- a/actioncable/app/javascript/action_cable/index.js +++ b/actioncable/app/javascript/action_cable/index.js @@ -15,8 +15,8 @@ export { Subscription, Subscriptions, adapters, - logger, createWebSocketURL, + logger, } export function createConsumer(url = getConfig("url") || INTERNAL.default_mount_path) { diff --git a/actioncable/test/javascript/src/unit/action_cable_test.js b/actioncable/test/javascript/src/unit/action_cable_test.js index 2181f955e3..c46f9878d2 100644 --- a/actioncable/test/javascript/src/unit/action_cable_test.js +++ b/actioncable/test/javascript/src/unit/action_cable_test.js @@ -42,14 +42,15 @@ module("ActionCable", () => { assert.equal(consumer.url, testURL) }) - test("uses function to generate URL", assert => { + test("dynamically computes URL from function", assert => { let dynamicURL = testURL const generateURL = () => { return dynamicURL } + const consumer = ActionCable.createConsumer(generateURL) + assert.equal(consumer.url, testURL) dynamicURL = `${testURL}foo` - const consumer = ActionCable.createConsumer(generateURL) assert.equal(consumer.url, `${testURL}foo`) }) }) |