mirror of
https://github.com/robertkrimen/otto
synced 2025-10-19 19:55:30 +08:00

Enable more linters, address the issues and do a major naming refactor to use golang lower camelCase identifiers for types, functions, methods and variable names. Also: * Clean up inline generation so it doesn't rely on temporary variables. * Remove unused functions generated by inline.pl.
53 lines
1.0 KiB
Go
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 {
|
|
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
|
|
}
|