-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathindex.js
More file actions
139 lines (111 loc) · 3.44 KB
/
index.js
File metadata and controls
139 lines (111 loc) · 3.44 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import StimulusReflex from "stimulus_reflex"
import ReconnectingWebSocket from "reconnecting-websocket"
import CableReady from "cable_ready"
const extend = function(object, properties) {
if (properties != null) {
for (let key in properties) {
const value = properties[key]
object[key] = value
}
}
return object
}
class Subscription {
constructor(consumer, params = {}, mixin) {
this.consumer = consumer
this.identifier = JSON.stringify(params)
extend(this, mixin)
}
send(data) {
return this.consumer.send(data)
}
unsubscribe() {
return this.consumer.subscriptions.remove(this)
}
}
class Subscriptions {
constructor(consumer) {
this.consumer = consumer
this.subscriptions = []
}
findAll(identifier) {
return this.subscriptions.filter((s) => s.identifier === identifier)
}
add(subscription) {
this.subscriptions.push(subscription)
// below functionality is not implemented
// this.consumer.ensureActiveConnection()
// this.notify(subscription, "initialized")
// TODO we needo a subscribe command
// this.sendCommand(subscription, "subscribe")
return subscription
}
create(channelName, mixin) {
const channel = channelName
const params = typeof channel === "object" ? channel : {channel}
const subscription = new Subscription(this.consumer, params, mixin)
return this.add(subscription)
}
}
class Consumer {
constructor(url, options) {
this.connection = new ReconnectingWebSocket(url, [], options || {})
this.subscriptions = new Subscriptions(this)
}
send(data) {
this.connection.send(data)
}
}
const getConsumer = (url) => {
// read up on the default options that actioncable has for websockets.
let options = {
maxRetries: 3,
debug: true
}
return new Consumer(url, options)
}
const AbstractConsumerAdapter = StimulusReflex.getAbstractClass()
class WebsocketConsumer extends AbstractConsumerAdapter{
constructor(consumer) {
super(consumer)
this.consumer.connection.addEventListener("message", (event) => {
let data = JSON.parse(event.data)
if (!data.cableReady) return
if (!data.operations.morph || !data.operations.morph.length) return
const urls = Array.from(
new Set(data.operations.morph.map(m => m.stimulusReflex.url))
)
if (urls.length !== 1 || urls[0] !== (location.href)) return
CableReady.perform(data.operations)
})
this.consumer.connection.addEventListener("message", (event) => {
let data = JSON.parse(event.data)
if (data.meta_type !== "cookie") return
document.cookie = `${data.key}=${data.value||""}; max-age=${data.max_age}; path=/`
})
}
find_subscription(identifier) {
return this.consumer.subscriptions.findAll(identifier)[0]
}
create_subscription(channel) {
this.consumer.subscriptions.create(channel)
}
connect() {
return this.consumer.connection.open()
}
disconnect() {
return this.consumer.connection.close()
}
isConnected() {
return this.consumer.connection.readyState == ReconnectingWebSocket.OPEN
}
send(identifier, data, options) {
let subscription = this.find_subscription(identifier)
subscription.send(JSON.stringify(data))
// return this.consumer.connection.send(JSON.stringify(data))
}
}
export {
WebsocketConsumer,
getConsumer
}