@baukit/notifications-core

中文文档
0.18.3发布于 2026/11/07MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 8160

Framework-free local notification planning: zoned occurrence resolution, horizon filtering, deterministic keep, cancel, and schedule sets, and owner-scoped replacement over a platform port.

原始使用文档
文档目录4 节
  1. Planning
  2. Owned replacement
  3. Conformance suite
  4. Boundaries

@baukit/notifications-core

@baukit/notifications-core plans local notifications and replaces only the ones a product owns. It has no runtime dependencies besides @baukit/localization-core and never imports a notification library. The product decides which occurrences are eligible and what the copy says. This package turns them into instants, drops the ones outside the horizon, and computes what to keep, cancel, and schedule.

Planning

import {
  planNotificationReplacement,
  resolveNotificationOccurrences,
} from '@baukit/notifications-core';

const resolution = resolveNotificationOccurrences({
  occurrences: [
    {
      logicalId: 'plan:2026-03-29',
      civilDate: '2026-03-29',
      civilTime: '02:30',
      contentDigest: 'v3',
    },
    {
      logicalId: 'plan:2026-03-30',
      civilDate: '2026-03-30',
      civilTime: '02:30',
      contentDigest: 'v3',
    },
  ],
  timeZone: 'Europe/Berlin',
  gap: 'shiftForward',
  fold: 'earlier',
  clock: () => Date.now(),
  horizonDays: 14,
});
// resolution.desired: both entries with epochMilliseconds and transition ('gap' for the first)

const plan = planNotificationReplacement(current, resolution.desired);
// plan.keep, plan.cancel, plan.schedule, each sorted by instant, then logical ID
  • Each occurrence resolves through resolveZonedLocalTime from @baukit/localization-core. The caller picks the gap and fold policies; neither has a default.
  • The horizon is the civil dates from today in timeZone through today plus horizonDays - 1. resolution.firstCivilDate and resolution.lastCivilDate report it.
  • resolution.skipped lists occurrences with a reason: past (the instant is at or before the clock), outside_horizon, or nonexistent_local_time (a gap under gap: 'reject').
  • Extra fields on an occurrence, such as the notification copy, pass through to desired.
  • An entry is kept only when its logical ID, instant, and content digest all match. A text change under the same digest is kept on purpose. Change the digest, or pass { replaceAll: true }, to replace the text.
  • Invalid input throws NotificationPlanError with a code and, when it applies, the logicalId: invalid_civil_date, invalid_civil_time, invalid_time_zone, invalid_horizon, invalid_clock, invalid_logical_id, invalid_content_digest, invalid_instant, or duplicate_logical_id. An invalid date throws even when it falls outside the horizon. An unknown policy throws RangeError, as in resolveZonedLocalTime.

The same input and clock give the same result, whatever order the occurrences arrive in. The shared vectors in fixtures/notifications/plan-vectors-v1.json cover DST gaps and folds, month and year changes, travel between zones, duplicates, invalid input, changed content, and horizon boundaries. @baukit/notifications-core/vectors exports notificationPlanVectorChecks(fixture), the checks the Vitest suite runs: each success case yields the expected plan, convergence once the plan is applied, and independence from input order. examples/expo-notifications-conformance runs them inside Hermes.

Owned replacement

createOwnedNotificationScheduler(platform, { pendingLimit }) runs a plan against a NotificationPlatform<TContent> port with four calls: list, cancel, permission, and schedule. @baukit/notifications-expo is the Expo implementation.

const outcome = await scheduler.replaceOwned({ namespace: 'reminders' }, desired);
// { status: 'complete' | 'incomplete' | 'superseded', kept, cancelled, scheduled, failures }
  • A notification belongs to a namespace when it carries a valid marker for that namespace and its identifier is baukit:<namespace>:<logicalId>. Everything else is left alone, including other Baukit namespaces and requests with a copied marker.
  • The scheduler cancels one request at a time and never cancels everything. An item whose cancel failed is not rescheduled in the same run.
  • It checks permission only when something needs scheduling. Without permission it still cancels stale entries.
  • It schedules in instant order and stops at pendingLimit, which counts every pending request on the device, not only this owner's.
  • Failures carry a code and a logical ID, never content: list_failed, cancel_failed, permission_denied, permission_failed, schedule_failed, schedule_limit. Any failure makes the outcome incomplete. Calling replaceOwned again with the same set converges.
  • One replacement runs per namespace at a time. While it runs, later calls for that namespace collapse to the newest. The ones that never ran resolve as superseded. Namespaces do not wait for each other. Share one scheduler per device so this works.
  • An invalid namespace (lowercase letters and digits separated by . or -, at most 64 characters) or an invalid desired set rejects with NotificationPlanError before the platform is touched.

Conformance suite

@baukit/notifications-core/vitest exports describeOwnedNotificationSchedulerContract. Run it against any platform implementation; it needs the consumer's Vitest installation.

import {
  InMemoryNotificationPlatform,
  createOwnedNotificationScheduler,
} from '@baukit/notifications-core';
import { describeOwnedNotificationSchedulerContract } from '@baukit/notifications-core/vitest';

describeOwnedNotificationSchedulerContract<string>((options) => {
  const platform = new InMemoryNotificationPlatform<string>();
  return {
    scheduler: createOwnedNotificationScheduler(platform, options),
    faults: platform.faults,
    content: (_logicalId, text) => text,
    addUnrelated: (identifier) => platform.addUnrelated(identifier),
    pending: () => platform.pending(),
  };
});

NotificationPlatformFaultState holds the injected faults (failed list, cancel, schedule and permission calls, a revoked permission, a held list), so a mocked native module can reuse it.

Boundaries

The package does not pick reminder times, quiet hours, or copy, and it does not request permission. Products compute eligible occurrences and the content digest, ask for permission in their own UX, and choose the pending limit for their platform. It plans one time zone per call.

版本与发布记录

@baukit/notifications-core 共发布 60 个版本,首次发布于 2026/09/29;过去 12 个月发布了 60 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.18.3latest2026/11/07今天
0.18.22026/11/061 天前
0.18.12026/11/062 天前
0.18.02026/11/052 天前
0.17.12026/11/034 天前
0.17.02026/11/025 天前

关于 @baukit/notifications-core 的常见问题

@baukit/notifications-core 是什么?

Framework-free local notification planning: zoned occurrence resolution, horizon filtering, deterministic keep, cancel, and schedule sets, and owner-scoped replacement over a platform port.

如何安装 @baukit/notifications-core?

在项目目录运行 npm install @baukit/notifications-core,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@baukit/localization-core(^0.18.3)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@baukit/notifications-core 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@baukit/notifications-core 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@baukit/notifications-core 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@baukit/notifications-core 可以商用吗?

@baukit/notifications-core 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@baukit/notifications-core 的下载量有多少?

根据 npm 官方下载统计,@baukit/notifications-core 在 2026/10/08 – 2026/11/06 这 30 天内被下载 8,160 次,日均约 272 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@baukit/notifications-core 还在维护吗?

最新版本 0.18.3 发布于 2026/11/07(今天);至今共发布 60 个版本,其中过去 12 个月发布了 60 个;首次发布于 2026/09/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。