aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable/app/javascript/action_cable/index.js.erb
blob: a5fb5b455628dafc9966b3eaa031aef665fa4456 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
 * decaffeinate suggestions:
 * DS101: Remove unnecessary use of Array.from
 * DS102: Remove unnecessary code created because of implicit returns
 * DS104: Avoid inline assignments
 * DS207: Consider shorter variations of null checks
 * DS208: Avoid top-level this
 * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
 */
//= export ActionCable
//= require_self
//= require ./action_cable/consumer

this.ActionCable = {
  INTERNAL: <%= ActionCable::INTERNAL.to_json %>,
  WebSocket: window.WebSocket,
  logger: window.console,

  createConsumer(url) {
    if (url == null) { let left
    url = (left = this.getConfig("url")) != null ? left : this.INTERNAL.default_mount_path }
    return new ActionCable.Consumer(this.createWebSocketURL(url))
  },

  getConfig(name) {
    const element = document.head.querySelector(`meta[name='action-cable-${name}']`)
    return (element != null ? element.getAttribute("content") : undefined)
  },

  createWebSocketURL(url) {
    if (url && !/^wss?:/i.test(url)) {
      const a = document.createElement("a")
      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 url
    }
  },

  startDebugging() {
    return this.debugging = true
  },

  stopDebugging() {
    return this.debugging = null
  },

  log(...messages) {
    if (this.debugging) {
      messages.push(Date.now())
      return this.logger.log("[ActionCable]", ...Array.from(messages))
    }
  }
}