go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

Add a WalkWithCancel function

Open kevin0527 opened this issue 1 month ago • 0 comments

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.

kevin0527 avatar Jun 05 '24 03:06 kevin0527