Skip to content

Commit 250d9fc

Browse files
committed
refactor: clean unused code in core thread, add new hook
1 parent 13805ab commit 250d9fc

File tree

6 files changed

+34
-27
lines changed

6 files changed

+34
-27
lines changed

packages/network-debugger/src/core/dc.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* @deprecated information is not complete
3+
*/
14
import { ClientRequest, IncomingMessage } from 'http'
25
import { MainProcess } from './fork'
36
import * as dc from 'node:diagnostics_channel'

packages/network-debugger/src/core/fork.ts

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -118,41 +118,21 @@ export class MainProcess {
118118
}
119119

120120
public sendRequest(type: RequestType, request: RequestDetail) {
121-
this.send({
122-
type,
123-
data: request
124-
})
125-
return this
126-
}
127-
128-
public initRequest(request: RequestDetail) {
129-
this.sendRequest('initRequest', request)
130-
return this
131-
}
132-
133-
public registerRequest(request: RequestDetail) {
134121
const currentCell = getCurrentCell()
135122
let req = request
136123
if (currentCell) {
137124
currentCell.request = req
138-
const pipes = currentCell.pipes.filter((p) => p.type === 'regsiter').map((p) => p.pipe)
125+
const pipes = currentCell.pipes.filter((p) => p.type === type).map((p) => p.pipe)
139126
pipes.forEach((pipe) => {
140127
req = pipe(req)
141128
})
142129
currentCell.request = req
143130
}
144131

145-
this.sendRequest('registerRequest', req)
146-
return this
147-
}
148-
149-
public updateRequest(request: RequestDetail) {
150-
this.sendRequest('updateRequest', request)
151-
return this
152-
}
153-
154-
public endRequest(request: RequestDetail) {
155-
this.sendRequest('endRequest', request)
132+
this.send({
133+
type,
134+
data: request
135+
})
156136
return this
157137
}
158138

packages/network-debugger/src/core/hooks/cell.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { type RequestDetail } from '../../common'
2+
import { RequestType } from '../fork'
23

34
export interface Pipe<T = RequestDetail> {
45
(req: T): T
@@ -8,7 +9,7 @@ export interface Cell {
89
request: RequestDetail
910
pipes: Array<{
1011
pipe: Pipe<RequestDetail>
11-
type: 'regsiter'
12+
type: RequestType
1213
}>
1314
/**
1415
* @default false
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './useRegisterRequest'
2+
export * from './useRequestPipe'
23
export * from './useAbortRequest'
34
export * from './cell'

packages/network-debugger/src/core/hooks/useRegisterRequest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const useRegisterRequest: RegisterRequestHook = (pipe) => {
1414

1515
cell.pipes.push({
1616
pipe,
17-
type: 'regsiter'
17+
type: 'registerRequest'
1818
})
1919

2020
return
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { RequestDetail } from '../../common'
2+
import { RequestType } from '../fork'
3+
import { getCurrentCell } from './cell'
4+
5+
export interface RequestPipeHook {
6+
(type: RequestType, pipe: (req: RequestDetail) => RequestDetail): void
7+
}
8+
9+
export const useRequestPipe: RequestPipeHook = (type, pipe) => {
10+
const cell = getCurrentCell()
11+
12+
if (!cell) {
13+
throw new Error('useRequestPipe must be used in request handler')
14+
}
15+
16+
cell.pipes.push({
17+
pipe,
18+
type
19+
})
20+
21+
return
22+
}

0 commit comments

Comments
 (0)