feat(custom): implemented custom promises
This commit is contained in:
parent
93b22a886a
commit
4d3e266b07
2 changed files with 64 additions and 0 deletions
56
pkg/custom.go
Normal file
56
pkg/custom.go
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
package gonf
|
||||||
|
|
||||||
|
var customPromises []*CustomPromise
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
customPromises = make([]*CustomPromise, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
type CustomPromiseInterface interface {
|
||||||
|
Promise
|
||||||
|
Status() Status
|
||||||
|
}
|
||||||
|
|
||||||
|
type CustomPromise struct {
|
||||||
|
promise CustomPromiseInterface
|
||||||
|
}
|
||||||
|
|
||||||
|
func MakeCustomPromise(p CustomPromiseInterface) *CustomPromise {
|
||||||
|
return &CustomPromise{
|
||||||
|
promise: p,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CustomPromise) IfRepaired(p ...Promise) Promise {
|
||||||
|
c.promise.IfRepaired(p...)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CustomPromise) Promise() Promise {
|
||||||
|
customPromises = append(customPromises, c)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CustomPromise) Resolve() {
|
||||||
|
c.promise.Resolve()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c CustomPromise) Status() Status {
|
||||||
|
return c.Status()
|
||||||
|
}
|
||||||
|
|
||||||
|
func resolveCustomPromises() (status Status) {
|
||||||
|
status = KEPT
|
||||||
|
for _, c := range customPromises {
|
||||||
|
if c.promise.Status() == PROMISED {
|
||||||
|
c.Resolve()
|
||||||
|
switch c.promise.Status() {
|
||||||
|
case BROKEN:
|
||||||
|
return BROKEN
|
||||||
|
case REPAIRED:
|
||||||
|
status = REPAIRED
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
|
@ -47,6 +47,14 @@ func Resolve() (status Status) {
|
||||||
case REPAIRED:
|
case REPAIRED:
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
// ----- CustomPromises ----------------------------------------
|
||||||
|
status = resolveCustomPromises()
|
||||||
|
switch status {
|
||||||
|
case BROKEN:
|
||||||
|
return BROKEN
|
||||||
|
case REPAIRED:
|
||||||
|
continue
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue