Skip to content

Commit

Permalink
cmd/internal/script: use sync.OnceValue
Browse files Browse the repository at this point in the history
Change-Id: I384a7391a26f24402c055aec98b37927305e2a39
Reviewed-on: https://go-review.googlesource.com/c/go/+/611042
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
  • Loading branch information
kolyshkin authored and gopherbot committed Sep 6, 2024
1 parent a1c3e24 commit a77b93c
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/cmd/internal/script/conds.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ func (b *boolCond) Eval(s *State, suffix string) (bool, error) {
// The eval function is not passed a *State because the condition is cached
// across all execution states and must not vary by state.
func OnceCondition(summary string, eval func() (bool, error)) Cond {
return &onceCond{eval: eval, usage: CondUsage{Summary: summary}}
return &onceCond{
eval: sync.OnceValues(eval),
usage: CondUsage{Summary: summary},
}
}

type onceCond struct {
once sync.Once
v bool
err error
eval func() (bool, error)
usage CondUsage
}
Expand All @@ -140,8 +140,7 @@ func (l *onceCond) Eval(s *State, suffix string) (bool, error) {
if suffix != "" {
return false, ErrUsage
}
l.once.Do(func() { l.v, l.err = l.eval() })
return l.v, l.err
return l.eval()
}

// CachedCondition is like Condition but only calls eval the first time the
Expand Down

0 comments on commit a77b93c

Please sign in to comment.