Skip to content

Instantly share code, notes, and snippets.

@katallaxie
Last active September 4, 2017 07:03
Show Gist options
  • Save katallaxie/b3153ad703ee99ded9c2184beb3cb479 to your computer and use it in GitHub Desktop.
Save katallaxie/b3153ad703ee99ded9c2184beb3cb479 to your computer and use it in GitHub Desktop.
Simple event observer in Golang
// Copyright © 2017 Sebastian Döll <sebastian@katallaxie.me>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package observer
import "sync"
import "time"
// Observer holds channels to publish events to
type Observer struct {
channels map[chan<- interface{}]struct{}
sync.RWMutex
}
// New returns a new Observer
func New() *Observer {
return &Observer{channels: make(map[chan<- interface{}]struct{})}
}
// Subscribe attaches a channel to an Observer
func (o *Observer) Subscribe(channel chan<- interface{}) {
o.Lock()
defer o.Unlock()
o.channels[channel] = struct{}{}
}
// Unsubscribe dettaches a channel from an Observer
func (o *Observer) Unsubscribe(channel chan<- interface{}) error {
o.Lock()
defer o.Unlock()
_, ok := o.channels[channel]
if !ok {
return &EventNotFound{}
}
delete(o.channels, channel)
return nil
}
// Delete dettaches all the channels from the Observer
func (o *Observer) Delete(channel chan<- interface{}) error {
o.Lock()
defer o.Unlock()
for channel := range o.channels {
close(channel)
}
o.channels = make(map[chan<- interface{}]struct{})
return nil
}
// Publish channels data to the attached channels
func (o *Observer) Publish(data interface{}) error {
o.RLock()
defer o.RUnlock()
for channel := range o.channels {
channel <- data
}
return nil
}
// PublishWithTimeout channels data to the attached channels with an timeout
func (o *Observer) PublishWithTimeout(data interface{}, timeout time.Duration) error {
o.RLock()
defer o.RUnlock()
for channel := range o.channels {
select {
case channel <- data:
case <-time.After(timeout):
}
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment