Skip to content

Commit

Permalink
Simplify ActionCable.createWebSocketURL and realphabetize exports (ra…
Browse files Browse the repository at this point in the history
…ils#35810)

* Remove unnecessary variable from ActionCable.createWebSocketURL

* Improve ActionCable test by creating the Consumer before reassigning URL

With this change, the test now actually verifies that the Consumer's url
property changes dynamically (from testURL to `${testURL}foo`).

* Fix alphabetization of ActionCable exports
  • Loading branch information
rmacklin authored and kaspth committed Apr 2, 2019
1 parent 2c4dab1 commit d03177f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
12 changes: 7 additions & 5 deletions actioncable/app/assets/javascripts/action_cable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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", {
Expand Down
10 changes: 6 additions & 4 deletions actioncable/app/javascript/action_cable/consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion actioncable/app/javascript/action_cable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export {
Subscription,
Subscriptions,
adapters,
logger,
createWebSocketURL,
logger,
}

export function createConsumer(url = getConfig("url") || INTERNAL.default_mount_path) {
Expand Down
5 changes: 3 additions & 2 deletions actioncable/test/javascript/src/unit/action_cable_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
})
})
Expand Down

0 comments on commit d03177f

Please sign in to comment.