remove duplicate files #8
@@ -1,37 +0,0 @@
|
|||||||
package util
|
|
||||||
|
|
||||||
type Distributer[T IClone[T]] struct {
|
|
||||||
queue chan T
|
|
||||||
hooks []func(T)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Distributer[T]) Init() {
|
|
||||||
d.queue = make(chan T, 100)
|
|
||||||
d.hooks = make([]func(T), 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Distribute a copy of an item to every hook that has described to this
|
|
||||||
// Collector.
|
|
||||||
func (d *Distributer[T]) Publish(item T) {
|
|
||||||
d.queue <- item
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add a new hook to the Collector. The hook will be called async whenever a
|
|
||||||
// new item is published.
|
|
||||||
func (d *Distributer[T]) Subscribe(hook func(T)) {
|
|
||||||
d.hooks = append(d.hooks, hook)
|
|
||||||
if len(d.hooks) == 1 {
|
|
||||||
go d.runner()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Will be started to run async when Subscribe is first called. Whenever
|
|
||||||
// Publish is called the runner will distribute a clone of the new item to
|
|
||||||
// every hook.
|
|
||||||
func (d *Distributer[T]) runner() {
|
|
||||||
for val := range d.queue {
|
|
||||||
for _, f := range d.hooks {
|
|
||||||
go f(val.Clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user