periodicAsync static method
Run the asynchronous func
every ms
milliseconds.
- If
callImmediately
is true (the default) then in addition to scheduling the periodic trigger,func
is also called immediately, and periodicAsync returns whenfunc
finishes. - If
allowReentry
is false (the default) then whilefunc
is running - additional invocation will be skipped, until the first invocation that is triggered afterfunc
returned. - Returns a cancellation callback.
Implementation
static Future<TFuncCancel> periodicAsync(int ms, TFuncAsyncAction func, [ bool callImmediately = true, bool allowReentry = false ]) async {
if (callImmediately) {
await func();
}
int numEntries = 0;
TFuncCancel funcCancel = Tick.periodic(ms, () async {
if (numEntries > 0 && !allowReentry) {
return;
}
numEntries++;
try {
await func();
}
catch (ex) {
rethrow;
}
finally {
numEntries--;
}
});
return funcCancel;
}