Create a plugin
shpnpm new plugin sleep-timer "Pause playback after a chosen number of minutes"
Anatomy
tsimport { definePlugin } from '@tomato-player/core';export interface SleepTimerAPI { start(minutes: number): void; cancel(): void }export const sleepTimer = (opts: { defaultMinutes?: number } = {}) =>definePlugin<SleepTimerAPI>({name: 'sleep-timer',setup(player) {let timer: ReturnType<typeof setTimeout> | undefined;const api: SleepTimerAPI = {start(min) { clearTimeout(timer); timer = setTimeout(() => player.pause(), min * 60_000); },cancel() { clearTimeout(timer); },};const off = player.on('sourcechange', api.cancel);return { api, destroy: () => { off(); api.cancel(); } };},});
Rules
- Clean up everything in
destroy(): listeners, timers, DOM, observers. Players are created and destroyed constantly in SPAs. - No work at import time. Touch
windowanddocumentonly insidesetup. - Be SSR-safe and feature-detect. Return early if an API is missing.
- Talk through the player, not to other plugins:
player.emit('plugin:my-event', data),player.provide(key, api),player.inject(key). - Stay small. The budget is 1.5 KB gzip. Lazy-load heavy SDKs with
import(). - Never break playback. Event handlers run inside try/catch, but async errors are yours to handle.
Testing
tsconst player = createPlayer(document.body.appendChild(document.createElement('div')), { plugins: [sleepTimer()] });player.plugin<SleepTimerAPI>('sleep-timer')!.start(1);
Run
pnpm test. Tests use Vitest and happy-dom against source files, so no build is needed.