1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-12 20:27:30 +08:00
otto/registry/registry.go
Steven Hartland 98effe01d8
chore: update ci versions (#519)
Update go, golangci-lint and action versions.

Address new lint failures flagged by updated golangci-lint version.
2024-04-13 17:05:50 +01:00

53 lines
1.0 KiB
Go

// 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 {
source func() string
active bool
}
// 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
}