One instance per application
use() gives every view and window the same instance; key= gives one per key; tests start fresh.
janelkit-store Shared application data: stores with actions, controlled writes and change tracking, one instance per application, and persistence. pip install janelkit-storeuse(): the instance of the running application
use(Cart) returns the instance of the running application: the same one in every view and window. It is created the first time it is asked for and released when the application closes. Without a running application, use raises StoreError.
In tests with janelkit.inspect.mount, each mount starts with new instances: a test never sees the data of the previous one. Cart() creates a loose instance, without an application, and Cart(items=[...]) gives it initial values.
One instance per key
use(Document, key=path) returns one instance per key, for data that exists several times, such as open documents. setup(self, key) receives the key. When asked for in the script of a component, a keyed instance is released, with its teardown, when the last component that asked for it is destroyed.
from janelkit_store import store, use
@store
class Document:
text: str = ""
path: str = ""
def setup(self, key):
self.path = key
# In the script of an editor component:
document = use(Document, key=path.value)CopyCommon mistakes
- A keyed instance lives while some component uses it. To keep it when no view shows it, ask for it from a component that stays mounted.
English documentation based on the JanelKit source. For additional details, browse the source docs ↗.