1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/drivers/common/lazy.go
Tim Voronov 5620be211c
Next (#214)
* Renamed DOCUMENT to PAGE

* Added PageLoadParams

* Added PageLoadParams

* Renamed LoadPageParams -> PageLoadParams

* Added support for context.Done() (#201)

* Bug/#189 operators precedence (#202)

* Fixed math operators precedence

* Fixed logical operators precedence

* Fixed array operator

* Added support for parentheses to enforce a different operator evaluation order

* Feature/#200 drivers (#209)

* Added new interfaces

* Renamed dynamic to cdp driver

* Renamed drivers

* Added ELEMENT_EXISTS function (#210)

* Renamed back PAGE to DOCUMENT (#211)

* Added Getter and Setter interfaces
2018-12-21 23:14:41 -05:00

92 lines
1.6 KiB
Go

package common
import (
"sync"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
type (
LazyFactory func() (core.Value, error)
LazyValue struct {
sync.Mutex
factory LazyFactory
ready bool
value core.Value
err error
}
)
func NewLazyValue(factory LazyFactory) *LazyValue {
lz := new(LazyValue)
lz.ready = false
lz.factory = factory
lz.value = values.None
return lz
}
// Ready indicates whether the value is ready.
// @returns (Boolean) - Boolean value indicating whether the value is ready.
func (lv *LazyValue) Ready() bool {
lv.Lock()
defer lv.Unlock()
return lv.ready
}
// Read returns an underlying value.
// Not thread safe. Should not mutated.
// @returns (Value) - Underlying value if successfully loaded, otherwise error
func (lv *LazyValue) Read() (core.Value, error) {
lv.Lock()
defer lv.Unlock()
if !lv.ready {
lv.load()
}
return lv.value, lv.err
}
// Write safely mutates an underlying value.
// Loads a value if it's not ready.
// Thread safe.
func (lv *LazyValue) Write(writer func(v core.Value, err error)) {
lv.Lock()
defer lv.Unlock()
if !lv.ready {
lv.load()
}
writer(lv.value, lv.err)
}
// Reset resets the storage.
// Next call of Read will trigger the factory function again.
func (lv *LazyValue) Reset() {
lv.Lock()
defer lv.Unlock()
lv.ready = false
lv.value = values.None
lv.err = nil
}
func (lv *LazyValue) load() {
val, err := lv.factory()
if err == nil {
lv.value = val
lv.err = nil
} else {
lv.value = values.None
lv.err = err
}
lv.ready = true
}