forked from robertkrimen/otto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
52 lines (44 loc) · 1.03 KB
/
registry.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
// Package registry is an experimental package to facilitate altering the otto runtime via import.
//
// This interface can change at any time.
package registry
var registry []*Entry = make([]*Entry, 0)
// Entry represents a registry entry.
type Entry struct {
active bool
source func() string
}
// newEntry returns a new Entry for source.
func newEntry(source func() string) *Entry {
return &Entry{
active: true,
source: source,
}
}
// Enable enables the entry.
func (e *Entry) Enable() {
e.active = true
}
// Disable disables the entry.
func (e *Entry) Disable() {
e.active = false
}
// Source returns the source of the entry.
func (e Entry) Source() string {
return e.source()
}
// Apply applies callback to all registry entries.
func Apply(callback func(Entry)) {
for _, entry := range registry {
if !entry.active {
continue
}
callback(*entry)
}
}
// Register registers a new Entry for source.
func Register(source func() string) *Entry {
entry := newEntry(source)
registry = append(registry, entry)
return entry
}