Notify class

A lightweight synchronization primitive for control-plane signaling.

Notify is perfect for coordination without data payloads. Unlike channels, it doesn't carry values - just wake-up signals. Use it for configuration changes, shutdown notifications, flush commands, and "check your state" signals.

Key Concepts

  • Permits: Stored notifications that can be consumed immediately
  • Waiters: Tasks waiting for notifications
  • notifyOne: Wake one waiter or store one permit
  • notifyAll: Wake all current waiters (doesn't store permits)

Usage Patterns

import 'dart:async';

import 'package:cross_channel/notify.dart';

Future<void> main() async {
  final notify = Notify();

  // 1. Basic signaling
  unawaited(Future.microtask(() async {
    final (future, _) = notify.notified();
    await future;
    print('Worker 1: Received signal');
  }));

  notify.notifyOne();

  // 2. Broadcast signaling
  unawaited(Future.microtask(() async {
    final (future, _) = notify.notified();
    await future;
    print('Worker 2: Shutting down');
  }));

  unawaited(Future.microtask(() async {
    final (future, _) = notify.notified();
    await future;
    print('Worker 3: Shutting down');
  }));

  notify.notifyAll();
}

When to use Notify vs Channels

  • Use Notify for: Config changes, shutdown signals, flush commands, "wake up and check" notifications
  • Use Channels for: Data processing, task queues, request/reply, anything with payloads

Constructors

Notify()

Properties

epoch int
Debug counter that increments on each notification (for testing/debugging).
no setter
hashCode int
The hash code for this object.
no setterinherited
isDisconnected bool
true if this Notify has been closed and will reject new waiters.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

close() → void
Close this Notify and fail all current and future waiters.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notified() → (Future<void>, void Function())
Register to be notified, returning a future and cancellation function.
notifiedTimeout(Duration d) Future<void>
notifyAll() → void
Wake up all currently waiting tasks (does not store permits).
notifyN(int n) → void
notifyOne() → void
Wake up exactly one waiter, or store one permit if no waiters.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited