registry.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Package registry is an experimental package to facilitate altering the otto runtime via import.
  2. //
  3. // This interface can change at any time.
  4. package registry
  5. var registry []*Entry = make([]*Entry, 0)
  6. // Entry represents a registry entry.
  7. type Entry struct {
  8. source func() string
  9. active bool
  10. }
  11. // newEntry returns a new Entry for source.
  12. func newEntry(source func() string) *Entry {
  13. return &Entry{
  14. active: true,
  15. source: source,
  16. }
  17. }
  18. // Enable enables the entry.
  19. func (e *Entry) Enable() {
  20. e.active = true
  21. }
  22. // Disable disables the entry.
  23. func (e *Entry) Disable() {
  24. e.active = false
  25. }
  26. // Source returns the source of the entry.
  27. func (e Entry) Source() string {
  28. return e.source()
  29. }
  30. // Apply applies callback to all registry entries.
  31. func Apply(callback func(Entry)) {
  32. for _, entry := range registry {
  33. if !entry.active {
  34. continue
  35. }
  36. callback(*entry)
  37. }
  38. }
  39. // Register registers a new Entry for source.
  40. func Register(source func() string) *Entry {
  41. entry := newEntry(source)
  42. registry = append(registry, entry)
  43. return entry
  44. }