2017-12-11 01:44:52 -05:00
|
|
|
//= require ./connection
|
|
|
|
//= require ./subscriptions
|
|
|
|
//= require ./subscription
|
2015-12-16 09:29:21 -05:00
|
|
|
|
2017-12-11 01:44:52 -05:00
|
|
|
// The ActionCable.Consumer establishes the connection to a server-side Ruby Connection object. Once established,
|
|
|
|
// the ActionCable.ConnectionMonitor will ensure that its properly maintained through heartbeats and checking for stale updates.
|
|
|
|
// The Consumer instance is also the gateway to establishing subscriptions to desired channels through the #createSubscription
|
|
|
|
// method.
|
|
|
|
//
|
|
|
|
// The following example shows how this can be setup:
|
|
|
|
//
|
Refactor decaffeinate output to more natural/idiomatic javascript
- Remove unnecessary Array.from usages from subscriptions.js
These were all Arrays before, so Array.from is a no-op
- Remove unnecessary IIFEs from subscriptions.js
- Manually decaffeinate sample ActionCable code in comments
Here the coffeescript -> ES2015 conversion was done by hand rather than
using decaffeinate, because these code samples were simple enough.
- Refactor ActionCable.Subscription to avoid initClass
- Refactor ActionCable.Subscription to use ES2015 default parameters
- Refactor ActionCable.ConnectionMonitor to avoid initClass
- Refactor ActionCable.ConnectionMonitor to use shorter variations of null checks
- Remove unnecessary code created because of implicit returns in ConnectionMonitor
This removes the `return` statements that were returning the value of
console.log and those from private methods whose return value was not
being used.
- Refactor ActionCable.Connection to avoid initClass
- Refactor Connection#isProtocolSupported and #isState
This addresses these three decaffeinate cleanup suggestions:
- DS101: Remove unnecessary use of Array.from
- DS104: Avoid inline assignments
- DS204: Change includes calls to have a more natural evaluation order
It also removes the use of Array.prototype.includes, which means we
don't have to worry about providing a polyfill or requiring that end
users provide one.
- Refactor ActionCable.Connection to use ES2015 default parameters
- Refactor ActionCable.Connection to use shorter variations of null checks
- Remove return statements that return the value of console.log() in ActionCable.Connection
- Simplify complex destructure assignment in connection.js
decaffeinate had inserted
```
adjustedLength = Math.max(protocols.length, 1)
```
to be safe, but we know that there has to always be at least one
protocol, so we don't have to worry about protocols.length being 0 here.
- Refactor Connection#getState
The decaffeinate translation of this method was not very clear, so we've
rewritten it to be more natural.
- Simplify destructure assignment in connection.js
- Remove unnecessary use of Array.from from action_cable.js.erb
- Refactor ActionCable#createConsumer and #getConfig
This addresses these two decaffeinate cleanup suggestions:
- DS104: Avoid inline assignments
- DS207: Consider shorter variations of null checks
- Remove unnecessary code created because of implicit returns in action_cable.js.erb
This removes the `return` statements that were returning the value of
console.log and those from methods that just set and unset the
`debugging` flag.
- Remove decaffeinate suggestion about avoiding top-level this
In this case, the top-level `this` is intentional, so it's okay to
ignore this suggestion.
- Remove decaffeinate suggestions about removing unnecessary returns
I did remove some of the return statements in previous commits, where
it seemed appropriate. However, the rest of these should probably remain
because the return values have been exposed through the public API. If
we want to break that contract, we can do so, but I think it should be
done deliberately as part of a breaking-API change (separate from this
coffeescript -> ES2015 conversion)
- Remove unused `unsupportedProtocol` variable from connection.js
Leaving this would cause eslint to fail
- Refactor Subscriptions methods to avoid `for` ... `of` syntax
Babel transpiles `for` ... `of` syntax to use `Symbol.iterator`, which
would require a polyfill in applications that support older browsers.
The `for` ... `of` syntax was produced by running `decaffeinate`, but in
these instances a simpler `map` should be sufficient and avoid any
`Symbol` issues.
2018-01-12 19:49:09 -05:00
|
|
|
// App = {}
|
|
|
|
// App.cable = ActionCable.createConsumer("ws://example.com/accounts/1")
|
|
|
|
// App.appearance = App.cable.subscriptions.create("AppearanceChannel")
|
2017-12-11 01:44:52 -05:00
|
|
|
//
|
|
|
|
// For more details on how you'd configure an actual channel subscription, see ActionCable.Subscription.
|
|
|
|
//
|
|
|
|
// When a consumer is created, it automatically connects with the server.
|
|
|
|
//
|
|
|
|
// To disconnect from the server, call
|
|
|
|
//
|
|
|
|
// App.cable.disconnect()
|
|
|
|
//
|
|
|
|
// and to restart the connection:
|
|
|
|
//
|
|
|
|
// App.cable.connect()
|
|
|
|
//
|
|
|
|
// Any channel subscriptions which existed prior to disconnecting will
|
|
|
|
// automatically resubscribe.
|
|
|
|
ActionCable.Consumer = class Consumer {
|
|
|
|
constructor(url) {
|
|
|
|
this.url = url
|
|
|
|
this.subscriptions = new ActionCable.Subscriptions(this)
|
|
|
|
this.connection = new ActionCable.Connection(this)
|
|
|
|
}
|
2015-12-16 09:29:21 -05:00
|
|
|
|
2017-12-11 01:44:52 -05:00
|
|
|
send(data) {
|
|
|
|
return this.connection.send(data)
|
|
|
|
}
|
2016-03-03 17:33:56 -05:00
|
|
|
|
2017-12-11 01:44:52 -05:00
|
|
|
connect() {
|
|
|
|
return this.connection.open()
|
|
|
|
}
|
2016-03-17 10:05:06 -04:00
|
|
|
|
2017-12-11 01:44:52 -05:00
|
|
|
disconnect() {
|
|
|
|
return this.connection.close({allowReconnect: false})
|
|
|
|
}
|
2016-03-17 10:05:06 -04:00
|
|
|
|
2017-12-11 01:44:52 -05:00
|
|
|
ensureActiveConnection() {
|
|
|
|
if (!this.connection.isActive()) {
|
|
|
|
return this.connection.open()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|