11import { RequestDetail } from '../common'
22import { headersToObject } from '../utils/map'
33import { MainProcess } from './fork'
4+ import { setCurrentCell } from './hooks/cell'
45
56export function proxyFetch ( mainProcess : MainProcess ) {
67 if ( ! globalThis . fetch ) {
@@ -15,10 +16,11 @@ export function proxyFetch(mainProcess: MainProcess) {
1516 }
1617}
1718
18- function fetchProxyFactory ( fetchFn : typeof fetch , mainProcess : MainProcess ) {
19+ export function fetchProxyFactory ( fetchFn : typeof fetch , mainProcess : MainProcess ) {
1920 return function ( request : string | URL | Request , options ?: RequestInit ) {
2021 const requestDetail = new RequestDetail ( )
21- requestDetail . requestStartTime = new Date ( ) . getTime ( )
22+ requestDetail . requestStartTime = Date . now ( )
23+ setCurrentCell ( { request : requestDetail , pipes : [ ] , isAborted : false } )
2224
2325 if ( typeof request === 'string' ) {
2426 requestDetail . url = request
@@ -37,10 +39,15 @@ function fetchProxyFactory(fetchFn: typeof fetch, mainProcess: MainProcess) {
3739 }
3840 requestDetail . requestData = options ?. body
3941
40- mainProcess . registerRequest ( requestDetail )
41- return fetchFn ( request as string | Request , options )
42+ const result = fetchFn ( request as string | Request , options )
4243 . then ( fetchResponseHandlerFactory ( requestDetail , mainProcess ) )
4344 . catch ( fetchErrorHandlerFactory ( requestDetail , mainProcess ) )
45+ . finally ( ( ) => {
46+ setCurrentCell ( null )
47+ } )
48+
49+ mainProcess . registerRequest ( requestDetail )
50+ return result
4451 }
4552}
4653
0 commit comments