-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable hot reloading with multiple clients connected
- Loading branch information
1 parent
dc42c84
commit aa3930f
Showing
2 changed files
with
58 additions
and
8 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,46 @@ | ||
package notify | ||
|
||
// Notifier allows one goroutine to send a notification to one or more other | ||
// goroutines. | ||
type Notifier struct { | ||
channels map[string]chan struct{} | ||
} | ||
|
||
func New() *Notifier { | ||
notifier := &Notifier{ | ||
channels: make(map[string]chan struct{}), | ||
} | ||
return notifier | ||
} | ||
|
||
func (notifier *Notifier) Register(id string) { | ||
notifier.channels[id] = make(chan struct{}) | ||
} | ||
|
||
func (notifier *Notifier) Notify() { | ||
for _, channel := range notifier.channels { | ||
channel <- struct{}{} | ||
} | ||
} | ||
|
||
func (notifier *Notifier) Get(id string) chan struct{} { | ||
channel, ok := notifier.channels[id] | ||
if ok { | ||
return channel | ||
} | ||
return nil | ||
} | ||
|
||
func (notifier *Notifier) Close(id string) { | ||
channel, ok := notifier.channels[id] | ||
if ok { | ||
close(channel) | ||
delete(notifier.channels, id) | ||
} | ||
} | ||
|
||
func (notifier *Notifier) CloseAll() { | ||
for id := range notifier.channels { | ||
notifier.Close(id) | ||
} | ||
} |