move gotd fork into repo. (#111)
- update to latest telegram layer - remove some references to fields in tg.Entities that don't exist in the schema - originally added here: https://github.com/beeper/td/commit/820929062a2ba0104397bc01235ab58a9cff780e - referenced here - https://github.com/mautrix/telegramgo/commit/124f0967ed195b5a380c9bd02e170ada9710dde3 - https://github.com/mautrix/telegramgo/commit/4205047aab2e0639217148b5d125bfaab668bd8e
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package tdsync
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Supervisor is simple task group primitive to control multiple
|
||||
// long-live tasks.
|
||||
// Unlike Groups, Supervisor does not cancel when one task is failed.
|
||||
// Unlike WaitGroup and errgroup.Group this is not allowed to use zero value.
|
||||
type Supervisor struct {
|
||||
wg sync.WaitGroup
|
||||
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
|
||||
onError func(err error)
|
||||
}
|
||||
|
||||
// NewSupervisor creates new Supervisor.
|
||||
func NewSupervisor(parent context.Context) *Supervisor {
|
||||
ctx, cancel := context.WithCancel(parent)
|
||||
|
||||
return &Supervisor{
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
}
|
||||
}
|
||||
|
||||
// WithErrorHandler sets tasks error handler
|
||||
// Must be called before any Go calls.
|
||||
func (s *Supervisor) WithErrorHandler(h func(err error)) *Supervisor {
|
||||
s.onError = h
|
||||
return s
|
||||
}
|
||||
|
||||
// Go calls the given function in a new goroutine.
|
||||
func (s *Supervisor) Go(task func(ctx context.Context) error) {
|
||||
s.wg.Add(1)
|
||||
go func() {
|
||||
defer s.wg.Done()
|
||||
|
||||
if err := task(s.ctx); err != nil {
|
||||
if s.onError != nil {
|
||||
s.onError(err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Cancel cancels all goroutines in group.
|
||||
//
|
||||
// Note: context cancellation error can be returned by Wait().
|
||||
func (s *Supervisor) Cancel() {
|
||||
s.cancel()
|
||||
}
|
||||
|
||||
// Wait blocks until all function calls from the Go method have returned, then
|
||||
// returns the first non-nil error (if any) from them.
|
||||
func (s *Supervisor) Wait() error {
|
||||
s.wg.Wait()
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user