Skip to content

Commit 951f18c

Browse files
committed
extracted action handlers and clean up
1 parent 3a73cd7 commit 951f18c

File tree

8 files changed

+321
-146
lines changed

8 files changed

+321
-146
lines changed

apps/server/src/actions/cluster.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { GlideClusterClient } from "@valkey/valkey-glide"
2+
import { type Deps, withDeps } from "./utils.ts"
3+
import { setClusterDashboardData } from "../setDashboardData.ts"
4+
5+
export const setClusterData = withDeps<Deps, void>(
6+
async ({ ws, clients, connectionId, action }) => {
7+
const client = clients.get(connectionId)
8+
9+
if (client instanceof GlideClusterClient) {
10+
const { clusterId } = action.payload as unknown as { clusterId: string }
11+
await setClusterDashboardData(clusterId, client, ws)
12+
}
13+
},
14+
)

apps/server/src/actions/command.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { sendValkeyRunCommand } from "../sendCommand.ts"
2+
import { VALKEY } from "../../../../common/src/constants.ts"
3+
import { type Deps, withDeps } from "./utils.ts"
4+
5+
type CommandAction = {
6+
command: string
7+
connectionId: string
8+
}
9+
10+
export const sendRequested = withDeps<Deps, void>(
11+
async ({ ws, clients, connectionId, action }) => {
12+
const client = clients.get(connectionId!)
13+
14+
if (client) {
15+
await sendValkeyRunCommand(client, ws, action.payload as CommandAction)
16+
return
17+
}
18+
19+
ws.send(
20+
JSON.stringify({
21+
type: VALKEY.COMMAND.sendFailed,
22+
payload: {
23+
error: "Invalid connection Id",
24+
},
25+
}),
26+
)
27+
},
28+
)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { GlideClusterClient } from "@valkey/valkey-glide"
2+
import { connectToValkey } from "../connection.ts"
3+
import { type Deps, withDeps } from "./utils.ts"
4+
import { setClusterDashboardData } from "../setDashboardData.ts"
5+
6+
type ConnectPayload = {
7+
host: string
8+
port: number
9+
connectionId: string
10+
}
11+
12+
export const connectPending = withDeps<Deps, void>(
13+
async ({ ws, clients, action }) => {
14+
await connectToValkey(ws, action.payload as ConnectPayload, clients)
15+
},
16+
)
17+
18+
export const resetConnection = withDeps<Deps, void>(
19+
async ({ ws, connectionId, clients, action }) => {
20+
const client = clients.get(connectionId)
21+
22+
const { clusterId } = action.payload as unknown as { clusterId: string }
23+
24+
if (client instanceof GlideClusterClient) {
25+
await setClusterDashboardData(clusterId, client, ws)
26+
}
27+
},
28+
)

