ECOSYSTEM / STORE

One instance per application

use() gives every view and window the same instance; key= gives one per key; tests start fresh.

Ecosystem package janelkit-store Shared application data: stores with actions, controlled writes and change tracking, one instance per application, and persistence. pip install janelkit-store
STORE · PAGE 4 / 8~1 min read
Alpha / 0.2 — These pages document JanelKit 0.2 and its packages. Each series (0.1, 0.2…) may change the public API, always with migration notes: see Changes and migration. The website uses Svelte; the desktop examples use Python and JanelKit, not JavaScript.

use(): 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.

python
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)
Copy

Common 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 ↗.