task_sequencer 1.0.2
task_sequencer: ^1.0.2 copied to clipboard
A lightweight Flutter/Dart utility package for managing sequential callbacks and debounced/repeated tasks.
example/main.dart
import 'package:task_sequencer/task_sequencer.dart';
// This code defines a simple `FakePersonsApi`, intended for use in a mock environment,
// as a lightweight replacement for real backend interactions during
// development or in local simulations.
//
// A core component is a task sequencer, which ensures that all function calls
// modifying a shared mocked list are executed in a strict sequential order,
// even if they're triggered concurrently and asynchronously.
//
// When multiple asynchronous functions are invoked at the same time, the sequencer
// enqueues them and executes each one only after the previous one finishes.
// This guarantees a deterministic and predictable execution order, mimicking
// the behavior of a real backend that processes requests sequentially.
//
// By maintaining execution order, the system ensures data consistency and
// is well-suited for environments where concurrency and asynchronicity are involved,
// but backend behavior needs to be simulated locally.
class FakePersonsApi implements PersonsApi {
final _manager = TaskSequencerList(PersonDtoFaker.fakeList());
// Getter for read-only purposes
List<PersonDto> get persons => _manager.items;
@override
Future<PersonDto?> updatePerson(RequestBody body, String personId) async {
try {
return _manager.enqueue((list) async {
final person = list.firstWhere((p) => p.id == personId);
list[list.indexOf(person)] = person.copyWith(name: body.name);
return person;
});
} catch (e) {
return null;
}
}
@override
Future<PersonDto?> updatePersonPhoneNumbers(
String personId,
List<int> phoneNumbers,
) async {
try {
return _manager.enqueue((list) async {
final person = list.firstWhere((p) => p.id == personId);
list[list.indexOf(person)] = person.copyWith(
phoneNumbers: phoneNumbers,
);
return person;
});
} catch (e) {
return null;
}
}
}
// This class simulates a REST API
abstract class PersonsApi {
Future<PersonDto?> updatePerson(RequestBody body, String personId);
Future<PersonDto?> updatePersonPhoneNumbers(
String personId,
List<int> phoneNumbers,
);
}
// This class simulates a REST API DTO
class PersonDto {
const PersonDto({
required this.id,
required this.name,
required this.phoneNumbers,
});
final String id;
final String name;
final List<int> phoneNumbers;
PersonDto copyWith({String? name, List<int>? phoneNumbers}) => PersonDto(
id: id,
name: name ?? this.name,
phoneNumbers: phoneNumbers ?? this.phoneNumbers,
);
}
// This class simulates a request body
class RequestBody {
const RequestBody({required this.name});
final String name;
}
// Helper class for generating fake data
abstract class PersonDtoFaker {
static List<PersonDto> fakeList() => List.generate(
50,
(index) =>
PersonDto(id: '$index', name: 'Person $index', phoneNumbers: [index]),
);
}