Skip to content

Commit

Permalink
Add way to access scene object from within Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
lestrrat committed Aug 29, 2024
1 parent 702de8c commit 17c73b4
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions scene/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ type Scene struct {
actions []Action
}

type sceneKey struct{}

func InjectContext(ctx context.Context, s *Scene) context.Context {
return context.WithValue(ctx, sceneKey{}, s)
}

func FromContext(ctx context.Context) *Scene {
v := ctx.Value(sceneKey{})
if v == nil {
return nil
}
s, ok := v.(*Scene)
if !ok {
return nil
}
return s
}

func New() *Scene {
return &Scene{}
}
Expand All @@ -21,6 +39,8 @@ func (s *Scene) Add(a Action) *Scene {
}

func (s *Scene) Execute(ctx context.Context) error {
ctx = InjectContext(ctx, s)

for _, a := range s.actions {
if err := a.Execute(ctx); err != nil {
return err
Expand Down

0 comments on commit 17c73b4

Please sign in to comment.