apps/server/src/actions/keys.ts

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import { addKey, deleteKey, getKeyInfoSingle, getKeys, updateKey } from "../keys-browser.ts"
2+
import { VALKEY } from "../../../../common/src/constants.ts"
3+
import { type Deps, withDeps } from "./utils.ts"
4+
5+
type GetKeysPayload = {
6+
connectionId: string;
7+
pattern?: string | undefined;
8+
count?: number | undefined;
9+
}
10+
11+
export const getKeysRequested = withDeps<Deps, void>(
12+
async ({ ws, clients, connectionId, action }) => {
13+
const client = clients.get(connectionId)
14+
15+
if (client) {
16+
await getKeys(client, ws, action.payload as GetKeysPayload)
17+
} else {
18+
ws.send(
19+
JSON.stringify({
20+
type: VALKEY.KEYS.getKeysFailed,
21+
payload: {
22+
connectionId,
23+
error: "Invalid connection Id",
24+
},
25+
}),
26+
)
27+
}
28+
},
29+
)
30+
31+
interface KeyPayload {
32+
connectionId: string;
33+
key: string;
34+
}
35+
36+
export const getKeyTypeRequested = withDeps<Deps, void>(
37+
async ({ ws, clients, connectionId, action }) => {
38+
const { key } = action.payload as unknown as KeyPayload
39+
40+
console.log("Handling getKeyTypeRequested for key:", key)
41+
const client = clients.get(connectionId)
42+
43+
if (client) {
44+
await getKeyInfoSingle(client, ws, action.payload as unknown as KeyPayload)
45+
} else {
46+
console.log("No client found for connectionId:", connectionId)
47+
ws.send(
48+
JSON.stringify({
49+
type: VALKEY.KEYS.getKeyTypeFailed,
50+
payload: {
51+
connectionId,
52+
key,
53+
error: "Invalid connection Id",
54+
},
55+
}),
56+
)
57+
}
58+
},
59+
)
60+
61+
export const deleteKeyRequested = withDeps<Deps, void>(
62+
async ({ ws, clients, connectionId, action }) => {
63+
const { key } = action.payload as unknown as KeyPayload
64+
65+
console.log("Handling deleteKeyRequested for key:", key)
66+
const client = clients.get(connectionId)
67+
68+
if (client) {
69+
await deleteKey(client, ws, action.payload as unknown as KeyPayload)
70+
} else {
71+
console.log("No client found for connectionId:", connectionId)
72+
ws.send(
73+
JSON.stringify({
74+
type: VALKEY.KEYS.deleteKeyFailed,
75+
payload: {
76+
connectionId,
77+
key,
78+
error: "Invalid connection Id",
79+
},
80+
}),
81+
)
82+
}
83+
},
84+
)
85+
86+
interface AddKeyRequestedPayload extends KeyPayload {
87+
keyType: string;
88+
value?: string | undefined;
89+
fields?: {
90+
field: string;
91+
value: string;
92+
}[] | undefined;
93+
values?: string[] | undefined;
94+
ttl?: number | undefined;
95+
}
96+
97+
export const addKeyRequested = withDeps<Deps, void>(
98+
async ({ ws, clients, connectionId, action }) => {
99+
const { key } = action.payload as unknown as KeyPayload
100+
101+
console.log("Handling addKeyRequested for key:", key)
102+
const client = clients.get(connectionId)
103+
if (client) {
104+
await addKey(client, ws, action.payload as unknown as AddKeyRequestedPayload)
105+
} else {
106+
console.log("No client found for connectionId:", connectionId)
107+
ws.send(
108+
JSON.stringify({
109+
type: VALKEY.KEYS.addKeyFailed,
110+
payload: {
111+
connectionId,
112+
key,
113+
error: "Invalid connection Id",
114+
},
115+
}),
116+
)
117+
}
118+
},
119+
)
120+
121+
export const updateKeyRequested = withDeps<Deps, void>(
122+
async ({ ws, clients, connectionId, action }) => {
123+
const { key } = action.payload as unknown as KeyPayload
124+
125+
console.log("Handling updateKeyRequested for key:", key)
126+
const client = clients.get(connectionId)
127+
if (client) {
128+
await updateKey(client, ws, action.payload as unknown as AddKeyRequestedPayload)
129+
} else {
130+
console.log("No client found for connectionId:", connectionId)
131+
ws.send(
132+
JSON.stringify({
133+
type: VALKEY.KEYS.addKeyFailed,
134+
payload: {
135+
connectionId,
136+
key,
137+
error: "Invalid connection Id",
138+
},
139+
}),
140+
)
141+
}
142+
},
143+
)

apps/server/src/actions/stats.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { GlideClient } from "@valkey/valkey-glide"
2+
import { type Deps, withDeps } from "./utils.ts"
3+
import { setDashboardData } from "../setDashboardData.ts"
4+
5+
export const setData = withDeps<Deps, void>(
6+
async ({ ws, clients, connectionId }) => {
7+
const client = clients.get(connectionId)
8+
9+
if (client instanceof GlideClient)
10+
await setDashboardData(connectionId, client, ws)
11+
},
12+
)

apps/server/src/actions/utils.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { type GlideClient, type GlideClusterClient } from "@valkey/valkey-glide"
2+
import type WebSocket from "ws"
3+
4+
export type Deps = {
5+
ws: WebSocket
6+
clients: Map<string, GlideClient | GlideClusterClient>
7+
connectionId: string
8+
}
9+
10+
export type ReduxAction = {
11+
type: string
12+
payload: {
13+
connectionId: string
14+
[k: string]: unknown
15+
},
16+
meta: unknown
17+
}
18+
19+
export type WsActionMessage = {
20+
payload: { connectionId: string },
21+
type: string
22+
}
23+
24+
// most actions need ws, clients, connectionId before they can process a redux action
25+
export const withDeps =
26+
<D, R>(fn: (ctx: D & { action: ReduxAction }) => R | Promise<R>) =>
27+
(deps: D) =>
28+
async (action: ReduxAction): Promise<Awaited<R>> => {
29+
return await fn({ ...deps, action })
30+
}
31+
32+
export type Handler = (deps: Deps) => (action: ReduxAction) => Promise<void>
33+
34+
export const unknownHandler: Handler = () =>
35+
async (action: { type: string }) => {
36+
console.log("Unknown action type:", action.type)
37+
}

0 commit comments

Comments
 (0)