Skip to content

feat: add the async pull adapter with cancellation on early exit - #533

Merged
awatson1978 merged 1 commit into
release/1.0-beta-corefrom
core/pull-adapter
Sep 29, 2026
Merged

awatson1978 merged 1 commit into
release/1.0-beta-corefrom
core/pull-adapter

Conversation

@awatson1978

@awatson1978 awatson1978 commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

This adds the pull half of the hybrid contract, together with the fix for review finding 13. The contract so far is push-only: a generator calls listener methods directly. createEventAsyncIterable, new in src/eventStream/asyncIterator.js, is the adapter for consumers who'd rather pull. You hand it a function that drives a listener, and it gives you an async iterable you can for await over. Each yielded event is a plain { type, args } object, where type is a vocabulary method name like "value" or "startSequence".

The backpressure is real, not decorative. The adapter installs a drain gate on the listener, so once the internal queue passes highWaterMark, the producer suspends at its next checkpoint until the consumer catches up.

import { createEventAsyncIterable, fromDataSet } from "dcmjs/src/eventStream";

const iterable = createEventAsyncIterable(
    listener => fromDataSet(dataset, listener),
    { highWaterMark: 64 }
);

for await (const ev of iterable) {
    if (ev.type === "value") console.log(ev.args[0]);
    if (ev.type === "endItem") break; // early exit is safe: the producer unwinds
}

That break is why this PR also carries the finding-13 fix, and why the adapter was held out of #530 until now. The iterator the original code returned had no return() method — the hook JavaScript calls when you break out of a for await loop or leave it via an exception. Without it, a producer suspended in its drain gate would wait forever for a consumer that was already gone. With a stream source, it would also have left the underlying reader locked.

The fix is the return() implementation you can see at the bottom of asyncIterator.js: it marks the iteration cancelled, drops the queue, and rejects the producer's pending drain promise with an EventIterationCancelled error. The run handler recognizes that specific rejection as a clean end of iteration rather than a stream failure, so nothing surfaces as a spurious error.

The regression test in test/eventStream/asyncIterator.test.js drives a 50-element dataset through a highWaterMark of 2, breaks after the first event, and asserts the producer settles. I verified it in both directions: it fails against the unfixed code and passes here. The other tests in the suite check that the pulled event sequence and payloads match exactly what the push core receives.

🤖 Generated with Claude Code

@awatson1978
awatson1978 merged commit e13395f into release/1.0-beta-core Sep 29, 2026
8 checks passed
awatson1978 added a commit that referenced this pull request Sep 30, 2026
Kept in-branch rather than as a GitHub issue for now. Finding 13 is
marked fixed (#533) and the fromPart10Stream budget question is recorded
as resolved by the granted waiver (#538).

Co-Authored-By: Claude Fable 5 <[email protected]>
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.0.0-beta.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant