Skip to content

Understanding the API Lifecycle

The API transitions through four states during its lifetime.

State Machine

UNINITIALIZED ──→ LOADING ──→ READY ──→ DESTROYED

                                           └──→ UNINITIALIZED (re-init allowed)

States

StateDescriptionAllowed Operations
UNINITIALIZEDAPI not startedinit(), isReady(), getState(), subscribe()
LOADINGIframe creating, React mountingisReady(), getState(), subscribe()
READYMap fully loaded, commands acceptedAll public methods
DESTROYEDMap torn down, listeners clearedinit() to restart

Lifecycle Events

javascript
const map = await mapsted.maps.init({ element: '#map' });
// State: READY

mapsted.maps.on('mapMounted', () => console.log('React app mounted'));
mapsted.maps.on('load', () => console.log('Map tiles loaded'));

// When done:
await map.destroy();
// State: UNINITIALIZED (can re-init)

State Observation

getState() is async — it resolves to a snapshot of the current API state, so await it. subscribe(fn) attaches a listener that fires on every state transition and returns an Unsubscribe function.

javascript
import { getState, subscribe } from '@mapsted/maps-js-api';

const snapshot = await getState();   // getState() returns a Promise
console.log(snapshot.sdkState);      // "READY" | "LOADING" | …

const unsubscribe = subscribe((state) => {
  console.log('API state →', state.sdkState);
});

// Later, stop listening:
unsubscribe();

For event-stream observation (floor changes, selections, etc.) use the typed event API on('floorChange', fn) / on('select', fn) rather than state subscription — state is the slow-changing lifecycle phase, events are the fast-changing interactions.

See also: API Reference | How it works