/**
* 命令式 `toast()` API —— 与 `` 配对使用。
*
* 文件名故意小写 `toast.ts`,host 组件叫 `ToastHost.tsx`,避免 APFS 大小写冲突。
*
* 本文件只做公共入参归一化(kind / duration / position),投递与身份仲裁全部在
* `store.ts`。内部投递接口只给两个 Host 用,**不进公共 barrel**。
*/
import { createLogger } from '../../../utils/logger';
import { createToastStore } from './store';
import type { ToastEntry, ToastInput, ToastKind } from './types';
const log = createLogger('toast');
const store = createToastStore(log);
let _id = 0;
const DEFAULT_DURATION = 3000;
// [L-55] duration 合法域:[1, 2^31)。INT32_MAX = 2^31-1 = 2147483647,
// setTimeout 内部用 int32 存储,超出会溢出变成 0 立即触发。
const MAX_DURATION = 2147483647;
/** [L-55] 钳制 duration 到 [1, 2^31),域外回落 DEFAULT_DURATION 并 dev warn。 */
function clampDuration(raw: number | undefined): number {
if (raw == null) return DEFAULT_DURATION;
if (!Number.isFinite(raw) || raw < 1 || raw >= MAX_DURATION) {
log.warn(
`toast duration(${raw}) 超出合法域 [1, 2^31),已回落 ${DEFAULT_DURATION}ms。`
);
return DEFAULT_DURATION;
}
return Math.trunc(raw);
}
function emit(input: ToastInput, kind: ToastKind) {
const entry: ToastEntry =
typeof input === 'string'
? {
id: ++_id,
message: input,
kind,
duration: DEFAULT_DURATION,
position: 'bottom',
}
: {
id: ++_id,
message: input.message,
kind: input.kind ?? kind,
duration: clampDuration(input.duration),
position: input.position ?? 'bottom',
};
// 未挂 是**受支持路径**:消息留在 pending,Host 挂上后立即补投。
// 故这里不告警 —— 启动早期的 toast 本来就可能先于 Host。
store.publish(entry);
}
/**
* 命令式 toast API。在 app 根附近挂一个 `` 即可;需要在 `Modal` 里显示
* 时可在 Modal 内再挂一个,它会接管、关闭后自动归还。
*
* toast('已切换到日报');
* toast.success('订单提交成功');
* toast.error('网络异常,请重试');
* toast({ message: '正在同步…', duration: 5000 });
*/
export const toast = Object.assign((input: ToastInput) => emit(input, 'info'), {
info: (input: ToastInput) => emit(input, 'info'),
success: (input: ToastInput) => emit(input, 'success'),
error: (input: ToastInput) => emit(input, 'error'),
});
/** 内部:ToastHost 挂载时接管 owner;前任入栈挂起,本 lease 释放时归还。 */
export const registerToastHost = store.registerHost;
/** 内部:Host 提交画面后取得本条提示的显示时长和首次播报资格。 */
export const startToastPresentation = store.startPresentation;
/** 内部:三重身份 CAS —— owner token + leaseId + entry id 全对才算完成。 */
export const completeToast = store.complete;
/** 内部:timer / RAF / 动画回调动 UI 前的守卫。 */
export const isCurrentToastDelivery = store.isCurrent;