All files / components toast.ts

98.33% Statements 177/180
97.63% Branches 124/127
97.72% Functions 43/44
100% Lines 130/130

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393                                                                                                                                                                                                                                                                                        9x 9x 9x 9x               9x 53x 9x 53x 53x   9x     9x 9x 44x 44x                 9x       35x 35x 35x 33x 33x 33x 33x   9x                 9x 67x 67x 67x   9x       6x 7x 5x 4x 4x 8x 4x   9x 42x 42x 13x 13x 13x   9x 5x 5x 5x 5x 5x 5x   9x 44x 44x 55x 44x 17x 17x 16x   44x 55x 49x 49x     9x 19x 16x 19x 1x 1x   15x 15x                   19x 19x 14x 14x 14x 14x   9x         8x 10x 7x 5x         8x 8x 11x 8x   9x 10x 9x 9x 5x 5x     10x 10x 10x       5x 5x     10x 10x   9x 9x 8x 8x 5x 9x 9x 9x       5x 5x     9x 9x   9x 9x 9x 9x             3x         3x 2x 1x 1x 2x   2x 2x           1x 1x     8x             4x 4x 4x 2x                   4x   2x                   2x            
import { createRuntimeId } from '../accessibility/ids';
import { createControllerHost } from '../core/host';
import type {
  ChangeDetails,
  RuntimeController,
  RuntimeEventSource,
  Unsubscribe,
} from '../core/types';
import { createControllableValue } from '../state/controllable';
import { listen } from '../dom/dom';
 
/** Toast visual/announcement lifecycle state. @public */
export type ToastStatus = 'loading' | 'success' | 'error' | 'info';
 
/** Toast announcement politeness. @public */
export type ToastPoliteness = 'polite' | 'assertive';
 
/** Causes of toast queue mutations. @public */
export type ToastChangeReason = 'programmatic' | 'timeout' | 'dismiss' | 'promise' | 'update';
 
/** Public Toast record rendered by consumers. @public */
export interface ToastRecord {
  /** Stable deduplication ID. */
  readonly id: string;
  /** Human-readable announcement and visible message. */
  readonly message: string;
  /** Semantic lifecycle status. */
  readonly status: ToastStatus;
  /** Higher priorities sort before lower priorities. */
  readonly priority: number;
  /** Live-region politeness. */
  readonly politeness: ToastPoliteness;
  /** Auto-dismiss duration; `null` disables timeout. */
  readonly duration: number | null;
  /** Monotonic insertion sequence used as deterministic tie-breaker. */
  readonly sequence: number;
  /** Whether hover/focus has paused the timeout. */
  readonly paused: boolean;
}
 
/** Input used to show or update a Toast. @public */
export interface ToastInput {
  /** Optional deduplication ID; generated when omitted. */
  readonly id?: string;
  /** Human-readable message. */
  readonly message: string;
  /** Semantic status. @defaultValue `info` */
  readonly status?: ToastStatus;
  /** Sort priority. @defaultValue `0` */
  readonly priority?: number;
  /** Live-region politeness. @defaultValue `polite` */
  readonly politeness?: ToastPoliteness;
  /** Auto-dismiss duration in milliseconds. @defaultValue `5000` */
  readonly duration?: number | null;
}
 
/** Toast queue snapshot. @public */
export interface ToastSnapshot {
  /** Visible priority-ordered records. */
  readonly visible: readonly ToastRecord[];
  /** Waiting priority-ordered records. */
  readonly queued: readonly ToastRecord[];
  /** Complete deterministic queue. */
  readonly all: readonly ToastRecord[];
  /** Whether queue state is consumer-owned. */
  readonly controlled: boolean;
}
 
/** Toast mutation event payload. @public */
export interface ToastEvent {
  /** Affected record. */
  readonly toast: Readonly<ToastRecord>;
  /** Typed mutation cause. */
  readonly details: ChangeDetails<ToastChangeReason>;
}
 
/** Toast queue event map. @public */
export interface ToastEvents {
  /** Cancellable event emitted before insertion. */
  readonly beforeShow: ToastEvent;
  /** Event emitted after insertion. */
  readonly show: ToastEvent;
  /** Event emitted after a record update. */
  readonly update: ToastEvent;
  /** Event emitted after removal. */
  readonly dismiss: ToastEvent;
  /** Event emitted for every accepted queue mutation. */
  readonly stateChange: ToastEvent;
}
 
/** Message mapping for promise-backed toasts. @public */
export interface ToastPromiseMessages<TValue> {
  /** Message while the promise is pending. */
  readonly loading: string;
  /** Success message or value-aware formatter. */
  readonly success: string | ((value: TValue) => string);
  /** Error message or unknown-error formatter. */
  readonly error: string | ((error: unknown) => string);
}
 
/** Toast queue ownership and capacity options. @public */
export interface ToastOptions {
  /** Maximum concurrently visible records. @defaultValue `3` */
  readonly maxVisible?: number;
  /** Consumer-owned record reader. */
  readonly getToasts?: () => readonly ToastRecord[];
  /** Receives queue mutation requests in controlled mode. */
  readonly onToastsChange?: (
    toasts: readonly ToastRecord[],
    details: ChangeDetails<ToastChangeReason>,
  ) => void;
  /** Subscribes to consumer-owned queue changes. */
  readonly subscribeToasts?: (listener: () => void) => Unsubscribe;
}
 
/** Headless Toast queue controller. @public */
export interface ToastController
  extends RuntimeController<ToastSnapshot>, RuntimeEventSource<ToastEvents> {
  /** Inserts or updates by ID and returns its stable ID; destroyed controllers return no new ID. */
  show(input: ToastInput): string;
  /** Partially updates an existing record. */
  update(id: string, input: Partial<Omit<ToastInput, 'id'>>): void;
  /** Removes a record immediately. */
  dismiss(id: string, details?: ChangeDetails<ToastChangeReason>): void;
  /** Pauses an auto-dismiss timeout while preserving remaining duration. */
  pause(id: string): void;
  /** Resumes an auto-dismiss timeout from its preserved remaining duration. */
  resume(id: string): void;
  /** Pauses on hover/focus and resumes when interaction leaves the rendered toast. */
  bindPause(id: string, element: HTMLElement): () => void;
  /** Tracks a promise while preserving its original fulfillment or rejection. */
  promise<TValue>(
    promise: Promise<TValue>,
    messages: ToastPromiseMessages<TValue>,
    input?: Omit<ToastInput, 'message' | 'status'>,
  ): Promise<TValue>;
}
 
/** Creates a priority queue with deterministic ordering, pausable timers, and promise lifecycle. @public */
export function createToast(options: ToastOptions = {}): ToastController {
  const maxVisible = Math.max(1, options.maxVisible ?? 3);
  const initialRecords = options.getToasts?.() ?? [];
  let sequence = initialRecords.reduce((highest, record) => Math.max(highest, record.sequence), 0);
  const timers = new Map<
    string,
    {
      readonly timer?: ReturnType<typeof setTimeout>;
      readonly started: number;
      readonly remaining: number;
    }
  >();
  const sort = (records: readonly ToastRecord[]): readonly ToastRecord[] =>
    Object.freeze([...records].sort((a, b) => b.priority - a.priority || a.sequence - b.sequence));
  const build = (records: readonly ToastRecord[], controlled: boolean): ToastSnapshot => {
    const all = sort(records);
    return { visible: all.slice(0, maxVisible), queued: all.slice(maxVisible), all, controlled };
  };
  const host = createControllerHost<ToastSnapshot, ToastEvents>(
    build(initialRecords, options.getToasts !== undefined),
  );
  let reconcileTimers = (): void => undefined;
  const sync = (): void => {
    host.update(build(state.get(), state.controlled));
    reconcileTimers();
  };
  let pendingMutation:
    | {
        readonly type: 'show' | 'update' | 'dismiss';
        readonly payload: ToastEvent;
        readonly onCommit?: () => void;
      }
    | undefined;
  const commit = (
    _records: readonly ToastRecord[],
    changeDetails?: ChangeDetails<ToastChangeReason>,
  ): void => {
    if (changeDetails && pendingMutation) pendingMutation.onCommit?.();
    sync();
    if (!changeDetails || !pendingMutation) return;
    const { type, payload } = pendingMutation;
    pendingMutation = undefined;
    host.emit(type, { ...payload, details: changeDetails });
    host.emit('stateChange', { ...payload, details: changeDetails });
  };
  const state = createControllableValue<readonly ToastRecord[], ToastChangeReason>(
    {
      defaultValue: [],
      ...(options.getToasts ? { getValue: options.getToasts } : {}),
      ...(options.onToastsChange ? { onValueChange: options.onToastsChange } : {}),
      ...(options.subscribeToasts ? { subscribeValue: options.subscribeToasts } : {}),
    },
    commit,
  );
  const clearTimer = (id: string): void => {
    const active = timers.get(id);
    if (active?.timer) clearTimeout(active.timer);
    timers.delete(id);
  };
  const dismiss = (
    id: string,
    changeDetails: ChangeDetails<ToastChangeReason> = { reason: 'dismiss' },
  ): void => {
    if (!host.alive()) return;
    const record = state.get().find((toast) => toast.id === id);
    if (!record) return;
    const payload = { toast: record, details: changeDetails };
    pendingMutation = { type: 'dismiss', payload, onCommit: () => clearTimer(id) };
    const next = state.get().filter((toast) => toast.id !== id);
    Eif (state.set(next, changeDetails)) commit(next, changeDetails);
  };
  const startTimer = (record: ToastRecord, remaining = record.duration): void => {
    clearTimer(record.id);
    if (remaining === null || remaining <= 0 || record.paused) return;
    const started = Date.now();
    const timer = setTimeout(() => dismiss(record.id, { reason: 'timeout' }), remaining);
    timers.set(record.id, { timer, started, remaining });
  };
  const stopTimer = (id: string): number | null | undefined => {
    const active = timers.get(id);
    Iif (!active) return undefined;
    if (active.timer) clearTimeout(active.timer);
    const remaining = Math.max(0, active.remaining - (Date.now() - active.started));
    timers.set(id, { started: Date.now(), remaining });
    return remaining;
  };
  reconcileTimers = (): void => {
    Iif (!host.alive()) return;
    const records = state.get();
    const byId = new Map(records.map((record) => [record.id, record]));
    for (const id of [...timers.keys()]) {
      const record = byId.get(id);
      if (!record) clearTimer(id);
      else if (record.paused) stopTimer(id);
    }
    for (const record of records) {
      if (record.paused) continue;
      const active = timers.get(record.id);
      if (!active?.timer) startTimer(record, active?.remaining ?? record.duration);
    }
  };
  const show = (input: ToastInput): string => {
    if (!host.alive()) return input.id ?? '';
    const existing = input.id ? state.get().find((toast) => toast.id === input.id) : undefined;
    if (existing) {
      updateRecord(existing.id, input, { reason: 'update' });
      return existing.id;
    }
    sequence += 1;
    const record: ToastRecord = Object.freeze({
      id: input.id ?? createRuntimeId('toast'),
      message: input.message,
      status: input.status ?? 'info',
      priority: input.priority ?? 0,
      politeness: input.politeness ?? 'polite',
      duration: input.duration === undefined ? 5000 : input.duration,
      sequence,
      paused: false,
    });
    const payload = { toast: record, details: { reason: 'programmatic' as const } };
    if (!host.emit('beforeShow', payload)) return record.id;
    pendingMutation = { type: 'show', payload };
    const next = [...state.get(), record];
    if (state.set(next, payload.details)) commit(next, payload.details);
    return record.id;
  };
  const updateRecord = (
    id: string,
    input: Partial<Omit<ToastInput, 'id'>>,
    changeDetails: ChangeDetails<ToastChangeReason>,
  ): void => {
    if (!host.alive()) return;
    const previous = state.get().find((toast) => toast.id === id);
    if (!previous) return;
    const record: ToastRecord = Object.freeze({
      ...previous,
      ...input,
      duration: input.duration === undefined ? previous.duration : input.duration,
    });
    const payload = { toast: record, details: changeDetails };
    pendingMutation = { type: 'update', payload, onCommit: () => clearTimer(id) };
    const next = state.get().map((toast) => (toast.id === id ? record : toast));
    if (state.set(next, changeDetails)) commit(next, changeDetails);
  };
  const pause = (id: string): void => {
    if (!host.alive()) return;
    const record = state.get().find((toast) => toast.id === id);
    if (!record || record.paused) return;
    const active = timers.get(id);
    const remaining = active
      ? Math.max(0, active.remaining - (Date.now() - active.started))
      : record.duration;
    const paused = Object.freeze({ ...record, paused: true });
    const payload = { toast: paused, details: { reason: 'update' as const } };
    pendingMutation = {
      type: 'update',
      payload,
      onCommit: () => {
        clearTimer(id);
        if (remaining !== null) timers.set(id, { started: Date.now(), remaining });
      },
    };
    const next = state.get().map((toast) => (toast.id === id ? paused : toast));
    if (state.set(next, payload.details)) commit(next, payload.details);
  };
  const resume = (id: string): void => {
    if (!host.alive()) return;
    const record = state.get().find((toast) => toast.id === id);
    if (!record?.paused) return;
    const remaining = timers.get(id)?.remaining ?? record.duration;
    const resumed = Object.freeze({ ...record, paused: false });
    const payload = { toast: resumed, details: { reason: 'update' as const } };
    pendingMutation = {
      type: 'update',
      payload,
      onCommit: () => {
        clearTimer(id);
        if (remaining !== null) timers.set(id, { started: Date.now(), remaining });
      },
    };
    const next = state.get().map((toast) => (toast.id === id ? resumed : toast));
    if (state.set(next, payload.details)) commit(next, payload.details);
  };
  host.resources.add(() => state.destroy());
  host.resources.add(() => [...timers.keys()].forEach(clearTimer));
  sync();
  return {
    getSnapshot: host.getSnapshot,
    subscribe: host.subscribe,
    on: host.on,
    off: host.off,
    once: host.once,
    show,
    update: (id, input) => updateRecord(id, input, { reason: 'update' }),
    dismiss,
    pause,
    resume,
    bindPause(id, element) {
      if (!host.alive()) return () => undefined;
      const cleanups = [
        listen<PointerEvent>(element, 'pointerenter', () => pause(id)),
        listen<PointerEvent>(element, 'pointerleave', () => resume(id)),
        listen<FocusEvent>(element, 'focusin', () => pause(id)),
        listen<FocusEvent>(element, 'focusout', (event) => {
          const related = event.relatedTarget;
          if (
            related &&
            typeof related === 'object' &&
            'nodeType' in related &&
            element.contains(related as Node)
          )
            return;
          resume(id);
        }),
      ];
      return host.resources.add(() => cleanups.splice(0).forEach((cleanup) => cleanup()));
    },
    async promise<TValue>(
      promise: Promise<TValue>,
      messages: ToastPromiseMessages<TValue>,
      input: Omit<ToastInput, 'message' | 'status'> = {},
    ) {
      const id = show({ ...input, message: messages.loading, status: 'loading', duration: null });
      try {
        const value = await promise;
        updateRecord(
          id,
          {
            message:
              typeof messages.success === 'function' ? messages.success(value) : messages.success,
            status: 'success',
            duration: input.duration === undefined ? 5000 : input.duration,
          },
          { reason: 'promise' },
        );
        return value;
      } catch (error) {
        updateRecord(
          id,
          {
            message: typeof messages.error === 'function' ? messages.error(error) : messages.error,
            status: 'error',
            politeness: 'assertive',
            duration: input.duration === undefined ? 5000 : input.duration,
          },
          { reason: 'promise' },
        );
        throw error;
      }
    },
    destroy: host.destroy,
  };
}