Share state between pages
One native app can show several bundles, or several pages of one project, and each is its own Angular app. LynxSessionStorage gives them a shared key-value store that lasts as long as the app is running.
It isn't the browser's sessionStorage, which belongs to one tab. Every Lynx view in the host app (the native app that runs your bundles) sees the same keys. The store empties when the app quits: to keep values after that, use LynxLocalStorage.
The web preview and unit tests have no native store, so LynxSessionStorage keeps values in memory instead. Every method works, but each page gets its own store, so nothing is shared.
Write a value and watch it
setItem(key, value)stores a value under a key. Store a plain object, or a single value such as a string or a number.watch(key)returns a signal for the key. It starts asundefined, then holds the stored value.- Call
watch()in a field initializer or constructor. It stops listening when the component is destroyed. Elsewhere, pass an injector:watch('count', { injector }). - Call
setItem()again after a change. On a device the store keeps a copy, so changing an object after you store it doesn't update what other pages read.
Read a value once
getItem() returns a Promise of the stored value. Handle a key that was never set with ??, as the first example does.
When another page's changes appear
A watch() signal updates when any page writes the key:
- Right away when the same page calls
setItem(). - Shortly after another page writes it. Lynx sends change notifications only to the background thread, so AngularLynx passes them on to the copy of your app that draws the UI.
Subscribe with a callback
For code that doesn't use signals, subscribe() calls you back when the key changes:
Unsubscribe yourself: unlike watch(), a subscription doesn't end with the component.
