mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-26 05:30:54 +03:00
parent
423594e03a
commit
b80853de90
342 changed files with 2118 additions and 2102 deletions
24
lazy/init.go
24
lazy/init.go
|
@ -38,13 +38,13 @@ type Init struct {
|
|||
children []*Init
|
||||
|
||||
init onceMore
|
||||
out interface{}
|
||||
out any
|
||||
err error
|
||||
f func() (interface{}, error)
|
||||
f func() (any, error)
|
||||
}
|
||||
|
||||
// Add adds a func as a new child dependency.
|
||||
func (ini *Init) Add(initFn func() (interface{}, error)) *Init {
|
||||
func (ini *Init) Add(initFn func() (any, error)) *Init {
|
||||
if ini == nil {
|
||||
ini = New()
|
||||
}
|
||||
|
@ -58,15 +58,15 @@ func (ini *Init) InitCount() int {
|
|||
}
|
||||
|
||||
// AddWithTimeout is same as Add, but with a timeout that aborts initialization.
|
||||
func (ini *Init) AddWithTimeout(timeout time.Duration, f func(ctx context.Context) (interface{}, error)) *Init {
|
||||
return ini.Add(func() (interface{}, error) {
|
||||
func (ini *Init) AddWithTimeout(timeout time.Duration, f func(ctx context.Context) (any, error)) *Init {
|
||||
return ini.Add(func() (any, error) {
|
||||
return ini.withTimeout(timeout, f)
|
||||
})
|
||||
}
|
||||
|
||||
// Branch creates a new dependency branch based on an existing and adds
|
||||
// the given dependency as a child.
|
||||
func (ini *Init) Branch(initFn func() (interface{}, error)) *Init {
|
||||
func (ini *Init) Branch(initFn func() (any, error)) *Init {
|
||||
if ini == nil {
|
||||
ini = New()
|
||||
}
|
||||
|
@ -74,14 +74,14 @@ func (ini *Init) Branch(initFn func() (interface{}, error)) *Init {
|
|||
}
|
||||
|
||||
// BranchdWithTimeout is same as Branch, but with a timeout.
|
||||
func (ini *Init) BranchWithTimeout(timeout time.Duration, f func(ctx context.Context) (interface{}, error)) *Init {
|
||||
return ini.Branch(func() (interface{}, error) {
|
||||
func (ini *Init) BranchWithTimeout(timeout time.Duration, f func(ctx context.Context) (any, error)) *Init {
|
||||
return ini.Branch(func() (any, error) {
|
||||
return ini.withTimeout(timeout, f)
|
||||
})
|
||||
}
|
||||
|
||||
// Do initializes the entire dependency graph.
|
||||
func (ini *Init) Do() (interface{}, error) {
|
||||
func (ini *Init) Do() (any, error) {
|
||||
if ini == nil {
|
||||
panic("init is nil")
|
||||
}
|
||||
|
@ -154,7 +154,7 @@ func (ini *Init) Reset() {
|
|||
}
|
||||
}
|
||||
|
||||
func (ini *Init) add(branch bool, initFn func() (interface{}, error)) *Init {
|
||||
func (ini *Init) add(branch bool, initFn func() (any, error)) *Init {
|
||||
ini.mu.Lock()
|
||||
defer ini.mu.Unlock()
|
||||
|
||||
|
@ -179,7 +179,7 @@ func (ini *Init) checkDone() {
|
|||
}
|
||||
}
|
||||
|
||||
func (ini *Init) withTimeout(timeout time.Duration, f func(ctx context.Context) (interface{}, error)) (interface{}, error) {
|
||||
func (ini *Init) withTimeout(timeout time.Duration, f func(ctx context.Context) (any, error)) (any, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
c := make(chan verr, 1)
|
||||
|
@ -203,6 +203,6 @@ func (ini *Init) withTimeout(timeout time.Duration, f func(ctx context.Context)
|
|||
}
|
||||
|
||||
type verr struct {
|
||||
v interface{}
|
||||
v any
|
||||
err error
|
||||
}
|
||||
|
|
|
@ -48,16 +48,16 @@ func TestInit(t *testing.T) {
|
|||
|
||||
var result string
|
||||
|
||||
f1 := func(name string) func() (interface{}, error) {
|
||||
return func() (interface{}, error) {
|
||||
f1 := func(name string) func() (any, error) {
|
||||
return func() (any, error) {
|
||||
result += name + "|"
|
||||
doWork()
|
||||
return name, nil
|
||||
}
|
||||
}
|
||||
|
||||
f2 := func() func() (interface{}, error) {
|
||||
return func() (interface{}, error) {
|
||||
f2 := func() func() (any, error) {
|
||||
return func() (any, error) {
|
||||
doWork()
|
||||
return nil, nil
|
||||
}
|
||||
|
@ -110,7 +110,7 @@ func TestInit(t *testing.T) {
|
|||
func TestInitAddWithTimeout(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
|
||||
init := New().AddWithTimeout(100*time.Millisecond, func(ctx context.Context) (interface{}, error) {
|
||||
init := New().AddWithTimeout(100*time.Millisecond, func(ctx context.Context) (any, error) {
|
||||
return nil, nil
|
||||
})
|
||||
|
||||
|
@ -122,7 +122,7 @@ func TestInitAddWithTimeout(t *testing.T) {
|
|||
func TestInitAddWithTimeoutTimeout(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
|
||||
init := New().AddWithTimeout(100*time.Millisecond, func(ctx context.Context) (interface{}, error) {
|
||||
init := New().AddWithTimeout(100*time.Millisecond, func(ctx context.Context) (any, error) {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
|
@ -145,7 +145,7 @@ func TestInitAddWithTimeoutTimeout(t *testing.T) {
|
|||
func TestInitAddWithTimeoutError(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
|
||||
init := New().AddWithTimeout(100*time.Millisecond, func(ctx context.Context) (interface{}, error) {
|
||||
init := New().AddWithTimeout(100*time.Millisecond, func(ctx context.Context) (any, error) {
|
||||
return nil, errors.New("failed")
|
||||
})
|
||||
|
||||
|
@ -178,8 +178,8 @@ func TestInitBranchOrder(t *testing.T) {
|
|||
|
||||
base := New()
|
||||
|
||||
work := func(size int, f func()) func() (interface{}, error) {
|
||||
return func() (interface{}, error) {
|
||||
work := func(size int, f func()) func() (any, error) {
|
||||
return func() (any, error) {
|
||||
doWorkOfSize(size)
|
||||
if f != nil {
|
||||
f()
|
||||
|
@ -225,7 +225,7 @@ func TestInitBranchOrder(t *testing.T) {
|
|||
func TestResetError(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
r := false
|
||||
i := New().Add(func() (interface{}, error) {
|
||||
i := New().Add(func() (any, error) {
|
||||
if r {
|
||||
return nil, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue