refactor(connections): conns paused

This commit is contained in:
Zephyruso 2023-09-06 20:13:22 +08:00
parent ff7dab74b6
commit 68522960fb

View File

@ -25,8 +25,8 @@ import {
} from '@tanstack/solid-table' } from '@tanstack/solid-table'
import byteSize from 'byte-size' import byteSize from 'byte-size'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import { differenceWith, isEqualWith } from 'lodash' import { differenceWith } from 'lodash'
import { For, createEffect, createMemo, createSignal, untrack } from 'solid-js' import { For, createEffect, createMemo, createSignal } from 'solid-js'
import { twMerge } from 'tailwind-merge' import { twMerge } from 'tailwind-merge'
import { Button, ConnectionsTableOrderingModal } from '~/components' import { Button, ConnectionsTableOrderingModal } from '~/components'
import { import {
@ -66,25 +66,15 @@ export default () => {
const connections = useWsRequest<{ connections: Connection[] }>('connections') const connections = useWsRequest<{ connections: Connection[] }>('connections')
const [closedConnectionsWithSpeed, setClosedConnectionsWithSpeed] = const [closedConnectionsWithSpeed, setClosedConnectionsWithSpeed] =
createSignal<ConnectionWithSpeed[]>([]) createSignal<ConnectionWithSpeed[]>([], { equals: () => paused() })
const [activeConnectionsWithSpeed, setActiveConnectionsWithSpeed] = const [activeConnectionsWithSpeed, setActiveConnectionsWithSpeed] =
createSignal<ConnectionWithSpeed[]>([]) createSignal<ConnectionWithSpeed[]>([], { equals: () => paused() })
const [paused, setPaused] = createSignal(false) const [paused, setPaused] = createSignal(false)
const [pausedActiveConnectionsSnapshot, setPausedActiveConnectionsSnapshot] =
createSignal<ConnectionWithSpeed[]>([])
const [pausedClosedConnectionsSnapshot, setPausedClosedConnectionsSnapshot] =
createSignal<ConnectionWithSpeed[]>([])
createEffect(() => { const updateConnections =
const data = connections()?.connections (data: Connection[]) => (prevConnections: ConnectionWithSpeed[]) => {
if (!data) {
return
}
setActiveConnectionsWithSpeed((prevConnections) => {
const prevMap = new Map<string, Connection>() const prevMap = new Map<string, Connection>()
prevConnections.forEach((prev) => prevMap.set(prev.id, prev)) prevConnections.forEach((prev) => prevMap.set(prev.id, prev))
@ -97,49 +87,38 @@ export default () => {
return { return {
...connection, ...connection,
downloadSpeed: prevConn.download downloadSpeed:
? connection.download - prevConn.download connection.download - (prevConn.download ?? connection.download),
: 0, uploadSpeed:
uploadSpeed: prevConn.upload connection.upload - (prevConn.upload ?? connection.upload),
? connection.upload - prevConn.upload
: 0,
} }
}) })
const closedConnections = differenceWith( const closedConnections = differenceWith(
prevConnections, prevConnections,
connections, connections,
(a, b) => isEqualWith(a, b, (a, b) => a.id === b.id), (a, b) => a.id === b.id,
) )
setClosedConnectionsWithSpeed((prev) => setClosedConnectionsWithSpeed((prev) =>
[...prev, ...closedConnections].slice(-1000), [...prev, ...closedConnections].slice(-1000),
) )
return connections.slice(-100) return connections
}) }
})
createEffect(() => { createEffect(() => {
if (paused()) { const data = connections()?.connections
setPausedActiveConnectionsSnapshot(
untrack(() => activeConnectionsWithSpeed()),
)
setPausedClosedConnectionsSnapshot( if (!data) {
untrack(() => closedConnectionsWithSpeed()), return
)
} }
const updater = updateConnections(data)
setActiveConnectionsWithSpeed(updater)
}) })
const activeConnectionsWithSpeedAndPausing = createMemo(() =>
paused() ? pausedActiveConnectionsSnapshot() : activeConnectionsWithSpeed(),
)
const closedConnectionsWithSpeedAndPausing = createMemo(() =>
paused() ? pausedClosedConnectionsSnapshot() : closedConnectionsWithSpeed(),
)
const onCloseConnection = (id: string) => request.delete(`connections/${id}`) const onCloseConnection = (id: string) => request.delete(`connections/${id}`)
const [columnVisibility, setColumnVisibility] = makePersisted( const [columnVisibility, setColumnVisibility] = makePersisted(
@ -292,8 +271,8 @@ export default () => {
}, },
get data() { get data() {
return activeTab() === ActiveTab.activeConnections return activeTab() === ActiveTab.activeConnections
? activeConnectionsWithSpeedAndPausing() ? activeConnectionsWithSpeed()
: closedConnectionsWithSpeedAndPausing() : closedConnectionsWithSpeed()
}, },
sortDescFirst: true, sortDescFirst: true,
enableHiding: true, enableHiding: true,
@ -312,12 +291,12 @@ export default () => {
{ {
type: ActiveTab.activeConnections, type: ActiveTab.activeConnections,
name: t('activeConnections'), name: t('activeConnections'),
count: activeConnectionsWithSpeedAndPausing().length, count: activeConnectionsWithSpeed().length,
}, },
{ {
type: ActiveTab.closedConnections, type: ActiveTab.closedConnections,
name: t('closedConnections'), name: t('closedConnections'),
count: closedConnectionsWithSpeedAndPausing().length, count: closedConnectionsWithSpeed().length,
}, },
] ]