watch<T> static method
ZenWorker
watch<T>(
- ValueNotifier<
T> observable, - void callback(
- T
- WorkerType type = WorkerType.ever,
- Duration? duration,
- bool condition(
- T
Universal worker creation method with explicit generic type preservation
Implementation
static ZenWorker watch<T>(
ValueNotifier<T> observable,
void Function(T) callback, {
WorkerType type = WorkerType.ever,
Duration? duration,
bool Function(T)? condition,
}) {
// Validate configuration
if ((type == WorkerType.debounce ||
type == WorkerType.throttle ||
type == WorkerType.interval) &&
duration == null) {
throw ArgumentError(
'Duration required for ${type.name}'); // coverage:ignore-line
}
// Validate duration is not negative
if (duration != null && duration.isNegative) {
throw ArgumentError(
'Duration cannot be negative'); // coverage:ignore-line
}
// Create worker first
final worker = _ZenWorker<T>(
type: type,
callback: callback,
duration: duration,
condition: condition,
);
// Create a ZenWorker (public API) that delegates to the internal worker
final zenWorker = ZenWorker(
() => worker.dispose(),
pauseFunction: () => worker.pause(),
resumeFunction: () => worker.resume(),
isPausedGetter: () => worker.isPaused,
isDisposedGetter: () => worker.isDisposed,
);
// Set worker reference for auto-disposal
worker.setWorker(zenWorker);
worker.listenTo(observable);
return zenWorker;
}