start method
A common interface defining methods for start/stop lifecycle control.
The typical use case for this is to control asynchronous processing. This interface does not imply specific auto-startup semantics.
Usage Example
class BackgroundTaskManager implements Lifecycle {
bool _running = false;
@override
void start() {
if (!_running) {
startBackgroundTasks();
_running = true;
}
}
@override
void stop() {
if (_running) {
stopBackgroundTasks();
_running = false;
}
}
@override
bool isRunning() => _running;
}
Start this component.
Should not throw an exception if the component is already running.
Implementation
@override
FutureOr<void> start() {
return synchronized(_lock, () async {
if (!_isRunning) {
_isRunning = true;
await publishEvent(ContextStartedEvent.withClock(this, () => getStartTime()));
await doStart();
}
});
}