Skip to content

state

import {
  saveState,
  loadState,
  registerLoadable
} from "exome/state";

function saveState

Saves given store instance and its children (even recursive) to string that can be later restored.

function saveState(store: Exome, readable?: boolean): string;
Example
class CounterStore extends Exome {
  public count = 5
 
  public increment() {
    this.count += 1
  }
}
 
{"$exome_id":"CounterStore-LS5WUJPF17SF","count":5}

function loadState

Loads saved store into existing store instance. It will rebuild all children stores too.

function loadState(store: Exome, state: string): any;
Example
const counterStore = new CounterStore()
const savedStore = `{"$exome_id":"CounterStore-LS5WUJPF17SF","count":200}`
 
CounterStore { count: 200, increment: [Function] }

function registerLoadable

For loadState to know what stores to build instances from, we must make sure we register them.

function registerLoadable(config: Record<string, new () => Exome>): void;
Example
registerLoadable({
  CounterStore,
})