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
/
connections.js
249 lines (193 loc) · 5.49 KB
/
connections.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
241
242
243
244
245
246
247
248
249
const defaultState = {
connections: {},
list: []
}
/* eslint-disable complexity */
export default function connections (state = defaultState, action) {
switch (action.type) {
case 'ADD_CONNECTION':
return addConnection(state, action)
case 'REMOVE_CONNECTION':
return removeConnection(state, action)
case 'SET_NAMESPACE_AND_URL':
return setNamespaceAndUrl(state, action)
case 'SET_CONNECTED':
return setConnected(state, action, true)
case 'SET_DISCONNECTED':
return setConnected(state, action, false)
case 'ADD_EVENT':
return addEvent(state, action)
case 'REMOVE_EVENT':
return removeEvent(state, action)
case 'TOGGLE_EVENT_VISIBILITY':
return toggleEventVisibility(state, action)
case 'SET_SETTABORDER':
return changeTabOrders(state, action)
case 'SET_EVENT_COLOR':
return setEventColor(state, action)
default:
return state
}
}
/* eslint-enable complexity */
/**
* Adds connection object to the store (this is where the information on a socket.io connection is kept)
*/
function addConnection (state, action) {
const connections = {...state.connections}
const id = action.id
const newConnection = {
index: state.list.length,
id
}
connections[id] = newConnection
const list = state.list.slice()
list.push({
url: '',
id,
disabled: false,
connected: false,
events: [],
order: list.length + 1
})
return {
connections,
list
}
}
/**
* Removes a connection from the list
*/
function removeConnection (state, action) {
const connection = state.connections[action.id]
const list = state.list.slice()
list[connection.index].disabled = true
return {
connections: state.connections,
list
}
}
/**
* Updates the url of a connection
*/
function setNamespaceAndUrl (state, action) {
const list = state.list.slice()
const id = action.id
// set namespace
if(action.namespace) {
const namespaceWithLeadingSlash = action.namespace.charAt(0) === '/' ? action.namespace : '/' + action.namespace
list[state.connections[id].index].namespace = namespaceWithLeadingSlash
} else {
list[state.connections[id].index].namespace = ''
}
// set URL
list[state.connections[id].index].url = action.url
return {
connections: state.connections,
list
}
}
/**
* Updates a connection 'connected' status to newValue
*/
function setConnected (state, action, newValue) {
const list = state.list.slice()
const id = action.id
list[state.connections[id].index].connected = newValue
if(!newValue) {
list[state.connections[id].index].events = []
}
return {
connections: state.connections,
list
}
}
/**
* Adds an event that a connection should listen to
*/
function addEvent (state, action) {
const list = state.list.slice()
const id = action.id
list[state.connections[id].index].events.push(action.event)
return {
connections: state.connections,
list
}
}
/**
* Removes an event that a connection should listen to
*/
function removeEvent (state, action) {
const list = state.list.map(t => ({...t}))
const id = action.id
const eventName = action.eventName
const connection = list[state.connections[id].index]
const events = connection.events
connection.events = events.filter( event => event.name !== eventName )
list[state.connections[id].index] = connection
return {
connections: state.connections,
list
}
}
/**
* Sets the event to hidden, messages will stil be received but not shown
*/
function toggleEventVisibility (state, action) {
const list = state.list.slice()
const id = action.id
const events = list[state.connections[id].index].events.slice()
const eventName = action.eventName
for ( let x = 0, l = events.length; x < l; x++ )
if ( events[x].name === eventName )
events[x].visible = !events[x].visible
list[state.connections[id].index].events = events
return {
connections: state.connections,
list
}
}
/**
* Change the order in which the tabs at the top are shown
*/
function changeTabOrders (state, action) {
const list = state.list.map(t => ({...t}))
const originalOrder = list[state.connections[action.id].index].order
const newOrder = action.order
let order
const newList = list.map(tab => {
order = tab.order
if ( tab.id === action.id )
tab.order = newOrder
else {
if ( originalOrder < newOrder ) {
if ( order > originalOrder && order <= newOrder )
tab.order--
} else if ( order < originalOrder && order >= newOrder )
tab.order++
}
return tab
})
return {
connections: state.connections,
list: newList
}
}
/**
* Sets the event color
*/
function setEventColor (state, action) {
const list = state.list.map(t => ({...t}))
const id = action.id
const connection = list[state.connections[id].index]
const eventName = action.eventName
connection.events = connection.events.map(e => {
if ( e.name === eventName )
e.color = action.color
return e
})
return {
connections: state.connections,
list
}
}