-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
futurism_channel.js
51 lines (46 loc) · 1.25 KB
/
futurism_channel.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
42
43
44
45
46
47
48
49
50
51
/* global CustomEvent, setTimeout */
import CableReady from 'cable_ready'
const debounceEvents = (callback, delay = 20) => {
let timeoutId
let events = []
return (...args) => {
clearTimeout(timeoutId)
events = [...events, ...args]
timeoutId = setTimeout(() => {
timeoutId = null
callback(events)
events = []
}, delay)
}
}
export const createSubscription = consumer => {
consumer.subscriptions.create('Futurism::Channel', {
connected() {
window.Futurism = this
document.addEventListener(
'futurism:appear',
debounceEvents(events => {
this.send({
signed_params: events.map(e => e.target.dataset.signedParams),
sgids: events.map(e => e.target.dataset.sgid),
signed_controllers: events.map(e => e.target.dataset.signedController),
urls: events.map(_ => window.location.href)
})
})
)
},
received(data) {
if (data.cableReady) {
CableReady.perform(data.operations, {
emitMissingElementWarnings: false
})
document.dispatchEvent(
new CustomEvent('futurism:appeared', {
bubbles: true,
cancelable: true
})
)
}
}
})
}