-
Notifications
You must be signed in to change notification settings - Fork 3
♻️ Make forEach a resource for guaranteed stream subscription #209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
taras
wants to merge
1
commit into
main
Choose a base branch
from
refactor/foreach-resource
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,42 +1,54 @@ | ||
| import type { Operation, Stream } from "effection"; | ||
| import { | ||
| type Operation, | ||
| type Stream, | ||
| type Task, | ||
| resource, | ||
| spawn, | ||
| } from "effection"; | ||
|
|
||
| /** | ||
| * Invoke a function for each item passing through the stream. | ||
| * Subscribe to a stream and invoke a function for each item, returning a | ||
| * {@link Task} that resolves to the stream's close value. | ||
| * | ||
| * Because `forEach` is a resource, the subscription is established as soon as | ||
| * it is yielded, **before** the consumer loop begins. This makes it safe to | ||
| * use without an extra `spawn` + `sleep(0)` dance. | ||
| * | ||
| * @template T - The type of items in the stream | ||
| * @template TClose - The type of the close value returned when the stream ends | ||
| * @param fn - A function that processes each item from the stream. | ||
| * @param stream: A stream to process | ||
| * @param stream - A stream to process | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * import { forEach } from "./for-each.ts"; | ||
| * import { createSignal } from "effection"; | ||
| * | ||
| * // Process items from a stream | ||
| * const stream = createSignal<number, void>(); | ||
| * // Background usage – subscribes and runs without blocking | ||
| * yield* forEach(function*(item) { | ||
| * console.log(`Processing: ${item}`); | ||
| * }, stream); | ||
| * | ||
| * yield* spawn(() => forEach(function*(item) { | ||
| * // Blocking usage – waits until the stream closes | ||
| * yield* (yield* forEach(function*(item) { | ||
| * console.log(`Processing: ${item}`); | ||
| * }, stream)); | ||
| * | ||
| * yield* stream.send(1); | ||
| * yield* stream.send(2); | ||
| * ``` | ||
| */ | ||
| export function forEach<T, TClose>( | ||
| fn: (item: T) => Operation<void>, | ||
| stream: Stream<T, TClose>, | ||
| ): Operation<TClose> { | ||
| return { | ||
| *[Symbol.iterator]() { | ||
| let subscription = yield* stream; | ||
| ): Operation<Task<TClose>> { | ||
| return resource(function* (provide) { | ||
| const subscription = yield* stream; | ||
| const task = yield* spawn(function* (): Operation<TClose> { | ||
| let next = yield* subscription.next(); | ||
| while (!next.done) { | ||
| yield* fn(next.value); | ||
| next = yield* subscription.next(); | ||
| } | ||
| return next.value; | ||
| }, | ||
| }; | ||
| }); | ||
| yield* provide(task); | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: thefrontside/effectionx
Length of output: 95
🏁 Script executed:
Repository: thefrontside/effectionx
Length of output: 100
🏁 Script executed:
Repository: thefrontside/effectionx
Length of output: 2466
🏁 Script executed:
Repository: thefrontside/effectionx
Length of output: 2071
🏁 Script executed:
Repository: thefrontside/effectionx
Length of output: 1521
🏁 Script executed:
Repository: thefrontside/effectionx
Length of output: 1482
🏁 Script executed:
Repository: thefrontside/effectionx
Length of output: 890
🏁 Script executed:
Repository: thefrontside/effectionx
Length of output: 523
🏁 Script executed:
# Check other stream operations for comparison with forEach pattern cat -n stream-helpers/reduce.tsRepository: thefrontside/effectionx
Length of output: 1868
🏁 Script executed:
Repository: thefrontside/effectionx
Length of output: 1046
🏁 Script executed:
Repository: thefrontside/effectionx
Length of output: 702
🏁 Script executed:
Repository: thefrontside/effectionx
Length of output: 2706
🏁 Script executed:
Repository: thefrontside/effectionx
Length of output: 6048
🏁 Script executed:
Repository: thefrontside/effectionx
Length of output: 49
🏁 Script executed:
# Read the relevant policies cat .policies/ergonomics.mdRepository: thefrontside/effectionx
Length of output: 4267
🏁 Script executed:
Repository: thefrontside/effectionx
Length of output: 3956
🏁 Script executed:
Repository: thefrontside/effectionx
Length of output: 4099
🏁 Script executed:
Repository: thefrontside/effectionx
Length of output: 3030
🏁 Script executed:
Repository: thefrontside/effectionx
Length of output: 2468
🏁 Script executed:
Repository: thefrontside/effectionx
Length of output: 49
🏁 Script executed:
Repository: thefrontside/effectionx
Length of output: 187
🏁 Script executed:
Repository: thefrontside/effectionx
Length of output: 1403
🏁 Script executed:
Repository: thefrontside/effectionx
Length of output: 3895
The returned
Taskdoesn't own the subscription lifetime.Line 43 subscribes in the resource scope, but line 44 spawns a child task that only consumes from the subscription. Halting the returned
Taskstops the drain loop but leaves the resource scope mounted until the caller's scope exits, keeping the subscription alive. This violates.policies/ergonomics.md("Helpers must preserve structured concurrency semantics") and.policies/structured-concurrency.md("Cancellation, cleanup, and task lifetimes should be obvious and enforced").Compare this to
drain()(line 27–32), where the subscription lifetime equals the operation lifetime—clear and correct. Move the subscription acquisition into the spawned child task, or document that the returned Task doesn't own cleanup and callers must scope the entire operation. Add a test for earlytask.halt()to verify the intended semantics.🤖 Prompt for AI Agents