-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added rough (almost concept) implementation of Yeet and Snag - error …
…handling functions
- Loading branch information
Dima Kossovich
committed
Oct 6, 2023
1 parent
46875e9
commit 8eba1ad
Showing
7 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package internalpipe | ||
|
||
import ( | ||
"sync" | ||
"unsafe" | ||
) | ||
|
||
type ErrHandler func(err error) | ||
|
||
type Yeti struct { | ||
Errs []error | ||
Handlers []ErrHandler | ||
Pipe2Hdlrs map[unsafe.Pointer][]ErrHandler | ||
EMx *sync.Mutex | ||
HMx *sync.Mutex | ||
} | ||
|
||
func (y *Yeti) Yeet(err error) { | ||
y.EMx.Lock() | ||
y.Errs = append(y.Errs, err) | ||
y.EMx.Unlock() | ||
} | ||
|
||
func (y *Yeti) Snag(handler ErrHandler) { | ||
y.Handlers = append(y.Handlers, handler) | ||
} | ||
|
||
func (y *Yeti) SnagPipe(p unsafe.Pointer, h ErrHandler) { | ||
y.HMx.Lock() | ||
if hdlrs, ok := y.Pipe2Hdlrs[p]; ok { | ||
y.Pipe2Hdlrs[p] = append(hdlrs, h) | ||
} else { | ||
// FIXME: use constant, remove else | ||
y.Pipe2Hdlrs[p] = append(make([]ErrHandler, 0, 10), h) | ||
} | ||
y.HMx.Unlock() | ||
} | ||
|
||
type yeti interface { | ||
Yeet(err error) | ||
SnagPipe(p unsafe.Pointer, h ErrHandler) | ||
// TODO: Handle should be called after each Pipe function eval | ||
Handle(p unsafe.Pointer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package pipe | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/koss-null/funcfrog/internal/internalpipe" | ||
) | ||
|
||
const ( | ||
initErrsAmount = 10 | ||
initHandlersAmount = 5 | ||
) | ||
|
||
type Yeti interface { | ||
// yeet an error | ||
Yeet(err error) | ||
// snag and handle the error | ||
Snag(handler func(err error)) | ||
} | ||
|
||
// FIXME | ||
func Yeet() Yeti { | ||
return &internalpipe.Yeti{ | ||
Errs: make(map[*any][]error, initErrsAmount), | ||
Handlers: make(map[*any][]internalpipe.ErrHandler, initHandlersAmount), | ||
Mx: &sync.Mutex{}, | ||
} | ||
} |