示例工程 31-notifications-taskbar
31-notifications-taskbar/app/shared/ipc.ts
export const CH = {
get: "notify:get",
preview: "notify:preview",
send: "notify:send",
simulateClick: "notify:simulate-click",
setBadge: "notify:set-badge",
setProgress: "notify:set-progress",
bounce: "notify:bounce",
clear: "notify:clear",
changed: "notify:changed",
} as const;
export type Urgency = "low" | "normal" | "critical";
export type ProgressMode =
"none" | "normal" | "indeterminate" | "error" | "paused";
export type NotifyPayload = {
title: string;
body: string;
silent: boolean;
urgency: Urgency;
};
export type Redacted = {
title: string;
body: string;
hits: string[];
};
export type NotifyLog = {
at: number;
kind: "show" | "click" | "close" | "failed" | "badge" | "progress" | "bounce";
text: string;
};
export type Capabilities = {
notifications: boolean;
actions: boolean;
badge: boolean;
progress: boolean;
overlay: boolean;
bounce: boolean;
};
export type NotifySnapshot = {
platform: string;
capabilities: Capabilities;
supported: boolean;
lastPayload: Redacted | null;
badge: string;
progress: number;
mode: ProgressMode;
lastClicked: string;
log: NotifyLog[];
};
const URGENCIES: Urgency[] = ["low", "normal", "critical"];
const MODES: ProgressMode[] = [
"none",
"normal",
"indeterminate",
"error",
"paused",
];
export function assertPreviewReq(
value: unknown,
): asserts value is { title: string; body: string } {
if (typeof value !== "object" || value === null) {
throw new Error("BAD_PREVIEW_REQ");
}
const { title, body } = value as { title?: unknown; body?: unknown };
if (
typeof title !== "string" ||
title.length === 0 ||
title.length > 120 ||
typeof body !== "string" ||
body.length === 0 ||
body.length > 500
) {
throw new Error("BAD_PREVIEW_REQ");
}
}
export function assertSendReq(value: unknown): asserts value is NotifyPayload {
assertPreviewReq(value);
const { silent, urgency } = value as {
silent?: unknown;
urgency?: unknown;
};
if (typeof silent !== "boolean" || !URGENCIES.includes(urgency as Urgency)) {
throw new Error("BAD_SEND_REQ");
}
}
export function assertBadgeReq(
value: unknown,
): asserts value is { text: string } {
if (typeof value !== "object" || value === null) {
throw new Error("BAD_BADGE_REQ");
}
const text = (value as { text?: unknown }).text;
if (typeof text !== "string" || text.length > 4) {
throw new Error("BAD_BADGE_REQ");
}
}
export function assertProgressReq(
value: unknown,
): asserts value is { value: number; mode: ProgressMode } {
if (typeof value !== "object" || value === null) {
throw new Error("BAD_PROGRESS_REQ");
}
const { value: progress, mode } = value as {
value?: unknown;
mode?: unknown;
};
if (
typeof progress !== "number" ||
!Number.isFinite(progress) ||
progress < -1 ||
progress > 1 ||
!MODES.includes(mode as ProgressMode)
) {
throw new Error("BAD_PROGRESS_REQ");
}
}