forked from stimulusreflex/cable_ready
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream_from_controller.js
41 lines (37 loc) · 1.05 KB
/
stream_from_controller.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
41
import { Controller } from 'stimulus'
import CableReady from 'cable_ready'
export default class extends Controller {
static values = {
identifier: String,
sgid: String
}
connect () {
if (this.application.consumer && this.notPreview) {
this.channel = this.application.consumer.subscriptions.create(
{
channel: 'CableReady::Stream',
identifier: this.identifierValue,
sgid: this.sgidValue
},
{
received (data) {
if (data.cableReady) CableReady.perform(data.operations)
}
}
)
} else {
console.error(
`"stream-from" Stimulus controller cannot connect without an ActionCable consumer.\nPlease set 'application.consumer = consumer' in your index.js to use CableReady streaming.`
)
}
}
disconnect () {
this.channel.unsubscribe()
}
get notPreview () {
return (
!document.documentElement.hasAttribute('data-turbolinks-preview') &&
!document.documentElement.hasAttribute('data-turbo-preview')
)
}
}