Quark #

Modules #

Quark v1.4.0 Documentation

Contents

quark/state #

onState #

Event triggered when any state is changed.

getState(name) #

Get a global state value by name. If not found in memory, try to read from storage if it's global.

getState(name: string)

Type parameters: T

setState(state,self?) #

Update state values (both local and global). Rules for keys:

  • Normal keys (no $ prefix): updated only on self (local state).
  • Keys starting with $ (e.g. $count):
    • Treated as global shared state.
    • The value is stored in GlobalStates.
    • All GlobalState controllers that subscribed to this key will receive an update.
  • Keys starting with $ (e.g. $theme):
    • Treated as global + persistent state.
    • Same as $key, but in addition the value is also written to storage under the key "_global_state_" + name.
    • On next load, if GlobalStates does not contain the key, it will be recovered from storage. Update order:
  • Collect all changed $ / $ keys and update GlobalStates (and storage if $).
  • Notify all other subscribed GlobalState controllers with the relevant subset of changes.
  • Finally, update self (if provided) using ViewController.prototype.setState and trigger the callback (if provided).
    setState(state: S, self: any, self?: GlobalState): Promise<void>

Parameters:

  • state — Key-value pairs to update.
  • self — The GlobalState instance that initiated the update (optional).
               Used to avoid notifying itself twice and to update its local state at the end.
    

Returns: resolves after all updates and callbacks are done. Type parameters: S

Class: GlobalState #

GlobalState #

A special ViewController that can participate in global state sharing. Any state key starting with $ will be synchronized across all GlobalState instances. Keys starting with $ are persisted in storage.

Type parameters: P, S Extends: ViewController<P,S>

globalstate.triggerLoad() #

Triggered when the controller is loaded. Synchronizes its global state keys with the GlobalStates.

triggerLoad()

globalstate.triggerUnload() #

Triggered when the controller is unloaded. Remove it from global subscribers if it was global.

triggerUnload()

Class: PersistentState #

PersistentState #

Extends GlobalState with persistence across component lifecycles. Each PersistentState instance gets a unique persistent ID.

Type parameters: P, S Extends: GlobalState<P,S>

persistentstate.persistentID #

Unique ID for each PersistentState class

readonly persistentID: 

persistentstate.saveState() #

Subclass can override this to define what to save persistently

saveState(): Partial<S> | null

persistentstate.recoveryState() #

Restore state from PersistentStates map

recoveryState(): Partial<S> | null

persistentstate.triggerLoad() #

When loading, recover state first, then proceed with global sync

triggerLoad()

persistentstate.triggerUnload() #

Called when the PersistentState controller is unloaded.

  • First calls the parent's triggerUnload (to clean up global subscriptions).
  • Then tries to save the component's state via saveState().
    • If a partial state is returned, store it in PersistentStates (in-memory cache).
    • Otherwise, remove any previously stored state for this component.
      triggerUnload()