Skip to main content

Interface: PersistenceOptions

Defined in: packages/media-session/src/persistence.ts:187

Properties

autosave?

optional autosave?: false | PersistenceAutosaveOptions;

Defined in: packages/media-session/src/persistence.ts:242

Checkpoint the position while a track plays through uninterrupted. On by default, every DEFAULT_AUTOSAVE_INTERVAL_MS ms; false turns it off entirely and restores the broadcast-only behaviour.

Read by withPersistence only — restorePersisted and clearPersisted share this options type but do not write on a timer.

What it does, and what it deliberately does not

Broadcasts are discontinuity-only by design (ARCHITECTURE §7), so a 40-minute track played straight through used to produce no write at all and restore at whatever the last play/seek/track-change said. A default that saves nothing is not a defensible default, so the timer is on.

What it costs is exactly one storage setItem per interval, and it costs nothing else: the tick re-projects the anchor the app already broadcast (value + (now − at) × rate) in JavaScript. It asks no player for a position, and it does not cross the bridge — the native resumption mirror is refreshed by broadcasts and PersistedMediaService.save only, so that a checkpoint can never become per-tick bridge traffic.

When it runs

Only while the last broadcast said playing with a rate above zero — there is nothing to re-project otherwise, and a paused session's record is already correct. It stops on the next non-playing broadcast, on MediaServiceApi.stopService and on PersistedMediaService.clear, and it re-arms from the last write rather than on a fixed cadence, so an app that broadcasts often costs nothing extra.

The two things it does not fix

  • The restored position is still always paused — freezing the anchor at write time is what makes a restore honest, whatever wrote it (see withPersistence).
  • Android freezes JS timers once the Activity is gone. That is a React Native platform behaviour, not something this package can reach: with no Activity, setTimeout stops firing and so does this. Autosave therefore covers the foreground; keep service.save() on AppState leaving active — that fires at exactly the moment autosave stops — and in onTaskRemoved, and the two together leave no uncovered window that anything could have covered.

key?

optional key?: string;

Defined in: packages/media-session/src/persistence.ts:189

Default

DEFAULT_PERSISTENCE_KEY


now?

optional now?: () => number;

Defined in: packages/media-session/src/persistence.ts:199

Injected clock. Exists so tests are deterministic.

Returns

number

Default

Date.now


onError?

optional onError?: (error: unknown) => void;

Defined in: packages/media-session/src/persistence.ts:197

Called when a write fails (the storage engine threw or rejected).

There is nowhere else for it to go: broadcast setters are synchronous and return void, so a rejected write has no caller left to reject to. Defaults to console.error; it is never swallowed.

Parameters

ParameterType
errorunknown

Returns

void