Source
static Disposable safeEvery(interval, action()) {
Duration duration;
if (interval is Duration) {
duration = interval;
} else if (interval is int) {
duration = new Duration(milliseconds: interval);
} else if (interval is Interval) {
duration = interval.duration;
} else {
throw new Exception("Invalid Interval: ${interval}");
}
ExecutableFunction schedule;
Timer timer;
bool disposed = false;
schedule = () async {
await action();
if (!disposed) {
new Timer(duration, schedule);
}
};
timer = new Timer(duration, schedule);
return new FunctionDisposable(() {
if (timer != null) {
timer.cancel();
}
disposed = true;
});
}