-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
stream_from_element.js
40 lines (36 loc) · 1.06 KB
/
stream_from_element.js
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
import CableReady from 'cable_ready'
import { consumer } from './action_cable'
class StreamFromElement extends HTMLElement {
connectedCallback () {
if (this.preview) return
if (consumer) {
this.channel = consumer.subscriptions.create(
{
channel: 'CableReady::Stream',
identifier: this.getAttribute('identifier')
},
{
received (data) {
if (data.cableReady) CableReady.perform(data.operations)
}
}
)
} else {
console.error(
'The `stream_from` helper cannot connect without an ActionCable consumer.\nPlease run `rails generate cable_ready:stream_from` to fix this.'
)
}
}
disconnectedCallback () {
if (this.channel) this.channel.unsubscribe()
}
get preview () {
return (
document.documentElement.hasAttribute('data-turbolinks-preview') ||
document.documentElement.hasAttribute('data-turbo-preview')
)
}
}
if (!window.customElements.get('stream-from')) {
window.customElements.define('stream-from', StreamFromElement)
}