Synchronised state object across clients
// replace it with the actual host
import State from 'https://example.com/index.mjs'
const state = await State.get(localStorage.stateId);
localStorage.stateId = state.id;
// wait for changes
state.addEventListener('change', console.log);
// update/remove an entry
await state.set('key', value);
await state.remove('key');
// get current state
console.log(state.current);
POST /states
Create a state
Response:
{
"id": "<state uid>",
"version": 1,
"state": {}
}
GET /states?id=<state_uid>
Get the current state
{
"id": "<state uid>",
"version": 123,
"state": {...}
}
POST /events
Modify state with an event:
Request to add values:
{
"id": "<state uid>",
"type": "add",
"key": "foo",
"value": 123
}
Request to remove values:
{
"id": "<state uid>",
"type": "remove",
"key": "foo"
}
GET /events
Event source channel for state updates
Variable | Description |
---|---|
DATA_PATH | Path on container side to a folder where state files are stored |