Refactor the "handleRequest" pipeline #1901
Replies: 1 comment 4 replies
-
|
Overall this seems like a good direction! Re cache - looking into this more, i'm not sure if we need to implement a generic one yet, or if we can implement something more 'as necessary' and make it more generic later if we find it valuable. https://stackblitz.com/edit/vitest-dev-vitest-utw4vd?file=test%2Fbasic.test.ts I put together a very very very naiive sample of the performance for a large set of handlers. For For Just caching the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Motivation
There are a few pain points when working with the
handleRequest/getResponsefunctions internally:handleRequestis a mess:msw/src/core/utils/handleRequest.ts
Lines 45 to 52 in b440f12
It accepts way too much (
requestIdneeded just foremitter, theemitteritself, which is a huge side-effect that request resolution doesn't need at all) and it's generally poorly structured (does too much).getResponseas a standalone function.msw/src/core/utils/getResponse.ts
Lines 19 to 23 in b440f12
It makes the request resolution logic feel more complex than it actually is. Granted, it used to be much larger, and after the V2 refactoring I should've just moved it into
handleRequest.ts.Proposed changes
Replace
handleRequestwith a newNetworkAPIWe would still have to work around propagating
requestIdbecause it's a serializable request identifier (unlike theRequestinstance).Introduce cache
Add a
cacheproperty on theNetworkinstance that would do the following:HttpHandler).Refactor events (
emitter)Either make the
Networkemit its own events and pipe them to the setup API-basedemitter, or make the network accept the saidemitterdirectly.Beta Was this translation helpful? Give feedback.
All reactions