registry

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package registry provides a generic, type-safe registry system for managing triggers, power-ups, and actions. It supports automatic registration through init() functions.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetPowerUpFactory

func GetPowerUpFactory(name string) (types.PowerUpFactory, error)

GetPowerUpFactory retrieves a power-up factory by name.

func GetTriggerFactory

func GetTriggerFactory(name string) (types.TriggerFactory, error)

GetTriggerFactory retrieves a trigger factory by name.

func MustGet

func MustGet[T any](reg Registry[T], name string) T

MustGet retrieves an item and panics if not found This is useful when the item must exist

func MustRegister

func MustRegister[T any](reg Registry[T], name string, item T)

MustRegister registers an item and panics if registration fails This is useful for init() functions where registration errors are programming errors

func RegisterPowerUpFactory

func RegisterPowerUpFactory(name string, factory types.PowerUpFactory) error

RegisterPowerUpFactory registers a factory function for creating power-ups.

func RegisterTriggerFactory

func RegisterTriggerFactory(name string, factory types.TriggerFactory) error

RegisterTriggerFactory registers a factory function for creating triggers.

Types

type Registry

type Registry[T any] interface {
	// Register adds an item to the registry
	Register(name string, item T) error

	// Get retrieves an item from the registry
	Get(name string) (T, error)

	// Remove removes an item from the registry
	Remove(name string) error

	// List returns all registered names
	List() []string

	// Has checks if an item is registered
	Has(name string) bool

	// Clear removes all items from the registry
	Clear()

	// Count returns the number of registered items
	Count() int
}

Registry is a generic, thread-safe registry for storing and retrieving items by name

Example

Example usage

// Create a registry for string handlers
reg := New[func() string]()

// Register some handlers
_ = reg.Register("greeting", func() string { return "Hello, World!" })
_ = reg.Register("farewell", func() string { return "Goodbye!" })

// List all registered handlers
names := reg.List()
sort.Strings(names)
fmt.Println("Registered handlers:", names)

// Get and execute a handler
if handler, err := reg.Get("greeting"); err == nil {
	fmt.Println(handler())
}
Output:
Registered handlers: [farewell greeting]
Hello, World!

func GetRegistry

func GetRegistry[T any]() Registry[T]

GetRegistry returns the global registry for the specified type. It uses a type switch to return the correct singleton instance.

func New

func New[T any]() Registry[T]

New creates a new Registry instance

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL