-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws-client-global.js
174 lines (145 loc) · 3.13 KB
/
ws-client-global.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
class WsClient {
constructor() {
/**
* created for later, will be the
* variable holding the reference
* of the connection websocket.
*/
this.ws = null
/**
* created for later, will be the
* variable storing all the event
* functions by their names.
*/
this.events = {}
/**
* a list of functions executed
* when the onclose event is fired
*/
this.oncloseEvents = []
/**
* a list of `unique-id: Promise`
*/
this.thenables = {}
/**
* the counter used as a unique id in this.thenables
*/
this.counter = 0
this.hash = ''
}
/**
*
* @param {String} title
* @param {Function} fn
*/
on(title, fn) {
this.events[title] = fn
return this
}
/**
*
* @param {String} title
* @param {Function} fn
*/
onAnswer(title, fn) {
this.events[title + '-done'] = fn
}
/**
*
* @param {String} title
* @param {Object} obj
*/
emit(title, obj) {
const chosenEvent = this.events[title]
if (chosenEvent && typeof chosenEvent === 'function') {
chosenEvent(obj)
}
}
/**
*
* @param {Function} fn
*/
addOnclose(fn) {
this.oncloseEvents.push(fn)
}
/**
*
* @param {String} title
* @param {Object} message
* @param {Number} thenableId
*/
send(title, message, thenableId = undefined) {
this.ws.send(JSON.stringify({ title, message, thenableId, hash: this.hash }))
}
/**
* @returns {Number} new generated id
*/
generateNewId() {
this.counter = (this.counter + 1) % 1000
return this.counter
}
/**
*
* @param {Number} id
*/
hasThenable(id) {
return !!this.thenables[id]
}
/**
*
* @param {Number} id
* @param {Object} data
*/
consumeThenable(id, data = null) {
if (!this.hasThenable(id))
return
this.thenables[id](data)
delete this.thenables[id]
}
/**
*
* @param {String} title
* @param {Object} message
*/
thenable(title, message) {
const uniqueId = this.generateNewId()
const promise = new Promise(resolve => {
this.thenables[uniqueId] = resolve
})
this.send(title, message, uniqueId)
return promise
}
/**
*
* @param {Object} e
* @private
*/
_onmessage(e) {
const message = JSON.parse(e.data)
if (message.thenableId) {
this.consumeThenable(message.thenableId, message)
}
this.emit(message.title, message)
}
/**
*
* @param {String} address
*/
open(address = `${window.location.hostname}:${window.location.port}`) {
return new Promise((resolve, reject) => {
if (window.location.protocol === 'https:') {
this.ws = new WebSocket(`wss://${address}/ws`)
}
else {
this.ws = new WebSocket(`ws://${address}/ws`)
}
this.ws.onopen = _ => resolve()
this.ws.onclose = e =>
this.oncloseEvents.forEach(fn => fn())
this.ws.onmessage = this._onmessage.bind(this)
})
.then(() => this.thenable('__open__', {}))
.then(({ message }) => this.hash = message.hash)
}
}
window.WsClient = WsClient