Skip to content
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

Add a WalkWithCancel function #4181

Open
kevin0527 opened this issue Jun 5, 2024 · 0 comments
Open

Add a WalkWithCancel function #4181

kevin0527 opened this issue Jun 5, 2024 · 0 comments

Comments

@kevin0527
Copy link
Contributor

Is your feature request related to a problem? Please describe.
The current Walk function processes each element but does not provide a mechanism for cancellation. If the processing needs to be canceled, the existing code does not support it directly. I suggest adding a WalkWithCancel function that accepts a context.Context parameter, allowing the processing to be canceled when necessary.

Describe the solution you'd like

func (s Stream) WalkWithCancel(ctx context.Context, fn WalkFunc, opts ...Option) Stream {
}
ctx, cancel := context.WithCancel(context.Background())
fx.From(func(source chan<- any) {
	for i := 0; i < 10; i++ {
		source <- i
	}
}).WalkWithCancel(ctx, func(item any, pipe chan<- any) {
	i := item.(int)
	if i == 5 {
		cancel()
	}
}).Done()

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

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

No branches or pull requests

1 participant