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 version of Pump() with status callbacks #4

Merged
merged 1 commit into from
Jan 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,35 @@ func Pump(ctx context.Context, dst Sink, src Source) error {

panic("unreachable")
}

// Pump moves values from a source into a sink.
// PumpWithStatus lets you include callbacks so you know when it's processing vs. waiting.
//
// Currently this doesn't work atomically, so if a Sink errors in the
// Pour call, the value that was read from the source is lost.
func PumpWithStatus(ctx context.Context, dst Sink, src Source, startWaiting func(), doneWaiting func(), startProcessing func(), doneProcessing func()) error {
if psrc, ok := src.(PushSource); ok {
return psrc.Push(ctx, dst)
}

for {
startWaiting()
v, err := src.Next(ctx)
doneWaiting()
if IsEOS(err) {
return nil
} else if err != nil {
return err
}

startProcessing()
err = dst.Pour(ctx, v)
doneProcessing()
if err != nil {
return err
}
}

panic("unreachable")
}