runWithThreshold<T, R> static method

Future<R> runWithThreshold<T, R>({
  1. required R function(
    1. T
    ),
  2. required T message,
  3. required int dataSize,
  4. int asyncThreshold = defaultAsyncThreshold,
})

Runs a function in an isolate if the data size exceeds the threshold.

The function parameter is the function to run in the isolate. The message parameter is the data to pass to the function. The dataSize parameter is the size of the data in bytes. The asyncThreshold parameter determines the data size (in bytes) above which the operation will be performed in a separate isolate.

Returns the result of the function.

Implementation

static Future<R> runWithThreshold<T, R>({
  required R Function(T) function,
  required T message,
  required int dataSize,
  int asyncThreshold = defaultAsyncThreshold,
}) async {
  // If the data size is below the threshold, run synchronously
  if (dataSize < asyncThreshold) {
    return function(message);
  }

  // Otherwise, run in a separate isolate
  _log.fine('Running operation in isolate for data size: $dataSize bytes');
  return Isolate.run(() => function(message));
}