Skip to content

fix(Interceptor): wrap the "once" event listener, ditch "untilIdle" Promise#430

Merged
kettanaito merged 8 commits intomainfrom
fix/interceptor-once
Sep 20, 2023
Merged

fix(Interceptor): wrap the "once" event listener, ditch "untilIdle" Promise#430
kettanaito merged 8 commits intomainfrom
fix/interceptor-once

Conversation

@kettanaito
Copy link
Copy Markdown
Member

@kettanaito kettanaito commented Sep 18, 2023

Changes

These are internal changes and have no effect on the public API.

This change drops the internal implementation of AsyncEventEmitter used to await all request listeners from being done. Instead, it introduces a RequestConrtoller that just holds a response Promise to whichever surface calling .respondWith().

The request listener await logic is achieved by:

  • Adding a once request listener that always responds to the intercepted request with `undefined. using the request controller. This leverages the native listeners execution order to provide a fall-safe listener in case the consumer hasn't handled the request in any way.
  • Adding a new emitAsync utility to account for async request listeners. emitAsync executes all the listeners for the given events awaiting them (i.e. in order).

Comment thread src/utils/AsyncEventEmitter.ts Outdated
eventName: EventName,
listener: Listener<any>
): this {
const wrappedListener = this.wrapListener(eventName, listener)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

once listener has to be wrapped the same as the on listener to open the event queue once the respective event is emitted.

@kettanaito kettanaito changed the title fix(Interceptor): wrap the "once" event listener fix(Interceptor): wrap the "once" event listener, ditch "untilIdle" Promise Sep 20, 2023
@kettanaito kettanaito merged commit cf4e228 into main Sep 20, 2023
@kettanaito kettanaito deleted the fix/interceptor-once branch September 20, 2023 22:05
@kettanaito
Copy link
Copy Markdown
Member Author

Released: v0.25.2 🎉

This has been released in v0.25.2!

Make sure to always update to the latest version (npm i @mswjs/interceptors@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant