Writing plugins¶
Plugins let you log or inspect reads and changes to reactive values. Signified calls your plugin's methods when these events happen.
Enable plugins¶
Hooks run while at least one plugin is registered with plugin_manager, as
shown below. With no plugin registered, reactive values skip them.
Available hooks¶
Each hook receives the reactive object as its value argument:
| Hook | When it runs |
|---|---|
created |
A reactive value is created. |
read |
Its current value is read. |
updated |
It sends a change notification. |
named |
It is given a name. |
Create and register a plugin¶
Implement the hooks you need with @hookimpl, then register the instance.
A plugin can also be a module of @hookimpl functions. This plugin counts new
reactive values:
from typing import Any
from signified import Signal, Variable
from signified.plugins import hookimpl, plugin_manager
class CreationCounter:
def __init__(self) -> None:
self.count = 0
@hookimpl
def created(self, value: Variable[Any]) -> None:
self.count += 1
print(f"Created {value:d}") # :d shows the object's type and ID.
plugin = CreationCounter()
plugin_manager.register(plugin)
x = Signal(1)
y = x + 1
print(plugin.count) # 2
plugin_manager.unregister(plugin)
Use logging in place of print if your application already has logging set
up. Unregister the plugin when you no longer need it. For a larger example,
see the access tracker,
which counts reads and writes for each reactive value.
When several plugins implement the same hook, the most recently registered one
runs first. register raises an error if the plugin is already registered, if
an @hookimpl method's name is not one of the hooks above, or if it does not
accept a value argument.