Created
January 31, 2021 13:59
-
-
Save alphamikle/b9b9efa3174fcc853bf1b47e8ee93dcb to your computer and use it in GitHub Desktop.
Search with 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
/// 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 method will called on event [Events.cacheItems], which will sent by Backend | |
Future<void> _startSearchOnIsolate() async { | |
isLoading = true; | |
notifyListeners(); | |
searchController.addListener(_searchInIsolate); | |
await _testSearch(); | |
searchController.removeListener(_searchInIsolate); | |
isLoading = false; | |
notifyListeners(); | |
} | |
/// On every input event we send message to Backend | |
void _searchInIsolate() { | |
canPlaceNextLetter = false; | |
isSearching = true; | |
notifyListeners(); | |
send(Events.startSearch, searchController.text); | |
} | |
/// Writing data from Backend (isolate) to Frontend (reactive state) | |
Future<void> _setFilteredItems(List<Item> filteredItems) async { | |
isSearching = false; | |
notifyListeners(); | |
await wait(800); | |
items.clear(); | |
items.addAll(filteredItems); | |
notifyListeners(); | |
canPlaceNextLetter = true; | |
} | |
Future<void> _endLoadingEvents(List<Item> items) async { | |
this.items.clear(); | |
this.items.addAll(items); | |
final double time = bench.endTimer('Load items in separate isolate'); | |
requestDurations.add(time); | |
await wait(800); | |
isLoading = false; | |
notifyListeners(); | |
_stopFpsMeter(); | |
print('Load items in isolate ->' + requestDurations.join(' ').replaceAll('.', ',')); | |
requestDurations.clear(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment