Skip to content

6.2.8.4. NotificationVM

6.2.8.4.1. When To Use It

Use NotificationVM when a host wants to render pending notifications from INotificationHub as bindable view state with auto-dismiss timing.

This is a render-side VM, not the notification hub itself. In C#, the shipped type lives in the VMx.Notifications package/namespace rather than the core VMx assembly.

6.2.8.4.2. Shape And Ownership

NotificationVM wraps one Notification value and owns:

  • lifespan and remaining time
  • derived opacity
  • resolved state
  • dismiss command that resolves through the hub

It is usually created by a higher-level notifications list VM that subscribes to INotificationHub.Pending.

6.2.8.4.3. Lifecycle And Messaging

The hub still owns the source notification state. NotificationVM adds the render-time lifecycle:

  • timer starts at construction
  • timer expiry resolves with Approve
  • manual dismiss resolves immediately and cancels the timer
  • external hub resolution updates IsResolved
  • dispose tears down timer and subscriptions
  • resolution or dispose stops the clock: RemainingTime and Opacity keep the values they had at that moment (NOTIF-019, ADR-0197)
  • a VM built over a disposed hub constructs normally; its pending subscription completes at once (NOTIF-020)

Timer, subscription, command, and property-stream teardown happens at most once under the Disposal Contract.

6.2.8.4.4. Cross-Language Surface

Concept C# Python TypeScript Swift Rust
Type NotificationVM NotificationVM NotificationVM NotificationVM NotificationVm
Lifespan default 60 s 60 s 60 s 60 s 60 s
Dismiss command DismissCommand dismiss_command dismissCommand dismissCommand dismiss_command()

Swift accepts any NotificationScheduler: DispatchQueue.main, RunLoop.main or OperationQueue.main in an app, and VirtualTimeScheduler in tests. The timers run on that scheduler, so pass one on the thread that uses the VM.

Rust's NotificationVm::with_scheduler takes any Scheduler. new and with_hub use SystemScheduler, so the lifespan runs in real time, and tests pass a VirtualTimeScheduler (ADR-0219). Its dispose() makes every clone of dismiss_command() inert. Dropping the VM does not, so call dispose() when a VM leaves the visible list. Dropping every handle to the VM and its commands cancels auto-dismiss, and the notification stays pending.

6.2.8.4.5. Example

The Notes Workspace notifications feature projects pending notifications into render VMs:

  • C#: NotificationsVM.cs (examples/csharp/avalonia/NotesShowcase/ViewModels/NotificationsVM.cs)
  • Python: notifications_vm.py (examples/python/textual/notes_showcase/src/notes_showcase/viewmodels/notifications_vm.py)
  • TypeScript: notificationsVM.ts (examples/typescript/react/notes-showcase/src/viewmodels/notificationsVM.ts)
  • Swift: NotificationsVM.swift (examples/swift/notes-showcase/Sources/NotesShowcaseCore/ViewModels/NotificationsVM.swift)

Those VMs subscribe to Pending, build a bounded visible list, and let each NotificationVM manage its own fade and dismissal timing.

var vm = new NotificationVM(n, _notificationHub, _scheduler, _lifespan);
_map[n] = vm;
_visible.Add(vm);
vm = NotificationVM(
    notification=n,
    hub=self._notification_hub,
    scheduler=self._scheduler,
    lifespan=self._lifespan,
)
self._map[n] = vm
self._visible.append(vm)
const vm = new NotificationVM(
  n,
  this.#notificationHub,
  this.#scheduler,
  this.#lifespanMs,
);
this.#map.set(n, vm);
this.#visible.push(vm);
let lifespan = _lifespan ?? 60.0
let vm = NotificationVM(
    notification: n,
    hub: _notificationHub,
    scheduler: _scheduler,
    lifespan: lifespan
)
_map[key] = vm
_visible.append(vm)

6.2.8.4.6. Common Pitfalls

  • Treating the render VM as the source of truth. The hub owns pending state.
  • Forgetting to inject a deterministic scheduler in tests.
  • Using NotificationVM for prompts that require explicit approve/reject actions. That is ConfirmationVM.