Overview
dispose() tears down a player instance completely.
It is idempotent, so a second call is a no-op.
After dispose() the instance is permanently dead, and re-use requires constructing a new player.
Signature
TypeScript
player.dispose(): void
What it does
- Checks that the player is not already
disposedordisposing; if so, returns immediately. - Transitions the phase to
disposing. - Drains the policy cleanup queue in registration order. The cleanup queue holds every subscription, event listener, interval, and RAF handle that
setup()’s_wire*helpers registered. Each is called inside atry/catch, so a misbehaving cleanup function surfaces awarningevent but does not block the rest of the queue. - Clears the cleanup queue.
- If
ready()never resolved, rejects theready()promise withcore:player/disposed. - Emits
dispose. - Transitions the phase to
disposed. - Removes all event listeners via
off('all').
Cleanup queue contract
Every cross-cutting concern registered during setup() pushes a cleanup function onto _policyCleanup:
- Network monitor subscription
- Visibility monitor subscription
- Wake-lock release (if held)
- Metrics interval
- Progress throttle handler
- Preload/transition listeners
Plugin lifecycles are separate.
Plugin teardown is handled by removePlugin() and LifecycleRegistry, which runs before dispose() drains the policy queue when setup() calls dispose() internally.
Example
TypeScript
const player = nmplayer('main').setup({ ... });
// Later: SPA route change, component unmount, etc.
player.dispose();
// Attempting to call transport methods after dispose throws:
// PlayerError: core:player/disposed