From 65d922102097fcd06dd46baea225747e08abd347 Mon Sep 17 00:00:00 2001 From: Caleb Foust Date: Fri, 8 Nov 2024 13:44:25 +0800 Subject: [PATCH] fix: quick scrolling through lots of previews --- pkg/replay/loader/module.go | 18 ++++++++++++++++-- pkg/replay/player/module.go | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/pkg/replay/loader/module.go b/pkg/replay/loader/module.go index 29164bf4..59d51b08 100644 --- a/pkg/replay/loader/module.go +++ b/pkg/replay/loader/module.go @@ -51,8 +51,16 @@ func (l *Loader) Init() tea.Cmd { } } + ctx := l.Lifetime.Ctx() + events := make([]sessions.Event, 0) for { + if ctx.Err() != nil { + return loadedEvent{ + err: ctx.Err(), + } + } + event, err := reader.Read() if err == io.EOF || err == io.ErrUnexpectedEOF { break @@ -65,10 +73,16 @@ func (l *Loader) Init() tea.Cmd { events = append(events, event) } - ctx := l.Lifetime.Ctx() + p, err := player.FromEventsContext(ctx, events) + if err != nil { + return loadedEvent{ + err: err, + } + } + replay := replay.New( ctx, - player.FromEvents(events), + p, l.timeBinds, l.copyBinds, l.options..., diff --git a/pkg/replay/player/module.go b/pkg/replay/player/module.go index 03ff17c9..160fcc0e 100644 --- a/pkg/replay/player/module.go +++ b/pkg/replay/player/module.go @@ -1,6 +1,8 @@ package player import ( + "context" + "github.com/cfoust/cy/pkg/emu" "github.com/cfoust/cy/pkg/geom" "github.com/cfoust/cy/pkg/geom/tty" @@ -183,3 +185,22 @@ func FromEvents(events []sessions.Event) *Player { return player } + +// FromEventsContext creates a Player from the given events, but stops if the +// context is cancelled. +func FromEventsContext( + ctx context.Context, + events []sessions.Event, +) (*Player, error) { + player := New() + + for _, event := range events { + if ctx.Err() != nil { + return nil, ctx.Err() + } + + player.Process(event) + } + + return player, nil +}