This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Future<void> runSearchWithCompute() async { | |
cacheItems(); | |
isLoading = true; | |
notifyListeners(); | |
searchController.addListener(_searchWithCompute); | |
await _testSearch(); | |
searchController.removeListener(_searchWithCompute); | |
isLoading = false; | |
notifyListeners(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Item { | |
const Item( | |
this.id, | |
this.createdAt, | |
this.profile, | |
this.imageUrl, | |
); | |
final int id; | |
final DateTime createdAt; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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, | |
}; | |
/// ----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Events { | |
increment, | |
} | |
class FirstState with Frontend<Events> { | |
int counter = 0; | |
void increment([int diff = 1]) { | |
send(Events.increment, diff); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |