80 lines
2.1 KiB
Go
80 lines
2.1 KiB
Go
package downloader
|
|
|
|
import (
|
|
"context"
|
|
|
|
"softbox.local/core/application"
|
|
)
|
|
|
|
// ApplicationPublisher is implemented by application.Runtime.
|
|
type ApplicationPublisher interface {
|
|
Publish(context.Context, application.Event) error
|
|
}
|
|
|
|
// ApplicationObserver maps downloader events to the documented application
|
|
// event envelope and concrete payloads.
|
|
type ApplicationObserver struct {
|
|
Publisher ApplicationPublisher
|
|
}
|
|
|
|
// PublishDownload implements Observer.
|
|
func (observer ApplicationObserver) PublishDownload(
|
|
ctx context.Context,
|
|
event Event,
|
|
) error {
|
|
if observer.Publisher == nil {
|
|
return nil
|
|
}
|
|
envelope := application.Event{
|
|
RequestID: event.RequestID,
|
|
AppID: event.AppID,
|
|
}
|
|
switch event.Type {
|
|
case EventStarted:
|
|
envelope.Type = application.EventDownloadStarted
|
|
envelope.Payload = application.DownloadStartedPayload{
|
|
Attempt: event.Attempt,
|
|
Done: event.Done,
|
|
TotalKnown: event.TotalKnown,
|
|
Total: event.Total,
|
|
}
|
|
case EventProgress:
|
|
envelope.Type = application.EventDownloadProgress
|
|
envelope.Payload = application.DownloadProgressPayload{
|
|
Attempt: event.Attempt,
|
|
Done: event.Done,
|
|
TotalKnown: event.TotalKnown,
|
|
Total: event.Total,
|
|
SpeedBytesSec: event.SpeedBytesSec,
|
|
}
|
|
case EventPaused:
|
|
envelope.Type = application.EventDownloadPaused
|
|
envelope.Payload = application.DownloadPausedPayload{
|
|
Attempt: event.Attempt,
|
|
Done: event.Done,
|
|
}
|
|
case EventCompleted:
|
|
envelope.Type = application.EventDownloadCompleted
|
|
envelope.Payload = application.DownloadCompletedPayload{
|
|
Attempt: event.Attempt,
|
|
Done: event.Done,
|
|
Path: event.CompletedPath,
|
|
}
|
|
case EventFailed:
|
|
envelope.Type = application.EventDownloadFailed
|
|
envelope.Payload = application.DownloadFailedPayload{
|
|
Attempt: event.Attempt,
|
|
Done: event.Done,
|
|
ErrorCode: event.ErrorCode,
|
|
}
|
|
case EventCanceled:
|
|
envelope.Type = application.EventDownloadCanceled
|
|
envelope.Payload = application.DownloadCanceledPayload{
|
|
Attempt: event.Attempt,
|
|
}
|
|
default:
|
|
return nil
|
|
}
|
|
return observer.Publisher.Publish(ctx, envelope)
|
|
}
|