From 03b1b0129032b7e9000bd82501355e766920fbe6 Mon Sep 17 00:00:00 2001 From: jinyus Date: Wed, 31 Jan 2024 13:43:57 -0500 Subject: [PATCH] delegate writes for buffered beacons --- .../lib/src/beacons/buffered.dart | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/packages/state_beacon_core/lib/src/beacons/buffered.dart b/packages/state_beacon_core/lib/src/beacons/buffered.dart index 0adae7d5..8df09049 100644 --- a/packages/state_beacon_core/lib/src/beacons/buffered.dart +++ b/packages/state_beacon_core/lib/src/beacons/buffered.dart @@ -25,12 +25,19 @@ abstract class BufferedBaseBeacon extends ReadableBeacon> } /// Adds a new value to the buffer. - void add(T newValue); + void add(T newValue) { + _internalAdd(newValue, delegated: true); + } + + void _internalAdd(T newValue, {bool delegated = false}); /// Clears the buffer - void reset() { + void reset({bool force = false}) { + if (_delegate != null) { + _delegate!.reset(force: force); + } _clearBuffer(); - _setValue(_initialValue); + _setValue(_initialValue, force: force); } @override @@ -45,7 +52,7 @@ abstract class BufferedBaseBeacon extends ReadableBeacon> @override void _onNewValueFromWrapped(T value) { - add(value); + _internalAdd(value); } } @@ -59,7 +66,11 @@ class BufferedCountBeacon extends BufferedBaseBeacon { final int countThreshold; @override - void add(T newValue) { + void _internalAdd(T newValue, {bool delegated = false}) { + if (delegated && _delegate != null) { + _delegate!.set(newValue); + return; + } super._addToBuffer(newValue); if (_buffer.length == countThreshold) { @@ -81,7 +92,12 @@ class BufferedTimeBeacon extends BufferedBaseBeacon { Timer? _timer; @override - void add(T newValue) { + void _internalAdd(T newValue, {bool delegated = false}) { + if (delegated && _delegate != null) { + _delegate!.set(newValue); + return; + } + super._addToBuffer(newValue); _startTimerIfNeeded(); } @@ -97,8 +113,8 @@ class BufferedTimeBeacon extends BufferedBaseBeacon { } @override - void reset() { + void reset({bool force = false}) { _timer?.cancel(); - super.reset(); + super.reset(force: force); } }