-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemitr.go
82 lines (64 loc) · 1.86 KB
/
emitr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package emitr
import "sync"
// Emitter represents an event emitter.
type Emitter struct {
lock sync.Mutex
once map[string][]func()
when map[string][]func()
}
// Once adds a callback which is called only once when an
// event is emitted, and then removed immediately after.
func (e *Emitter) Once(name string, fn func()) {
e.lock.Lock()
defer e.lock.Unlock()
if e.once == nil {
e.once = make(map[string][]func())
}
e.once[name] = append(e.once[name], fn)
}
// When adds a callback which is called each and every
// time that an event is emitted.
func (e *Emitter) When(name string, fn func()) {
e.lock.Lock()
defer e.lock.Unlock()
if e.when == nil {
e.when = make(map[string][]func())
}
e.when[name] = append(e.when[name], fn)
}
// Emit runs callbacks for the specified event name,
// and ensures that one-off events, are removed when
// the event has finished emitting.
func (e *Emitter) Emit(name string) {
e.lock.Lock()
defer e.lock.Unlock()
if e.when != nil {
if _, ok := e.when[name]; ok {
for i := len(e.when[name]) - 1; i >= 0; i-- {
e.when[name][i]()
}
}
}
if e.once != nil {
if _, ok := e.once[name]; ok {
for i := len(e.once[name]) - 1; i >= 0; i-- {
e.once[name][i]()
e.once[name][i] = nil
e.once[name] = e.once[name][:i]
}
}
}
}