Skip to content

Instantly share code, notes, and snippets.

View alphamikle's full-sized avatar

Mike Alfa alphamikle

View GitHub Profile
@alphamikle
alphamikle / search_with_compute.dart
Created January 31, 2021 13:58
Search with compute
Future<void> runSearchWithCompute() async {
cacheItems();
isLoading = true;
notifyListeners();
searchController.addListener(_searchWithCompute);
await _testSearch();
searchController.removeListener(_searchWithCompute);
isLoading = false;
notifyListeners();
}
@alphamikle
alphamikle / search_with_isolate.dart
Created January 31, 2021 13:59
Search with isolate
/// Start operation in isolate by sending message
Future<void> runSearchInIsolate() async {
send(Events.cacheItems);
}
void _middleLoadingEvent() {
final double time = bench.endTimer('Load items in separate isolate');
requestDurations.add(time);
bench.startTimer('Load items in separate isolate');
}
@alphamikle
alphamikle / search_with_isolate_backend.dart
Created January 31, 2021 14:00
Search with isolate backend
/// Handler for event [Events.cacheItems]
void _cacheItems() {
_notFilteredItems.clear();
final List<Item> multipliedItems = [];
for (int i = 0; i < 10; i++) {
multipliedItems.addAll(_items);
}
_notFilteredItems.addAll(multipliedItems);
send(Events.cacheItems);
}
@alphamikle
alphamikle / item_data_class.dart
Created January 31, 2021 14:01
Item data class
class Item {
const Item(
this.id,
this.createdAt,
this.profile,
this.imageUrl,
);
final int id;
final DateTime createdAt;
@alphamikle
alphamikle / short_frontend_and_backend.dart
Created January 31, 2021 14:01
Frontend with backend
/// Frontend part
Future<void> decrement([int diff = 1]) async {
counter = await runBackendMethod<int, int>(Events.decrement, diff);
}
/// -----
/// Backend part
Future<int> _decrement(int diff) async {
counter -= diff;
@alphamikle
alphamikle / tasks_and_operations.dart
Created January 31, 2021 14:02
Tasks and operations
/// Frontend part
/// This block is responsible for handling events from the isolate.
@override
Map<Events, Function> get tasks => {
Events.increment: _setCounter,
Events.decrement: _setCounter,
Events.error: _setCounter,
};
/// -----
enum Events {
increment,
}
class FirstState with Frontend<Events> {
int counter = 0;
void increment([int diff = 1]) {
send(Events.increment, diff);
}
@alphamikle
alphamikle / backend_processing.dart
Created January 31, 2021 14:03
Backend processing
class FirstBackend extends Backend<Events, void> {
FirstBackend(BackendArgument<void> argument) : super(argument);
int counter = 0;
void _increment(int diff) {
counter += diff;
send(Events.increment, counter);
}
@alphamikle
alphamikle / frontend_processing.dart
Created January 31, 2021 14:04
Frontend processing
enum Events {
decrement,
}
class FirstState with ChangeNotifier, Frontend<Events> {
int counter = 0;
Future<void> decrement([int diff = 1]) async {
counter = await runBackendMethod<int, int>(Events.decrement, diff);
}
@alphamikle
alphamikle / backend_sync_mode.dart
Created January 31, 2021 14:05
Backend sync mode
class FirstBackend extends Backend<Events, void> {
FirstBackend(BackendArgument<void> argument) : super(argument);
int counter = 0;
/// Or, you can simply return a value
Future<int> _decrement(int diff) async {
counter -= diff;
return counter;
}