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.
-
Motivation
There are a few pain points when working with the
handleRequest
/getResponse
functions internally:handleRequest
is a mess:msw/src/core/utils/handleRequest.ts
Lines 45 to 52 in b440f12
It accepts way too much (
requestId
needed just foremitter
, theemitter
itself, which is a huge side-effect that request resolution doesn't need at all) and it's generally poorly structured (does too much).getResponse
as 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
handleRequest
with a newNetwork
APIWe would still have to work around propagating
requestId
because it's a serializable request identifier (unlike theRequest
instance).Introduce cache
Add a
cache
property on theNetwork
instance that would do the following:HttpHandler
).Refactor events (
emitter
)Either make the
Network
emit its own events and pipe them to the setup API-basedemitter
, or make the network accept the saidemitter
directly.Beta Was this translation helpful? Give feedback.
All reactions