-
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.
chore:import-repos Importing repos to org
- Loading branch information
0 parents
commit cd63faa
Showing
5 changed files
with
406 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2022 Firechain Foundation | ||
|
||
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. | ||
|
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,76 @@ | ||
# Emitter. | ||
|
||
A flexible event emitter inspired by NodeJS's `event-emitter`. | ||
|
||
Emits events. Doesn't ask for raises. | ||
|
||
## Usage | ||
|
||
```go | ||
package main | ||
|
||
import( | ||
"fmt" | ||
"github.com/firechain-network/emitter" | ||
) | ||
|
||
func main(){ | ||
echo := fmt.Println | ||
emitter := Emitter.Construct() | ||
|
||
// with inline functions: | ||
emitter.On("0x021f…400f:Tick", func(args ... interface{}){ | ||
// ... | ||
}) | ||
|
||
emitter.Once("0x021f…400f:Tick", func(args ...interface{}){ | ||
// ... | ||
}) | ||
|
||
// with defined functions: | ||
handler := func(args ...interface{}){ | ||
// ... | ||
} | ||
|
||
emitter.On("0x021f…400f:Tick", handler) | ||
|
||
// emitting: | ||
emitter.EmitAsync("0x021f…400f:Tick", nil) | ||
emitter.EmitAsync("0x021f…400f:Tick", []interface{}{"foo", "bar"}) | ||
emitter.EmitSync("0x021f…400f:Tick", nil) | ||
emitter.EmitSync("0x021f…400f:Tick", []interface{}{"foo", "bar"}) | ||
|
||
// wildcards: | ||
emitter.On("0x021f…400f:*", handler) | ||
emitter.On("**", handler) | ||
|
||
// removing listeners: | ||
emitter.RemoveListener("0x021f…400f:Tick", handler) | ||
|
||
// remove all listeners from a specific event: | ||
emitter.RemoveAllListeners("0x021f…400f:Tick") | ||
|
||
// remove all listener registrations globally: | ||
emitter.RemoveAllListeners() | ||
|
||
// fetch listeners: | ||
emitter.Listeners("0x021f…400f:Tick") // nil if empty | ||
|
||
// fetch the number of listeners for a specific event: | ||
emitter.ListenersCount("0x021f…400f:Tick") | ||
|
||
} | ||
``` | ||
|
||
## Types | ||
|
||
```go | ||
type Emitter struct { | ||
listeners map[interface{}][]Listener | ||
} | ||
|
||
type Listener struct { | ||
callback func(...interface{}) | ||
once bool | ||
} | ||
``` |
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,3 @@ | ||
module github.com/firechain-network/emitter | ||
|
||
go 1.12 |
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,173 @@ | ||
package Emitter | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
func eventMatchPattern(eventName, pattern []rune) bool { | ||
for len(pattern) > 0 { | ||
switch pattern[0] { | ||
case '*': | ||
return eventMatchPattern(eventName, pattern[1:]) || (len(eventName) > 0 && eventMatchPattern(eventName[1:], pattern)) | ||
|
||
default: | ||
if len(eventName) == 0 || eventName[0] != pattern[0] { | ||
return false | ||
} | ||
} | ||
|
||
eventName = eventName[1:] | ||
pattern = pattern[1:] | ||
} | ||
|
||
return len(eventName) == 0 && len(pattern) == 0 | ||
} | ||
|
||
type Emitter struct { | ||
listeners map[interface{}][]Listener | ||
mutex *sync.Mutex | ||
} | ||
|
||
type Listener struct { | ||
callback func(...interface{}) | ||
once bool | ||
} | ||
|
||
func Construct() *Emitter { | ||
return &Emitter{ | ||
make(map[interface{}][]Listener), | ||
&sync.Mutex{}, | ||
} | ||
} | ||
|
||
func (emitter *Emitter) Destruct() { | ||
emitter = nil | ||
} | ||
|
||
func (emitter *Emitter) AddListener(event string, callback func(...interface{})) *Emitter { | ||
return emitter.On(event, callback) | ||
} | ||
|
||
func (emitter *Emitter) On(event string, callback func(...interface{})) *Emitter { | ||
emitter.mutex.Lock() | ||
if _, ok := emitter.listeners[event]; !ok { | ||
emitter.listeners[event] = []Listener{} | ||
} | ||
emitter.listeners[event] = append(emitter.listeners[event], Listener{callback, false}) | ||
emitter.mutex.Unlock() | ||
|
||
emitter.EmitSync("newListener", []interface{}{event, callback}) | ||
return emitter | ||
} | ||
|
||
func (emitter *Emitter) Once(event string, callback func(...interface{})) *Emitter { | ||
emitter.mutex.Lock() | ||
if _, ok := emitter.listeners[event]; !ok { | ||
emitter.listeners[event] = []Listener{} | ||
} | ||
emitter.listeners[event] = append(emitter.listeners[event], Listener{callback, true}) | ||
emitter.mutex.Unlock() | ||
|
||
emitter.EmitSync("newListener", []interface{}{event, callback}) | ||
return emitter | ||
} | ||
|
||
func (emitter *Emitter) RemoveListener(event string, callback func(...interface{})) *Emitter { | ||
return emitter.removeListenerInternal(event, callback, false) | ||
} | ||
|
||
func (emitter *Emitter) removeListenerInternal(event string, callback func(...interface{}), suppress bool) *Emitter { | ||
emitter.mutex.Lock() | ||
|
||
if _, ok := emitter.listeners[event]; !ok { | ||
emitter.mutex.Unlock() | ||
return emitter | ||
} | ||
|
||
for k, v := range emitter.listeners[event] { | ||
if reflect.ValueOf(v.callback).Pointer() == reflect.ValueOf(callback).Pointer() { | ||
emitter.listeners[event] = append(emitter.listeners[event][:k], emitter.listeners[event][k+1:]...) | ||
|
||
emitter.mutex.Unlock() | ||
|
||
if !suppress { | ||
emitter.EmitSync("removeListener", []interface{}{event, callback}) | ||
} | ||
return emitter | ||
} | ||
} | ||
|
||
emitter.mutex.Unlock() | ||
return emitter | ||
} | ||
|
||
func (emitter *Emitter) RemoveAllListeners(event interface{}) *Emitter { | ||
emitter.mutex.Lock() | ||
defer emitter.mutex.Unlock() | ||
|
||
if event == nil { | ||
emitter.listeners = make(map[interface{}][]Listener) | ||
return emitter | ||
} | ||
if _, ok := emitter.listeners[event]; !ok { | ||
return emitter | ||
} | ||
|
||
delete(emitter.listeners, event) | ||
return emitter | ||
} | ||
|
||
func (emitter *Emitter) Listeners(event string) []Listener { | ||
emitter.mutex.Lock() | ||
defer emitter.mutex.Unlock() | ||
|
||
listeners := make([]Listener, 0) | ||
for eventPattern, lis := range emitter.listeners { | ||
shouldAdd := false | ||
|
||
// add generic "**" bound listeners | ||
shouldAdd = shouldAdd || eventPattern.(string) == "**" | ||
// add listener bound on full name event | ||
shouldAdd = shouldAdd || eventPattern.(string) == event | ||
// add listeners that have matching wildcard pattern | ||
shouldAdd = shouldAdd || | ||
(strings.Contains(eventPattern.(string), "*") && | ||
eventMatchPattern([]rune(event), []rune(eventPattern.(string)))) | ||
|
||
if shouldAdd { | ||
listeners = append(listeners, lis...) | ||
} | ||
} | ||
|
||
return listeners | ||
} | ||
|
||
// ListenersCount() - return the count of listeners in the speicifed event | ||
func (emitter *Emitter) ListenersCount(event string) int { | ||
return len(emitter.Listeners(event)) | ||
} | ||
|
||
// EmitSync() - run all listeners of the specified event in synchronous mode | ||
func (emitter *Emitter) EmitSync(event string, args ...interface{}) *Emitter { | ||
for _, v := range emitter.Listeners(event) { | ||
if v.once { | ||
emitter.removeListenerInternal(event, v.callback, true) | ||
} | ||
v.callback(args...) | ||
} | ||
|
||
return emitter | ||
} | ||
|
||
// EmitAsync() - run all listeners of the specified event in asynchronous mode using goroutines | ||
func (emitter *Emitter) EmitAsync(event string, args []interface{}) *Emitter { | ||
for _, v := range emitter.Listeners(event) { | ||
if v.once { | ||
emitter.removeListenerInternal(event, v.callback, true) | ||
} | ||
go v.callback(args...) | ||
} | ||
return emitter | ||
} |
Oops, something went wrong.