Last active
March 9, 2022 09:32
-
-
Save cybrox/6836233249052edf85ab78c296de6128 to your computer and use it in GitHub Desktop.
Dart Timed Event Display
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HypotheticalService { | |
final _stack = <Event>[]; | |
Event? _lastDisplayed; | |
void reportEvent(Event event) { | |
print("${DateTime.now()} Adding ${event.runtimeType} to stack"); | |
_stack.add(event); | |
_outputLatest(); | |
} | |
void _outputLatest() { | |
// Remove items that have reached their max display time | |
_stack.removeWhere((e) => e.maxPassed); | |
// If no pending events are left, output null | |
if (_stack.isEmpty) return _display(null); | |
// Display first from bottom of stack that hasn't been | |
// displayed for its minimum display time yet. | |
for (final item in _stack) { | |
if (!item.minPassed) { | |
_display(item); | |
return; | |
} | |
} | |
// If all have been displayed for their minimum time, just show newest | |
_display(_stack[_stack.length - 1]); | |
} | |
void _display(Event? event) { | |
if (_lastDisplayed == event) return; | |
_lastDisplayed = event; | |
if (event == null) { | |
print("${DateTime.now()} DISPLAY: nothing"); | |
return; | |
} | |
// We only start the timers once an item is displayed for the first time! | |
if (!event.timersRunning) event.startTimers(_outputLatest); | |
print("${DateTime.now()} DISPLAY: ${event.runtimeType}"); | |
} | |
} | |
abstract class Event { | |
get minTime; | |
get maxTime; | |
bool minPassed = false; | |
bool maxPassed = false; | |
bool timersRunning = false; | |
Function? _callback; | |
startTimers(Function callback) { | |
timersRunning = true; | |
_callback = callback; | |
Future.delayed(Duration(seconds: minTime)).then(_onMinTimePassed); | |
Future.delayed(Duration(seconds: maxTime)).then(_onMaxTimePassed); | |
} | |
void _onMinTimePassed(_) { | |
print("${DateTime.now()} $runtimeType min time $minTime passed"); | |
minPassed = true; | |
if (_callback != null) _callback!(); | |
} | |
void _onMaxTimePassed(_) { | |
print("${DateTime.now()} $runtimeType max time $maxTime passed"); | |
maxPassed = true; | |
if (_callback != null) _callback!(); | |
} | |
} | |
class EventA extends Event { | |
@override | |
final minTime = 1; | |
@override | |
final maxTime = 3; | |
} | |
class EventB extends Event { | |
@override | |
final minTime = 2; | |
@override | |
final maxTime = 5; | |
} | |
class EventC extends Event { | |
@override | |
final minTime = 3; | |
@override | |
final maxTime = 7; | |
} | |
class EventD extends EventA {} | |
void main() async { | |
// Hypothetical service | |
final service = HypotheticalService(); | |
// Hypothetical UI reporting events | |
service.reportEvent(EventA()); | |
service.reportEvent(EventB()); | |
service.reportEvent(EventC()); | |
await Future.delayed(const Duration(seconds: 1)); | |
service.reportEvent(EventD()); | |
await Future.delayed(const Duration(minutes: 1)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment