hook

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package hook provides functionality for the Tako framework.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cardinality

type Cardinality int

Cardinality defines how many providers can attach to a hook slot.

const (
	// SingleHook allows only one provider per slot; extra registrations are
	// logged as warnings and the first registered provider wins.
	SingleHook Cardinality = iota
	// MultiHook allows any number of providers; all are called and results
	// collected in registration order.
	MultiHook
)

type MetricsCollector

type MetricsCollector interface {
	RecordHook(id string, dur time.Duration)
	ClearHookStats(id string)
}

MetricsCollector defines the interface for profiling hooks.

type Provider

type Provider func() any

Provider is a function that returns a hook component, called on every render tick.

type Registry

type Registry interface {
	// Set registers a provider for a single-slot hook.
	// If multiple providers are registered, the first wins and a warning is logged.
	Set(hookID string, provider Provider) func()

	// Add registers a provider for a multi-slot hook.
	// All registered providers are called and their results collected.
	Add(hookID string, provider Provider) func()

	// Register registers a provider with an explicit cardinality.
	// Prefer Set or Add for clarity.
	Register(hookID string, provider Provider, cardinality Cardinality) func()

	// Get invokes the first registered provider for the given slot and
	// returns its result. Returns nil if no provider is registered.
	Get(hookID string) any

	// All invokes all registered providers for the given slot and returns
	// their results in registration order. Returns nil if no providers are registered.
	All(hookID string) []any

	// SetMetricsCollector attaches a profiler that records hook execution durations.
	SetMetricsCollector(collector MetricsCollector)

	// HookStats returns a map of hook slot names to the number of registered providers.
	HookStats() map[string]int
}

Registry manages named hook slots and their registered providers.

Fluent usage from application code:

app.Hooks().Set("render:toolbar", func() any { return myToolbar })
app.Hooks().Add("app.sidebar",    func() any { return sidebarItem })
result  := app.Hooks().Get("render:toolbar")
results := app.Hooks().All("app.sidebar")

Direct usage (e.g. from a plugin via ctx.Hooks()):

hooks.Set("render:toolbar", provider)
hooks.Get("render:toolbar")

func NewRegistry

func NewRegistry() Registry

NewRegistry creates a new Hook Registry.

Jump to

Keyboard shortcuts

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