metacubexd/src/signals/connections.ts

115 lines
3.0 KiB
TypeScript
Raw Normal View History

2023-09-08 13:39:51 +08:00
import { differenceWith, isNumber, unionWith } from 'lodash'
import { Accessor, createEffect, createSignal, untrack } from 'solid-js'
import { CONNECTIONS_TABLE_MAX_CLOSED_ROWS } from '~/constants'
2023-09-08 13:39:51 +08:00
import { Connection, ConnectionRawMessage } from '~/types'
2023-09-08 18:42:15 +08:00
export type WsMsg = {
connections?: ConnectionRawMessage[]
uploadTotal: number
downloadTotal: number
} | null
2023-09-08 11:55:22 +08:00
// we make connections global, so we can keep track of connections when user in proxy page
// when user selects proxy and close some connections they can back and check connections
// they closed
export let allConnections: Connection[] = []
2023-09-08 18:42:15 +08:00
export const setAllConnections = (allConns: Connection[]) => {
allConnections = allConns
}
2023-09-08 13:39:51 +08:00
export let latestConnectionMsg: Accessor<WsMsg> = () => ({
uploadTotal: 0,
downloadTotal: 0,
connections: [],
})
2023-09-08 18:42:15 +08:00
export const setLatestConnectionMsg = (accessor: Accessor<WsMsg>) => {
latestConnectionMsg = accessor
}
export const useConnections = () => {
2023-09-08 13:39:51 +08:00
const [closedConnections, setClosedConnections] = createSignal<Connection[]>(
[],
)
const [activeConnections, setActiveConnections] = createSignal<Connection[]>(
[],
)
const [paused, setPaused] = createSignal(false)
createEffect(() => {
2023-09-08 13:39:51 +08:00
const rawConns = latestConnectionMsg()?.connections
2023-09-08 13:39:51 +08:00
if (!rawConns) {
return
}
untrack(() => {
2023-09-08 13:39:51 +08:00
const activeConns = restructRawMsgToConnection(
rawConns,
activeConnections(),
)
const allConns = mergeAllConnections(activeConns)
if (!paused()) {
2023-09-08 13:39:51 +08:00
const closedConns = diffClosedConnections(activeConns, allConns)
setActiveConnections(activeConns)
setClosedConnections(
closedConns.slice(-CONNECTIONS_TABLE_MAX_CLOSED_ROWS),
)
}
setAllConnections(
allConns.slice(
-(activeConns.length + CONNECTIONS_TABLE_MAX_CLOSED_ROWS),
),
)
})
})
return {
2023-09-08 13:39:51 +08:00
closedConnections,
activeConnections,
paused,
setPaused,
}
}
2023-09-08 13:39:51 +08:00
2023-09-08 17:09:32 +08:00
export const restructRawMsgToConnection = (
2023-09-08 13:39:51 +08:00
connections: ConnectionRawMessage[],
prevActiveConnections: Connection[],
2023-09-08 17:09:32 +08:00
): Connection[] => {
2023-09-08 13:39:51 +08:00
const prevMap = new Map<string, Connection>()
prevActiveConnections.forEach((prev) => prevMap.set(prev.id, prev))
return connections.map((connection) => {
const prevConn = prevMap.get(connection.id)
if (
!prevConn ||
!isNumber(prevConn.download) ||
!isNumber(prevConn.upload)
) {
return { ...connection, downloadSpeed: 0, uploadSpeed: 0 }
}
return {
...connection,
downloadSpeed: connection.download - prevConn.download,
uploadSpeed: connection.upload - prevConn.upload,
}
})
}
2023-09-08 17:09:32 +08:00
export const mergeAllConnections = (activeConns: Connection[]) => {
return unionWith(allConnections, activeConns, (a, b) => a.id === b.id)
2023-09-08 13:39:51 +08:00
}
2023-09-08 17:09:32 +08:00
const diffClosedConnections = (
2023-09-08 13:39:51 +08:00
activeConns: Connection[],
allConns: Connection[],
2023-09-08 17:09:32 +08:00
) => {
2023-09-08 13:39:51 +08:00
return differenceWith(allConns, activeConns, (a, b) => a.id === b.id)
}