Skip to content

Add AbortSignal support to all async Collection methods#18

Merged
meritt merged 1 commit intomainfrom
feature/abort-signal
Apr 27, 2026
Merged

Add AbortSignal support to all async Collection methods#18
meritt merged 1 commit intomainfrom
feature/abort-signal

Conversation

@meritt
Copy link
Copy Markdown
Owner

@meritt meritt commented Apr 27, 2026

Summary

Every async method on Collection now accepts an optional { signal } and forwards it to the underlying driver call:

  • find / findOnesignal added to find options.
  • exists / count / distinct — new options arg with signal.
  • save — signal forwarded to both insertOne and replaceOne paths.
  • saveAll — signal forwarded to insertMany.
  • update — new options arg with signal (plays nicely with existing third-arg surface from update(): forward arrayFilters to the driver #17).
  • remove / removeById — new options arg with signal.

When the signal is already aborted (or fires mid-flight), the driver throws AbortError, which the existing try/catch collapses into the method's empty default + _emit. The fail-silent contract is unchanged — public methods still never throw.

Callers that need to distinguish "aborted" from "empty result" check signal.aborted after the await — same pattern Node uses for fs/promises and other AbortController-aware APIs.

findById is intentionally not extended — its second positional arg is already overloaded (fields shortcut). Callers that need signal-aware id lookup use findOne({ id }, { signal }) directly; the id alias is recognised by prepare().

Test plan

  • pnpm test — all 129 tests pass (13 new in test/abort-signal.js covering pre-aborted, mid-flight, fresh-signal-as-noop, and abort-detection-via-signal.aborted).
  • pnpm lint — clean.
  • pnpm format:check — clean.
  • CI matrix.

Context

Part of the yamb wishlist landing — see plan/yamb-architecture-decisions.md on feature/yamb-request. Required for the storage contract's cancellation semantics, and a prerequisite for the upcoming findCursor PR.

@coveralls
Copy link
Copy Markdown

coveralls commented Apr 27, 2026

Coverage Status

coverage: 99.764% (+0.9%) from 98.852% — feature/abort-signal into main

@meritt meritt mentioned this pull request Apr 27, 2026
4 tasks
@meritt meritt self-assigned this Apr 27, 2026
Every async method now accepts an optional `{ signal }` and forwards
it to the underlying driver call:

- find / findOne — { signal } added to find options
- exists / count / distinct — new options arg with signal
- save — signal forwarded to both insertOne and replaceOne paths
- saveAll — signal forwarded to insertMany
- update — new options arg with signal (plays nicely with existing
  third-arg surface)
- remove / removeById — new options arg with signal

When the signal is already aborted (or fires mid-flight), the driver
throws AbortError, which the existing try/catch collapses into the
method's empty default + _emit. The fail-silent contract is unchanged.

Callers that need to distinguish "aborted" from "empty result" check
`signal.aborted` after the await — same pattern Node uses for
fs/promises and other AbortController-aware APIs.

findById is intentionally not extended — its second positional arg is
already overloaded (fields shortcut). Callers that need signal-aware
id lookup use findOne({ id }, { signal }) directly; the `id` alias is
recognised by prepare().
@meritt meritt force-pushed the feature/abort-signal branch from 9a83b75 to 191b267 Compare April 27, 2026 19:18
@meritt meritt merged commit 5d1efd2 into main Apr 27, 2026
7 checks passed
@meritt meritt deleted the feature/abort-signal branch April 27, 2026 19:21
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.

2 participants