Timed Operations
Timed Operations provides better handling of timed operations such as Debounce Throttle operations.
Additionally, it provides optional callbacks to handle different states and outcomes, such as errors, waiting states, null or empty data, and successful completion of the operation.
Features
- Debounce (async/sync)
- Throttle (async/sync)
- Extensive handling (optional)
Usage
Throttle.sync()
Throttles a synchronous operation by preventing it from being executed more
frequently than the specified throttle
duration, and executes a success
callback with the result of the operation.
Throttle.sync<int>(
callId: 'example',
operation: () => 1 + 1,
onThrottle: () => print('Operation is throttled'),
onNull: () => print('Operation returned null'),
onEmpty: () => print('Operation returned an empty iterable or map'),
onSuccess: (result) => print('Operation result: $result'),
throttle: const Duration(milliseconds: 500),
);
Throttle.async()
Throttles the execution of an asynchronous operation and handles its result or errors
based on the given callback functions. The callId
parameter is used to identify
the operation and avoid concurrent executions. The operation
parameter is the
asynchronous operation to be throttled, and duration
is the duration of the
throttling period.
await Throttle.async<int>(
callId: 'example',
operation: () async {
// perform some async operation
await Future.value(100);
},
onThrottle: () => print('Operation is throttled'),
onNull: () => print('Operation returned null'),
onEmpty: () => print('Operation returned an empty iterable or map'),
onSuccess: (result) => print('Operation result: $result'),
duration: const Duration(milliseconds: 500),
timeout: const Duration(seconds: 5),
);
Debounce.sync()
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.
Debounce.sync<String>(
callId: 'my_call_id',
operation: fetchSomeData,
onSuccess: (data) => print('Got data: $data'),
);
Debounce.async()
Runs an asynchronous 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.
Debounce.async<String>(
callId: 'my_call_id',
operation: fetchSomeData(),
onSuccess: (data) => print('Got data: $data'),
);
❤️ Support Flutter Focus
Need Mobile, Web or Video marketing services? 📱 🌐 📹
Flutter Focus offers bespoke services in multimedia storytelling by mixing Mobile, Web and Video.
Libraries
- ff_timed_operations
- This library provides classes based on timed operations such as Debounce Throttle operations.