Files
sm-emby-share/lib/core/contracts/cancellation.dart
T
2026-07-14 11:11:36 +08:00

29 lines
711 B
Dart

import 'dart:async';
class CancelledException implements Exception {
final String? reason;
const CancelledException([this.reason]);
@override
String toString() =>
reason == null ? 'CancelledException' : 'CancelledException: $reason';
}
class CancellationToken {
final Completer<void> _completer = Completer<void>();
String? _reason;
bool get isCancelled => _completer.isCompleted;
String? get reason => _reason;
Future<void> get whenCancelled => _completer.future;
void cancel([String? reason]) {
if (_completer.isCompleted) return;
_reason = reason;
_completer.complete();
}
void throwIfCancelled() {
if (isCancelled) throw CancelledException(_reason);
}
}