diff options
| author | faiface <faiface@ksp.sk> | 2017-08-29 00:35:28 +0200 |
|---|---|---|
| committer | faiface <faiface@ksp.sk> | 2017-08-29 00:35:28 +0200 |
| commit | 09db3d0b721685fda1cee71833ec4dd0fe1b4c9a (patch) | |
| tree | 7473dfeb9d1b4664c25be1e7cc939a804855c650 /event/dispatch.go | |
| parent | 0c433c89a218c428cbc6dc093d4b4d6dd8174114 (diff) | |
| download | gui-09db3d0b721685fda1cee71833ec4dd0fe1b4c9a.zip | |
start over, concurrency full on
Diffstat (limited to 'event/dispatch.go')
| -rw-r--r-- | event/dispatch.go | 91 |
1 files changed, 0 insertions, 91 deletions
diff --git a/event/dispatch.go b/event/dispatch.go deleted file mode 100644 index 738161a..0000000 --- a/event/dispatch.go +++ /dev/null @@ -1,91 +0,0 @@ -package event - -import ( - "bytes" - "fmt" - "strings" - "sync" -) - -const Sep = "/" - -func Sprint(a ...interface{}) string { - var buf bytes.Buffer - for i := range a { - if i > 0 { - buf.WriteString(Sep) - } - fmt.Fprint(&buf, a[i]) - } - return buf.String() -} - -func Sscan(event string, a ...interface{}) { - for i, part := range strings.Split(event, Sep) { - fmt.Sscan(part, a[i]) - } -} - -type Dispatch struct { - mu sync.Mutex - handlers []func(event string) bool - trie map[string]*Dispatch -} - -func (d *Dispatch) Event(pattern string, handler func(event string) bool) { - if pattern == "" { - d.event(nil, handler) - return - } - d.event(strings.Split(pattern, Sep), handler) -} - -func (d *Dispatch) Happen(event string) bool { - if event == "" { - return d.happen(nil) - } - return d.happen(strings.Split(event, Sep)) -} - -func (d *Dispatch) event(pattern []string, handler func(event string) bool) { - d.mu.Lock() - defer d.mu.Unlock() - - if len(pattern) == 0 { - d.handlers = append(d.handlers, handler) - return - } - - if d.trie == nil { - d.trie = make(map[string]*Dispatch) - } - if d.trie[pattern[0]] == nil { - d.trie[pattern[0]] = &Dispatch{} - } - d.trie[pattern[0]].event(pattern[1:], handler) -} - -func (d *Dispatch) happen(event []string) bool { - d.mu.Lock() - handlers := d.handlers - d.mu.Unlock() - - for _, handler := range handlers { - if handler(strings.Join(event, Sep)) { - return true - } - } - - if len(event) == 0 { - return false - } - - d.mu.Lock() - next := d.trie[event[0]] - d.mu.Unlock() - - if next != nil { - return next.happen(event[1:]) - } - return false -} |