https://github.com/gianlucaguarini/go-observable
It allows you to send and receive events with a tiny simple API
https://github.com/gianlucaguarini/go-observable
Last synced: 7 months ago
JSON representation
It allows you to send and receive events with a tiny simple API
- Host: GitHub
- URL: https://github.com/gianlucaguarini/go-observable
- Owner: GianlucaGuarini
- License: mit
- Created: 2016-01-17T00:14:44.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-08-29T20:16:14.000Z (almost 7 years ago)
- Last Synced: 2024-12-10T02:11:02.426Z (7 months ago)
- Language: Go
- Homepage:
- Size: 19.5 KB
- Stars: 242
- Watchers: 7
- Forks: 11
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# go-observable
[](https://travis-ci.org/GianlucaGuarini/go-observable)
[](https://coveralls.io/github/GianlucaGuarini/go-observable?branch=master)Golang script heavily inspired to [riot-observable](https://github.com/riot/observable)
It allows you to send and receive events with a tiny simple API (thread safe)
## Installation
```go
go get github.com/GianlucaGuarini/go-observable
```## Api
### New()
Create a new observable struct reference
```go
o := observable.New()
```### On(event string, fn interface{})
Subscribe a callback to a certain event key
```go
o.On("ready", func() {
// I am ready!
})
```You can subscribe your observable also to multiple events for example:
```go
o.On("stop error", func(args ...interface{}) {
// It will be called when the "error" or the "stop" event will be dispatched
// args[0] here is either "stop" or "error" depending on the Trigger call
})
```By using the `*` namespace you will be able to listen all the observable events
__IMPORTANT__
If you pass arguments to your listeners you should add them also to this listener
as well```go
o.On("*", func(args ...interface{}) {
// args[0] here will be the event key called in the Trigger method
// in this case "stop" and "start"
})
o.Trigger("stop").Trigger("start")
```### Off(event string, fn interface{})
Unsubscribe a callback from an event key
```go
onReady := func() {
// I am ready
}
o.On("ready", onReady)
// do your stuff...
o.Off("ready", onReady) // the onReady will not be called anymore
```By using the key `*` you will remove all the observed
```go
o.On("ready", func() {
// ready
})
o.On("stop", func() {
// stop
})o.Off("*") // kill all the listeners
o.Trigger("ready stop") // the callbacks above will never be called
```### One(event string, fn interface{})
Subscribe a callback in order to be called only once
```go
o.One("ready", func() {
// I am ready and I will not be called anymore
})
```### Trigger(event string, arguments ...interface{})
Call all the callbacks subscribed to a certain event
```go
o.On("message", func(message string) {
// do something with the message
})
o.Trigger("message", "Hello There!")
```