Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/core/experimental/define-network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ export interface DefineNetworkOptions<
onUnhandledFrame?: UnhandledFrameHandle
}

export interface NetworkApi<Sources extends Array<NetworkSource<any>>>
extends NetworkHandlersApi {
export interface NetworkApi<
Sources extends Array<NetworkSource<any>>,
> extends NetworkHandlersApi {
readyState: NetworkReadyState
/**
* Enable the network interception and handling.
Expand Down Expand Up @@ -210,11 +211,7 @@ export function defineNetwork<Sources extends Array<NetworkSource<any>>>(
handlersController.reset(handlers)
},
restoreHandlers() {
for (const handler of handlersController.currentHandlers()) {
if ('isUsed' in handler) {
handler.isUsed = false
}
}
handlersController.restore()
},
listHandlers() {
return toReadonlyArray(handlersController.currentHandlers())
Expand Down
14 changes: 14 additions & 0 deletions src/core/experimental/handlers-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ export abstract class HandlersController {
),
)

for (const handler of this.currentHandlers()) {
if ('reset' in handler) {
handler['reset']()
}
}

const { initialHandlers } = this.getState()

if (nextHandlers.length === 0) {
Expand All @@ -108,6 +114,14 @@ export abstract class HandlersController {
})
}

public restore(): void {
for (const handler of this.currentHandlers()) {
if ('restore' in handler) {
handler['restore']()
}
}
}

#validateHandlers(handlers: Array<AnyHandler>): boolean {
return handlers.every((handler) => !Array.isArray(handler))
}
Expand Down
6 changes: 1 addition & 5 deletions src/core/experimental/setup-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ export abstract class SetupApi<
}

public restoreHandlers(): void {
this.handlersController.currentHandlers().forEach((handler) => {
if ('isUsed' in handler) {
handler.isUsed = false
}
})
this.handlersController.restore()
}

public resetHandlers(...nextHandlers: Array<AnyHandler>): void {
Expand Down
28 changes: 28 additions & 0 deletions src/core/handlers/RequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,34 @@ export abstract class RequestHandler<
this.isUsed = false
}

/**
* Reset the runtime state accumulated during response resolution,
* such as generator iterator progress. Called when this handler is
* removed from the active handlers list so re-adding it later starts
* from a clean state.
*/
protected reset(): void {
const iterator = this.resolverIterator
this.resolverIterator = undefined
this.resolverIteratorResult = undefined

if (typeof iterator?.return === 'function') {
void Promise.resolve(iterator.return())
}
}
Comment thread
kettanaito marked this conversation as resolved.

/**
* Restore this handler so it can match requests again after being
* exhausted (e.g. via `{ once: true }`). Also clears any accumulated
* resolution state.
*/
protected restore(): void {
if (this.options?.once) {
this.reset()
this.isUsed = false
}
}

/**
* Determine if the intercepted request should be mocked.
*/
Expand Down
21 changes: 0 additions & 21 deletions src/core/utils/internal/requestHandlerUtils.ts

This file was deleted.

Loading
Loading