-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathconnectionSlice.ts
More file actions
175 lines (165 loc) · 5.65 KB
/
connectionSlice.ts
File metadata and controls
175 lines (165 loc) · 5.65 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
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
import { createSlice, type PayloadAction } from "@reduxjs/toolkit"
import { CONNECTED, CONNECTING, ERROR, LOCAL_STORAGE, NOT_CONNECTED, VALKEY, DISCONNECTED } from "@common/src/constants.ts"
import * as R from "ramda"
type ConnectionStatus = typeof NOT_CONNECTED | typeof CONNECTED | typeof CONNECTING | typeof ERROR | typeof DISCONNECTED;
type Role = "primary" | "replica";
interface ConnectionDetails {
host: string;
port: string;
username: string;
password: string;
role?: Role;
clusterId?: string;
}
interface ReconnectState {
isRetrying: boolean;
currentAttempt: number;
maxRetries: number;
nextRetryDelay?: number;
}
export interface ConnectionState {
status: ConnectionStatus;
errorMessage: string | null;
connectionDetails: ConnectionDetails;
clusterNodes?: Record<string, ConnectionDetails>;
reconnect?: ReconnectState;
}
interface ValkeyConnectionsState {
[connectionId: string]: ConnectionState
}
const currentConnections = R.pipe(
(v: string) => localStorage.getItem(v),
(s) => (s === null ? {} : JSON.parse(s)),
)(LOCAL_STORAGE.VALKEY_CONNECTIONS)
const connectionSlice = createSlice({
name: VALKEY.CONNECTION.name,
initialState: {
connections: currentConnections as ValkeyConnectionsState,
},
reducers: {
connectPending: (
state,
action: PayloadAction<{
connectionId: string;
host: string;
port: string;
username?: string;
password?: string;
isRetry?: boolean;
}>,
) => {
const { connectionId, host, port, username = "", password = "", isRetry = false } = action.payload
const existingConnection = state.connections[connectionId]
state.connections[connectionId] = {
status: CONNECTING,
errorMessage: isRetry && existingConnection?.errorMessage ? existingConnection.errorMessage : null,
connectionDetails: { host, port, username, password },
...(existingConnection?.clusterNodes && { clusterNodes: existingConnection.clusterNodes }),
...(isRetry && existingConnection?.reconnect && {
reconnect: existingConnection.reconnect,
}),
}
},
standaloneConnectFulfilled: (
state,
action: PayloadAction<{
connectionId: string;
connectionDetails: { host: string; port: number};
}>,
) => {
const { connectionId } = action.payload
const connectionState = state.connections[connectionId]
if (connectionState) {
connectionState.status = CONNECTED
connectionState.errorMessage = null
}
},
clusterConnectFulfilled: (
state,
action: PayloadAction<{
connectionId: string;
clusterNodes: Record<string, ConnectionDetails>;
clusterId: string;
}>,
) => {
const { connectionId, clusterNodes, clusterId } = action.payload
const connectionState = state.connections[connectionId]
if (connectionState) {
connectionState.status = CONNECTED
connectionState.errorMessage = null
connectionState.clusterNodes = clusterNodes
connectionState.connectionDetails.clusterId = clusterId
// Clear retry state on successful connection
delete connectionState.reconnect
}
},
connectRejected: (state, action) => {
const { connectionId, errorMessage } = action.payload
if (state.connections[connectionId]) {
const existingConnection = state.connections[connectionId]
const isRetrying = existingConnection.reconnect?.isRetrying
state.connections[connectionId].status = ERROR
// Preserve original error message during retry attempts
if (isRetrying && existingConnection.errorMessage) {
state.connections[connectionId].errorMessage = existingConnection.errorMessage
} else {
state.connections[connectionId].errorMessage = errorMessage || "Valkey error"
}
}
},
startRetry: (state, action) => {
const { connectionId, attempt, maxRetries, nextRetryDelay } = action.payload
if (state.connections[connectionId]) {
state.connections[connectionId].reconnect = {
isRetrying: true,
currentAttempt: attempt,
maxRetries,
nextRetryDelay,
}
}
},
stopRetry: (state, action) => {
const { connectionId } = action.payload
if (state.connections[connectionId]?.reconnect) {
state.connections[connectionId].reconnect!.isRetrying = false
}
},
connectionBroken: (state, action) => {
const { connectionId } = action.payload
if (state.connections[connectionId]) {
state.connections[connectionId].status = DISCONNECTED
state.connections[connectionId].errorMessage = "Connection lost"
}
},
closeConnection: (state, action) => {
console.log(action)
const { connectionId } = action.payload
state.connections[connectionId].status = NOT_CONNECTED
state.connections[connectionId].errorMessage = null
},
updateConnectionDetails: (state, action) => {
const { connectionId } = action.payload
state.connections[connectionId].connectionDetails = {
...state.connections[connectionId].connectionDetails,
...action.payload,
}
},
deleteConnection: (state, action) => {
const { connectionId } = action.payload
return R.dissocPath(["connections", connectionId], state)
},
},
})
export default connectionSlice.reducer
export const {
connectPending,
standaloneConnectFulfilled,
clusterConnectFulfilled,
connectRejected,
connectionBroken,
closeConnection,
updateConnectionDetails,
deleteConnection,
startRetry,
stopRetry,
} = connectionSlice.actions