oracular / deserialize
Function: deserialize()
function deserialize(json, opts?): EngineState;Defined in: serialize.ts:67
Restores a state saved with serialize.
Always pass the app's current config: it is validated and replaces the stored snapshot, intents and signals no longer declared are dropped (α and β are recomputed from the kept contributions), and newly declared intents start at their prior. States saved by oracular 0.1.0 to 0.5.x are upgraded to the current shape on their own, and a state one version newer than STATE_VERSION (written by the next release, read after a rollback) is read as the current one, ignoring the fields it adds; migrate is only for a version this library does not know.
Parameters
| Parameter | Type | Description |
|---|---|---|
json | string | A string produced by serialize. |
opts? | DeserializeOptions | config: the app's current config. migrate: called as migrate(stored, version) when the stored version is neither STATE_VERSION nor one this library migrates itself; it must return a state of the current version. |
Returns
A validated state that satisfies the engine's invariants.
Throws
SyntaxError when json is not valid JSON.
Throws
TypeError when the stored data, the result of migrate, opts or opts.config has the wrong shape.
Throws
RangeError when the stored version is neither one this library reads (1 to one past STATE_VERSION) nor handled by a given migrate, when migrate does not return the current version, when a stored number is out of range, or when stored numbers are finite but too large to compute with (a tampered or corrupted state that would otherwise answer NaN).
Example
const saved = localStorage.getItem('oracular');
let state = saved ? deserialize(saved, { config }) : createState(config);