11import { SpeedIndicator } from './speed-indicator'
22import { RateCurve } from './rate-curve'
33import styled from '@emotion/styled'
4- import { useState , useContext , useRef } from 'react'
4+ import { useState , useContext , useEffect , useRef } from 'react'
55import { useRates } from '../hooks'
66import { ChannelsContext , ConfigContext , I18nContext } from '../context'
77import { zip , interval , Observable , Subscription } from 'rxjs'
@@ -40,6 +40,11 @@ type ChartItem = {
4040}
4141
4242const chartKeys : ChartKey [ ] = [ 'ping' , 'download' , 'upload' ]
43+ const MAX_RATE_POINTS = 60
44+
45+ function appendRate ( rates : number [ ] , rate : number ) {
46+ return [ ...rates . slice ( - ( MAX_RATE_POINTS - 1 ) ) , rate ]
47+ }
4348
4449enum RunningStep {
4550 NONE = 1 ,
@@ -64,6 +69,15 @@ export function RunCaseOnce() {
6469 const t = useContext ( I18nContext )
6570 const { duration, parallel, packCount, unit } = useContext ( ConfigContext )
6671
72+ useEffect (
73+ ( ) => ( ) => {
74+ runIdRef . current += 1
75+ sub . current ?. unsubscribe ( )
76+ sub . current = null
77+ } ,
78+ [ ] ,
79+ )
80+
6781 const waitObservable = < T , > ( observer : Observable < T > , onNext : ( value : T ) => void , runId : number ) =>
6882 new Promise < void > ( ( resolve , reject ) => {
6983 let settled = false
@@ -116,7 +130,7 @@ export function RunCaseOnce() {
116130 ) ,
117131 ( v ) => {
118132 pushTTL ( v )
119- setPingRates ( ( prevRates ) => [ ... prevRates , v ] )
133+ setPingRates ( ( prevRates ) => appendRate ( prevRates , v ) )
120134 } ,
121135 runId ,
122136 )
@@ -144,7 +158,7 @@ export function RunCaseOnce() {
144158 ( v ) => {
145159 const nextRate = v . reduce ( ( a , b ) => a + b , 0 )
146160 setDlRate ( nextRate )
147- setDlRates ( ( prevRates ) => [ ... prevRates , nextRate ] )
161+ setDlRates ( ( prevRates ) => appendRate ( prevRates , nextRate ) )
148162 } ,
149163 runId ,
150164 )
@@ -169,7 +183,7 @@ export function RunCaseOnce() {
169183 ( v ) => {
170184 const nextRate = v . reduce ( ( a , b ) => a + b , 0 )
171185 setUlRate ( nextRate )
172- setUlRates ( ( prevRates ) => [ ... prevRates , nextRate ] )
186+ setUlRates ( ( prevRates ) => appendRate ( prevRates , nextRate ) )
173187 } ,
174188 runId ,
175189 )
0 commit comments