Skip to content

Commit

Permalink
Context will not panic (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshineplan committed Jun 5, 2024
1 parent effee2f commit cb81eff
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,31 @@ var _ context.Context = &Chrome{}
func (c *Chrome) Deadline() (deadline time.Time, ok bool) {
c.mu.Lock()
defer c.mu.Unlock()
if _, _, _, err := c.context(context.Background(), false); err != nil {
panic(err)
}
return c.ctx.Deadline()
ctx, _, _, _ := c.context(context.Background(), false)
return ctx.Deadline()
}

func (c *Chrome) Done() <-chan struct{} {
c.mu.Lock()
defer c.mu.Unlock()
if _, _, _, err := c.context(context.Background(), false); err != nil {
panic(err)
}
return c.ctx.Done()
ctx, _, _, _ := c.context(context.Background(), false)
return ctx.Done()
}

func (c *Chrome) Err() error {
c.mu.Lock()
defer c.mu.Unlock()
if _, _, _, err := c.context(context.Background(), false); err != nil {
return nil
return err
}
return c.ctx.Err()
}

func (c *Chrome) Value(key any) any {
c.mu.Lock()
defer c.mu.Unlock()
if _, _, _, err := c.context(context.Background(), false); err != nil {
return nil
}
return c.ctx.Value(key)
ctx, _, _, _ := c.context(context.Background(), false)
return ctx.Value(key)
}

func (c *Chrome) context(ctx context.Context, reset bool) (context.Context, context.CancelFunc, bool, error) {
Expand Down Expand Up @@ -73,7 +67,7 @@ func (c *Chrome) context(ctx context.Context, reset bool) (context.Context, cont
if err := chromedp.Run(c.ctx, c.actions...); err != nil {
cancel()
log.Print(err)
return nil, nil, false, err
return c.ctx, nil, false, err
}
go func() {
select {
Expand Down

0 comments on commit cb81eff

Please sign in to comment.