Simplify ActionCable.createWebSocketURL and realphabetize exports (#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
This commit is contained in:
rmacklin 2019-04-02 14:04:43 -07:00 committed by Kasper Timm Hansen
parent 2c4dab11d1
commit d03177ffbc
4 changed files with 17 additions and 12 deletions

View File

@ -477,15 +477,17 @@
return Consumer; return Consumer;
}(); }();
function createWebSocketURL(url) { function createWebSocketURL(url) {
var webSocketURL = typeof url === "function" ? url() : url; if (typeof url === "function") {
if (webSocketURL && !/^wss?:/i.test(webSocketURL)) { url = url();
}
if (url && !/^wss?:/i.test(url)) {
var a = document.createElement("a"); var a = document.createElement("a");
a.href = webSocketURL; a.href = url;
a.href = a.href; a.href = a.href;
a.protocol = a.protocol.replace("http", "ws"); a.protocol = a.protocol.replace("http", "ws");
return a.href; return a.href;
} else { } else {
return webSocketURL; return url;
} }
} }
function createConsumer() { function createConsumer() {
@ -505,8 +507,8 @@
exports.Subscription = Subscription; exports.Subscription = Subscription;
exports.Subscriptions = Subscriptions; exports.Subscriptions = Subscriptions;
exports.adapters = adapters; exports.adapters = adapters;
exports.logger = logger;
exports.createWebSocketURL = createWebSocketURL; exports.createWebSocketURL = createWebSocketURL;
exports.logger = logger;
exports.createConsumer = createConsumer; exports.createConsumer = createConsumer;
exports.getConfig = getConfig; exports.getConfig = getConfig;
Object.defineProperty(exports, "__esModule", { Object.defineProperty(exports, "__esModule", {

View File

@ -58,16 +58,18 @@ export default class Consumer {
} }
export function createWebSocketURL(url) { 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") const a = document.createElement("a")
a.href = webSocketURL a.href = url
// Fix populating Location properties in IE. Otherwise, protocol will be blank. // Fix populating Location properties in IE. Otherwise, protocol will be blank.
a.href = a.href a.href = a.href
a.protocol = a.protocol.replace("http", "ws") a.protocol = a.protocol.replace("http", "ws")
return a.href return a.href
} else { } else {
return webSocketURL return url
} }
} }

View File

@ -15,8 +15,8 @@ export {
Subscription, Subscription,
Subscriptions, Subscriptions,
adapters, adapters,
logger,
createWebSocketURL, createWebSocketURL,
logger,
} }
export function createConsumer(url = getConfig("url") || INTERNAL.default_mount_path) { export function createConsumer(url = getConfig("url") || INTERNAL.default_mount_path) {

View File

@ -42,14 +42,15 @@ module("ActionCable", () => {
assert.equal(consumer.url, testURL) assert.equal(consumer.url, testURL)
}) })
test("uses function to generate URL", assert => { test("dynamically computes URL from function", assert => {
let dynamicURL = testURL let dynamicURL = testURL
const generateURL = () => { const generateURL = () => {
return dynamicURL return dynamicURL
} }
const consumer = ActionCable.createConsumer(generateURL)
assert.equal(consumer.url, testURL)
dynamicURL = `${testURL}foo` dynamicURL = `${testURL}foo`
const consumer = ActionCable.createConsumer(generateURL)
assert.equal(consumer.url, `${testURL}foo`) assert.equal(consumer.url, `${testURL}foo`)
}) })
}) })