Add scheduler, fix imports and exports

[ci skip]
This commit is contained in:
Szymon Uglis 2020-07-16 21:38:05 +02:00
parent 17c8221bc2
commit 7daa30a5cc
3 changed files with 28 additions and 2 deletions

View file

@ -0,0 +1 @@
export "src/scheduler/scheduler.dart";

View file

@ -0,0 +1,27 @@
import "dart:async";
import "package:nyxx/nyxx.dart" show Nyxx;
/// Callback for [ScheduledEvent]. Is executed every period given in [ScheduledEvent]
typedef ScheduledEventCallback = Future<void> Function(Nyxx, Timer);
/// Creates and starts new periodic event. [callback] will be executed every given duration of time.
/// Event can be stopped via [stop] function.
class ScheduledEvent {
/// [Nyxx] instance
final Nyxx client;
/// Callback which will be run every given period of time
final ScheduledEventCallback callback;
late final Timer _timer;
/// Creates and starts new periodic event. [callback] will be executed every [duration] of time.
/// Event can be stopped via [stop] function.
ScheduledEvent(this.client, Duration duration, this.callback) {
_timer = Timer.periodic(duration, (timer) => callback(client, timer));
}
/// Stops [ScheduledEvent] if running.
void stop() => _timer.cancel();
}

View file

@ -1,3 +1 @@
library nyxx.extensions.utils;
export "src/utils.dart";