sync<T> static method

void sync<T>({
  1. required String callId,
  2. required dynamic operation(),
  3. OnError? onError,
  4. OnWaiting? onWaiting,
  5. OnNull? onNull,
  6. OnEmpty? onEmpty,
  7. required void onSuccess(
    1. T data
    ),
  8. Duration duration = const Duration(milliseconds: 1000),
})

Runs a synchronous operation with a debounce mechanism to limit multiple calls to the same operation. This method waits for a specified duration after a call with a specific callId has been made before running the operation.

When the operation is complete, the onSuccess callback is called with the result. You can also provide optional callbacks to handle errors, waiting states, null and empty data.

Example usage:

sync<String>(
  callId: 'my_call_id',
  operation: fetchSomeData,
  onSuccess: (data) => print('Got data: $data'),
);

Implementation

static void sync<T>({
  required String callId,
  required Function() operation,
  OnError onError,
  OnWaiting onWaiting,
  OnNull onNull,
  OnEmpty onEmpty,
  required void Function(T data) onSuccess,
  Duration duration = const Duration(milliseconds: 1000),
}) {
  if (_debounceTimers.containsKey(callId)) {
    _debounceTimers[callId]?.cancel();
  }

  _debounceTimers[callId] = Timer(duration, () {
    TryCatch.sync<T>(
      operation: () => operation(),
      onError: onError,
      onNull: onNull,
      onEmpty: onEmpty,
      onSuccess: onSuccess,
    );
    _debounceTimers.remove(callId);
  });
}