sample method
Sample value changes at regular intervals
Implementation
StreamSubscription<T> sample(
Duration interval, void Function(T value) callback) {
RxTimingUtils._register(this);
late StreamController<T> controller;
Timer? timer;
void listener() {
if (!controller.isClosed) {
controller.add(value);
}
}
controller = StreamController<T>(
onListen: () {
addListener(listener);
timer = Timer.periodic(interval, (_) {
if (!controller.isClosed) {
controller.add(value);
}
});
// Track the timer for cleanup
if (timer != null) {
RxTimingUtils._getTimingData(this)?.addTimer(timer!);
}
},
onCancel: () {
removeListener(listener);
timer?.cancel();
},
);
final subscription = controller.stream.listen(callback);
RxTimingUtils._getTimingData(this)?.addSubscription(subscription);
return subscription;
}