GlobalHookRegistry#
- class brainstate.GlobalHookRegistry(config=None)[source]#
Singleton registry for global hooks that apply to all State instances.
Global hooks are executed before instance-specific hooks for each operation. This is useful for system-wide monitoring, logging, or validation.
The global registry is a singleton, accessed via GlobalHookRegistry.instance().
- Thread Safety:
The global registry is thread-safe, using the same locking mechanism as HookManager.
Examples
>>> # Register a global hook that logs all state reads >>> def log_all_reads(ctx): ... print(f"Global: Reading {ctx.state_name}") >>> handle = GlobalHookRegistry.instance().register_hook('read', log_all_reads) >>> >>> # Now all State instances will trigger this hook >>> import brainstate >>> s1 = brainstate.State(1) >>> s2 = brainstate.State(2) >>> _ = s1.value # Prints: Global: Reading None >>> _ = s2.value # Prints: Global: Reading None