-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
77 additions
and
104 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
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,53 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-present Datadog, Inc. | ||
|
||
// Package utils holds utils related files | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
type event interface{ comparable } | ||
|
||
// Listener describes the callback called by a notifier | ||
type Listener[O any] func(obj O) | ||
|
||
// Notifier describes a type that calls back listener that registered for a specific set of events | ||
type Notifier[E event, O any] struct { | ||
listenersLock sync.RWMutex | ||
listeners map[E][]Listener[O] | ||
} | ||
|
||
// RegisterListener registers an event listener | ||
func (n *Notifier[E, O]) RegisterListener(event E, listener Listener[O]) error { | ||
n.listenersLock.Lock() | ||
defer n.listenersLock.Unlock() | ||
|
||
if n.listeners != nil { | ||
n.listeners[event] = append(n.listeners[event], listener) | ||
} else { | ||
return fmt.Errorf("a listener was inserted before initialization") | ||
} | ||
return nil | ||
} | ||
|
||
func (n *Notifier[E, O]) NotifyListener(event E, obj O) { | ||
// notify listeners | ||
n.listenersLock.RLock() | ||
for _, l := range n.listeners[event] { | ||
l(obj) | ||
} | ||
n.listenersLock.RUnlock() | ||
|
||
} | ||
|
||
// NewNotifier returns a new notifier | ||
func NewNotifier[E event, O any]() *Notifier[E, O] { | ||
return &Notifier[E, O]{ | ||
listeners: make(map[E][]Listener[O]), | ||
} | ||
} |