You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.6 KiB
TypeScript

import { Flow } from './message'
4 years ago
export class FlowManager {
private items: Flow[]
private _map: Map<string, Flow>
4 years ago
private filterText: string
private filterTimer: number | null
private num: number
private max: number
constructor() {
this.items = []
this._map = new Map()
this.filterText = ''
this.filterTimer = null
this.num = 0
this.max = 1000
}
showList() {
let text = this.filterText
if (text) text = text.trim()
if (!text) return this.items
// regexp
if (text.startsWith('/') && text.endsWith('/')) {
text = text.slice(1, text.length - 1).trim()
if (!text) return this.items
try {
const reg = new RegExp(text)
return this.items.filter(item => {
return reg.test(item.request.url)
})
} catch (err) {
return this.items
}
}
return this.items.filter(item => {
return item.request.url.includes(text)
})
}
add(item: Flow) {
item.no = ++this.num
this.items.push(item)
this._map.set(item.id, item)
4 years ago
if (this.items.length > this.max) {
const oldest = this.items.shift()
4 years ago
if (oldest) this._map.delete(oldest.id)
}
}
4 years ago
get(id: string) {
return this._map.get(id)
}
4 years ago
changeFilter(text: string) {
this.filterText = text
}
4 years ago
changeFilterLazy(text: string, callback: () => void) {
if (this.filterTimer) {
clearTimeout(this.filterTimer)
this.filterTimer = null
}
this.filterTimer = setTimeout(() => {
this.filterText = text
callback()
4 years ago
}, 300) as any
}
clear() {
this.items = []
this._map = new Map()
}
}