-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathindex.js
More file actions
83 lines (69 loc) · 2.06 KB
/
index.js
File metadata and controls
83 lines (69 loc) · 2.06 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { Controller } from 'stimulus'
import StimulusReflex from "stimulus_reflex"
import { reflexControllerMethods, received } from "stimulus_reflex/javascript/reflexes"
import actionCable from 'stimulus_reflex/javascript/transports/action_cable'
import Debug from 'stimulus_reflex/javascript/debug'
import Deprecate from 'stimulus_reflex/javascript/deprecate'
import { WebsocketConsumer } from './websocket'
let globalConsumer
let params
// read up on the default options that actioncable has for websockets.
class StimulusReflexController extends Controller {
constructor (...args) {
super(...args)
register(this)
}
}
const createSubscription = controller => {
let consumer = globalConsumer || controller.application.consumer
const { channel } = controller.StimulusReflex
const subscription = { channel, ...params }
const identifier = JSON.stringify(subscription)
controller.StimulusReflex.subscription =
consumer.subscriptions.findAll(identifier)[0] ||
consumer.subscriptions.create(subscription, {
received: received,
connected: actionCable.connected,
// these are currently never called.
rejected: actionCable.rejected,
disconnected: actionCable.disconnected
})
}
const register = (controller, options = {}) => {
const channel = 'StimulusReflex::Channel'
controller.StimulusReflex = { ...options, channel }
createSubscription(controller)
Object.assign(controller, reflexControllerMethods)
}
const initialize = (application, {
controller = StimulusReflexController,
consumer,
debug,
params,
isolate,
deprecate
} = {}) => {
let options = {consumer, controller, debug, params, isolate, deprecate}
globalConsumer = consumer
StimulusReflex.initialize(application, options)
}
const Sockpuppet = {
initialize: initialize,
register: register,
get debug () {
return Debug.value
},
set debug (value) {
Debug.set(!!value)
},
get deprecate () {
return Deprecate.value
},
set deprecate (value) {
Deprecate.set(!!value)
}
}
export {
Sockpuppet
}
export default WebsocketConsumer