mirror of
https://github.com/asticode/go-astikit.git
synced 2026-04-22 14:57:10 +08:00
Inverted Add closer functions
This commit is contained in:
@@ -4,13 +4,13 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type CloseFunc func() error
|
||||
type CloseWithoutErrorFunc func()
|
||||
type CloseFunc func()
|
||||
type CloseFuncWithError func() error
|
||||
|
||||
// Closer is an object that can close several things
|
||||
type Closer struct {
|
||||
closed bool
|
||||
fs []CloseFunc
|
||||
fs []CloseFuncWithError
|
||||
// We need to split into 2 mutexes to allow using .Add() in .Do()
|
||||
mc *sync.Mutex // Locks .Close()
|
||||
mf *sync.Mutex // Locks fs
|
||||
@@ -45,7 +45,7 @@ func (c *Closer) Close() error {
|
||||
}
|
||||
|
||||
// Reset closers
|
||||
c.fs = []CloseFunc{}
|
||||
c.fs = []CloseFuncWithError{}
|
||||
|
||||
// Update attribute
|
||||
c.closed = true
|
||||
@@ -62,21 +62,20 @@ func (c *Closer) Close() error {
|
||||
return err
|
||||
}
|
||||
|
||||
// Add adds a close func at the beginning of the list
|
||||
func (c *Closer) Add(f CloseFunc) {
|
||||
c.AddWithError(func() error {
|
||||
f()
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Closer) AddWithError(f CloseFuncWithError) {
|
||||
// Lock
|
||||
c.mf.Lock()
|
||||
defer c.mf.Unlock()
|
||||
|
||||
// Append
|
||||
c.fs = append([]CloseFunc{f}, c.fs...)
|
||||
}
|
||||
|
||||
func (c *Closer) AddWithoutError(f CloseWithoutErrorFunc) {
|
||||
c.Add(func() error {
|
||||
f()
|
||||
return nil
|
||||
})
|
||||
c.fs = append([]CloseFuncWithError{f}, c.fs...)
|
||||
}
|
||||
|
||||
func (c *Closer) Append(dst *Closer) {
|
||||
@@ -93,7 +92,7 @@ func (c *Closer) Append(dst *Closer) {
|
||||
// NewChild creates a new child closer
|
||||
func (c *Closer) NewChild() (child *Closer) {
|
||||
child = NewCloser()
|
||||
c.Add(child.Close)
|
||||
c.AddWithError(child.Close)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
+6
-12
@@ -12,30 +12,24 @@ func TestCloser(t *testing.T) {
|
||||
c1 := NewCloser()
|
||||
c1.OnClosed(func(err error) { c++ })
|
||||
c2 := c1.NewChild()
|
||||
c1.Add(func() error {
|
||||
o = append(o, "1")
|
||||
return nil
|
||||
})
|
||||
c1.Add(func() error {
|
||||
c1.Add(func() { o = append(o, "1") })
|
||||
c1.AddWithError(func() error {
|
||||
o = append(o, "2")
|
||||
return errors.New("1")
|
||||
})
|
||||
c1.Add(func() error { return errors.New("2") })
|
||||
c2.Add(func() error {
|
||||
c1.AddWithError(func() error { return errors.New("2") })
|
||||
c2.AddWithError(func() error {
|
||||
o = append(o, "3")
|
||||
return errors.New("3")
|
||||
})
|
||||
c1.AddWithoutError(func() {
|
||||
o = append(o, "4")
|
||||
})
|
||||
err := c1.Close()
|
||||
if e := []string{"4", "2", "1", "3"}; !reflect.DeepEqual(o, e) {
|
||||
if e := []string{"2", "1", "3"}; !reflect.DeepEqual(o, e) {
|
||||
t.Errorf("expected %+v, got %+v", e, o)
|
||||
}
|
||||
if e, g := "2 && 1 && 3", err.Error(); !reflect.DeepEqual(g, e) {
|
||||
t.Errorf("expected %+v, got %+v", e, g)
|
||||
}
|
||||
c1.Add(func() error { return nil })
|
||||
c1.AddWithError(func() error { return nil })
|
||||
if err = c1.Close(); err != nil {
|
||||
t.Errorf("expected no error, got %+v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user