periodicAsync static method

Future<TFuncCancel> periodicAsync(
  1. int ms,
  2. TFuncAsyncAction func, [
  3. bool callImmediately = true,
  4. bool allowReentry = false,
])

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 when func finishes.
  • If allowReentry is false (the default) then while func is running - additional invocation will be skipped, until the first invocation that is triggered after func 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;
}