This repository has been archived by the owner on May 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
socketManager.js
240 lines (199 loc) · 6.94 KB
/
socketManager.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import { store } from './index'
import io from 'socket.io-client'
const storedConnections = {}
/**
* Returns current store state
* Returns empty object if store isn't available yet
*
* @return {Object} store state
*/
function getState () {
let result = {}
if ( store )
result = store.getState()
return result
}
/**
* Creates new connection object
*
* @param {String} id unique id to keep track of the socket with
*/
function createNewConnection (id) {
storedConnections[id] = {
url: '',
socket: null,
events: []
}
tryToSubscribeOrRetry(id)
}
/**
* Subscribes a listener to the store for a specific subscribed socket (the listener will update the socked based on changes made in the app)
* Retries automaticaly of store in unavailable
*
* @param {String} id unique id of the socket
*/
function tryToSubscribeOrRetry (id) {
if ( store )
storedConnections[id].unsubscribe = store.subscribe(listenForChanges.bind(id))
else
waitAndRetry(id)
}
function removeConnection (id) {
const connection = storedConnections[id]
if ( connection.socket )
connection.socket.disconnect()
if ( connection.unsubscribe )
connection.unsubscribe()
}
export { createNewConnection, removeConnection }
/**
* Creates the socket.io connection and listens for changes made in the store (by actions in the app by the user)
* and updates the socket accordingly
*
* - Creates socket.io connection
* - Adds and removes events the socket should listen to
* - Updates socket url
*/
function listenForChanges () {
const state = getState()
const id = this
const storedConnection = storedConnections[id]
const connection = state.connections.list[state.connections.connections[id].index]
const url = connection.url
let namespace = connection.namespace || ''
if ( url !== storedConnections[id].url || namespace !== storedConnections[id].namespace) {
storedConnections[id].url = url
storedConnections[id].namespace = namespace
let socket = storedConnection.socket
if ( socket /*&& socket.connected*/ ) { // this means the url was changed, previous connection gets removed and a new one is made
socket.disconnect()
// TODO all events that were added previously need to be added to the new socket
}
if ( url ) {
const parsedURL = new URL(url);
const path = parsedURL.pathname === '/' ? '/socket.io' : parsedURL.pathname
socket = io(parsedURL.origin + namespace, {path});
storedConnections[id].socket = socket
socket.on('connect', function () {
store.dispatch({type: 'REMOVE_ALL_MESSAGES'})
store.dispatch({type: 'REMOVE_ALL_SENTMESSAGES'})
store.dispatch({type: 'SET_CONNECTED', id})
})
socket.on('disconnect', function () {
store.dispatch({type: 'SET_DISCONNECTED', id})
})
}
}
// add or remove events
const differenceInEvents = compareLists(connection.events, storedConnection.events)
// socket should listen to the events in the generated list: differenceInEvents
if ( differenceInEvents.length ) {
const socket = storedConnection.socket
storedConnections[id].events = connection.events.slice()
differenceInEvents.forEach(event => socket.on(event.name, messageHandler.bind(socket, id, event.name)))
} else {
const invertedDifferenceInEvents = compareLists(storedConnection.events, connection.events)
// socket should stop listening to the events in the generated list: invertedDifferenceInEvents
if ( invertedDifferenceInEvents.length ) {
const socket = storedConnection.socket
storedConnections[id].events = connection.events.slice()
invertedDifferenceInEvents.forEach(event => socket.off(event.name))
}
}
}
/**
* Store a message received on a socket event
*
* @param {String} id unique socket id
* @param {String} eventName name of the event the message was received on
* @param {Mixed} message received messagem can be multiple arguments
*/
function messageHandler (id, eventName/*, message arguments*/) {
store.dispatch({type: 'ADD_MESSAGE', id, eventName, message: [].slice.call(arguments, 2)})
}
/**
* Returns difference between 2 arrays of socket events
*
* @param {Array} list1 array of events
* @param {Array} list2 array of events
*
* @return {Array} list of events not included in 1st array
*/
function compareLists (list1, list2) {
const difference = []
let event
let exists
for ( let x = 0, l = list1.length; x < l; x++ ) {
event = list1[x]
exists = false
for ( let y = 0, l = list2.length; y < l; y++ ) {
if ( event.name === list2[y].name )
exists = true
}
if ( !exists )
difference.push(event)
}
return difference
}
/**
* Waits 10ms before retrying tryToSubscribeOrRetry()
*
* @param {String} id unique socket id
*/
function waitAndRetry (id) {
setTimeout(function () {
tryToSubscribeOrRetry(id)
}, 10)
}
/**
* Call subscribeSendMessageListener() if store is available, retry in 10ms if it's not
*/
function retryLater () {
setTimeout(function () {
if ( store )
subscribeSendMessageListener()
else
retryLater()
}, 10)
}
/**
* Listens to the store to determine when a subscribed socket has to emit a message
*/
function subscribeSendMessageListener () {
let previousState = store.getState().sentMessages
store.subscribe(function () {
const state = store.getState()
const sentMessages = state.sentMessages
if ( previousState.length < sentMessages.length ) {
previousState = sentMessages
const newMessage = sentMessages.slice(-1)[0]
const connection = storedConnections[newMessage.socketId]
const socket = connection.socket
socket.emit( newMessage.eventName, ...newMessage.message.map( m => {
const value = m.value
switch ( m.type ) {
case 'String':
case 'JSON':
return value
case 'Boolean':
return value === 'true'
case 'Object':
case 'Array':
let evalResult
eval(`evalResult = ${value}`)
return evalResult
case 'Number':
return ~~value
}
} ) )
} else if (previousState.length > sentMessages.length) {
// update the record of sent messages when queue is cleared
previousState = store.getState().sentMessages
}
})
}
// initialise sendMessageListener
if ( store )
subscribeSendMessageListener()
else
retryLater()