forked from MetaCubeX/mihomo
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'MetaCubeX:Alpha' into Alpha
- Loading branch information
Showing
32 changed files
with
768 additions
and
254 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
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,50 @@ | ||
package utils | ||
|
||
import ( | ||
"io" | ||
"sync" | ||
|
||
list "github.com/bahlo/generic-list-go" | ||
) | ||
|
||
type Callback[T any] struct { | ||
list list.List[func(T)] | ||
mutex sync.RWMutex | ||
} | ||
|
||
func NewCallback[T any]() *Callback[T] { | ||
return &Callback[T]{} | ||
} | ||
|
||
func (c *Callback[T]) Register(item func(T)) io.Closer { | ||
c.mutex.RLock() | ||
defer c.mutex.RUnlock() | ||
element := c.list.PushBack(item) | ||
return &callbackCloser[T]{ | ||
element: element, | ||
callback: c, | ||
} | ||
} | ||
|
||
func (c *Callback[T]) Emit(item T) { | ||
c.mutex.RLock() | ||
defer c.mutex.RUnlock() | ||
for element := c.list.Front(); element != nil; element = element.Next() { | ||
go element.Value(item) | ||
} | ||
} | ||
|
||
type callbackCloser[T any] struct { | ||
element *list.Element[func(T)] | ||
callback *Callback[T] | ||
once sync.Once | ||
} | ||
|
||
func (c *callbackCloser[T]) Close() error { | ||
c.once.Do(func() { | ||
c.callback.mutex.Lock() | ||
defer c.callback.mutex.Unlock() | ||
c.callback.list.Remove(c.element) | ||
}) | ||
return nil | ||
} |
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
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
Oops, something went wrong.