Skip to content

Commit

Permalink
chore: improve ref init
Browse files Browse the repository at this point in the history
  • Loading branch information
sysulq committed Sep 9, 2024
1 parent 820d713 commit 1923c84
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions kod.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ type Ref[T any] struct {

// Get returns the held reference value.
func (r *Ref[T]) Get() T {
r.once.Do(func() {
r.value = lo.Must(r.getter()).(T)
})

r.init()
return r.value
}

Expand All @@ -116,12 +113,15 @@ func (r Ref[T]) isRef() {}
func (r *Ref[T]) setRef(lazyInit bool, getter componentGetter) {
r.getter = getter
if !lazyInit {
r.once.Do(func() {
r.value = lo.Must(r.getter()).(T)
})
r.init()
}
}

// init initializes the reference value.
func (r *Ref[T]) init() {
r.once.Do(func() { r.value = lo.Must(r.getter()).(T) })
}

// componentGetter is a function type for getting a reference value.
type componentGetter func() (any, error)

Expand Down

0 comments on commit 1923c84

Please sign in to comment.