puepy.reactivity
Provides the base classes for PuePy's reactivity system independent of web concerns. These classes are not intended to be used directly, but could be useful for implementing a similar system in a different context.
Classes:
Name | Description |
---|---|
Listener |
A simple class that notifies a collection of callback functions when its |
ReactiveDict |
A dictionary that notifies a listener when it is updated |
Listener
A simple class that allows you to register callbacks and then notify them all at once.
Attributes:
Name | Type | Description |
---|---|---|
callbacks |
list of callables
|
A list of callback functions to be called when |
Source code in puepy/reactivity.py
add_callback(callback)
Adds a callback function to the listener.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
callable
|
The callback function to be added |
required |
notify(*args, **kwargs)
Notify method
Executes each callback function in the callbacks list by passing in the given arguments and keyword arguments. If an exception occurs during the callback execution, it is logged using the logging library.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args |
Variable length argument list. |
()
|
|
**kwargs |
Arbitrary keyword arguments. |
{}
|
Source code in puepy/reactivity.py
remove_callback(callback)
Removes a callback function from the listener.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
callable
|
The callback to be removed |
required |
ReactiveDict
Bases: dict
A dictionary that notifies a listener when it is updated.
Attributes:
Name | Type | Description |
---|---|---|
listener |
Listener
|
A listener object that is notified when the dictionary is updated |
key_listeners |
dict
|
A dictionary of listeners that are notified when a specific key is updated |
Source code in puepy/reactivity.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
add_key_listener(key, callback)
Adds a key listener to the object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
The key for which the listener will be added. |
required |
callback |
callable
|
The callback function to be executed when the key event is triggered. |
required |
Source code in puepy/reactivity.py
mutate(*keys)
To be used as a context manager, this method is for either deferring all notifications until a change has been completed and/or notifying listeners when "deep" changes are made that would have gone undetected by __setitem__
.
Examples:
with reactive_dict.mutate("my_list", "my_dict"):
reactive_dict["my_list"].append("spam")
reactive_dict["my_dict"]["spam"] = "eggs"
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*keys |
A variable number of keys to update the notifications pending attribute with. If no keys are provided, all keys in the object will be updated. |
()
|
Returns:
Type | Description |
---|---|
The reactive dict itself, which stylistically could be nice to use in a |
Source code in puepy/reactivity.py
notify(*keys)
Notifies the listener and key listeners that the object has been updated.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*keys |
A variable number of keys to be modified for key-specific listeners. |
()
|
Source code in puepy/reactivity.py
Stateful
A class that provides a reactive state management system for components. A
Source code in puepy/reactivity.py
add_context(name, value)
Adds contxt from a reactive dict to be reacted on by the component.
initial()
To be overridden in subclasses, the initial()
method defines the initial state of the stateful object.
Returns:
Type | Description |
---|---|
dict
|
Initial component state |
on_state_change(context, key, value)
To be overridden in subclasses, this method is called whenever the state of the component changes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
What context the state change occured in |
required | |
key |
The key modified |
required | |
value |
The new value |
required |