Skip to content

Commit 8c6f7f4

Browse files
committed
Project Support
-add pubspec.yaml -flutter lib/main -dart bin Signed-off-by: Joel Winarske <joel.winarske@toyotaconnected.com>
1 parent 5bf7df3 commit 8c6f7f4

8 files changed

+81
-24
lines changed

loading_compute.dart renamed to bin/loading_compute.dart

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,20 @@ Future<void> loadDataFromCompute() async {
1414
}
1515

1616
// alternative way
17-
result = await compute((int count) async {
18-
List<Map<String, dynamic>> list = [];
19-
for (int i = 0; i < 10; i++) {
20-
list.add(await fetchData(count));
21-
}
22-
return list;
23-
}, 5,);
17+
result = await compute(
18+
(int count) async {
19+
List<Map<String, dynamic>> list = [];
20+
for (int i = 0; i < 10; i++) {
21+
list.add(await fetchData(count));
22+
}
23+
return list;
24+
},
25+
5,
26+
);
2427
}
2528

2629
Future<Map<String, dynamic>> fetchData(int requestCount) async {
27-
final url = Uri.parse('https://api.example.com/data');
30+
final url = Uri.parse('http://headers.jsontest.com/');
2831
List<Future<http.Response>> futures = [];
2932
for (int j = 0; j < requestCount; j++) {
3033
futures.add(http.get(url));
@@ -45,4 +48,4 @@ Future<Map<String, dynamic>> fetchData(int requestCount) async {
4548
print('An error occurred: $e');
4649
}
4750
return {};
48-
}
51+
}

loading_isolate.dart renamed to bin/loading_isolate.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Future<void> loadDataFromIsolate() async {
1010
List<Map<String, dynamic>> result = [];
1111
final receivePort = ReceivePort();
1212
final isolate = await Isolate.spawn(fetchDataIsolate, receivePort.sendPort);
13+
print(isolate.debugName);
1314
await for (var response in receivePort) {
1415
if (response == null) {
1516
break; // Exit the loop on completion signal
@@ -29,7 +30,7 @@ void fetchDataIsolate(SendPort sendPort) async {
2930
}
3031

3132
Future<Map<String, dynamic>> fetchData({int requestCount = 1}) async {
32-
final url = Uri.parse('https://api.example.com/data');
33+
final url = Uri.parse('http://headers.jsontest.com/');
3334
List<Future<http.Response>> futures = [];
3435
for (int j = 0; j < requestCount; j++) {
3536
futures.add(http.get(url));

loading_main_thread.dart renamed to bin/loading_main_thread.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Future<void> loadDataFromMainThread() async {
1313
}
1414

1515
Future<Map<String, dynamic>> fetchData({int requestCount = 1}) async {
16-
final url = Uri.parse('https://api.example.com/data');
16+
final url = Uri.parse('http://headers.jsontest.com/');
1717
List<Future<http.Response>> futures = [];
1818
for (int j = 0; j < requestCount; j++) {
1919
futures.add(http.get(url));
@@ -34,4 +34,4 @@ Future<Map<String, dynamic>> fetchData({int requestCount = 1}) async {
3434
print('An error occurred: $e');
3535
}
3636
return {};
37-
}
37+
}

lib/main.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
search_isolate.dart

search_compute.dart renamed to lib/search_compute.dart

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:math';
22
import 'package:compute/compute.dart';
33

44
import 'package:flutter/material.dart';
5+
import 'package:flutter/widgets.dart';
56

67
List<Map<String, dynamic>> list = []; // computedData
78
List<Map<String, dynamic>> filteredList = [];
@@ -18,12 +19,14 @@ Future<void> searchFromCompute() async {
1819
searching = true;
1920
final input = inputController.text;
2021
if (input.isNotEmpty && list.isNotEmpty) {
21-
await compute(() {
22-
filteredList.clear();
23-
filteredList.addAll(list.where((Map<String, dynamic> map) {
24-
return map.values.where((e) => e.value.contains(input)).isNotEmpty;
25-
}).toList());
26-
} as ComputeCallback<void, dynamic>, null);
22+
await compute(
23+
() {
24+
filteredList.clear();
25+
filteredList.addAll(list.where((Map<String, dynamic> map) {
26+
return map.values.where((e) => e.value.contains(input)).isNotEmpty;
27+
}).toList());
28+
} as ComputeCallback<void, dynamic>,
29+
null);
2730
}
2831
searching = false;
2932
}
@@ -33,7 +36,11 @@ Future<void> emulateTextInput() async {
3336
for (int i = 0; i < list.length; i++) {
3437
words.addAll(list[i].values.map((e) => e.value as String).toSet().toList());
3538
}
36-
words = words.map((String word) => word.substring(0, min(word.length, 3))).toSet().take(3).toList();
39+
words = words
40+
.map((String word) => word.substring(0, min(word.length, 3)))
41+
.toSet()
42+
.take(3)
43+
.toList();
3744

3845
for (var word in words) {
3946
final List<String> letters = word.split('');
@@ -57,4 +64,4 @@ Future<void> inputText(String word) async {
5764
inputController.value = TextEditingValue(text: word);
5865
await Future.delayed(const Duration(milliseconds: 500));
5966
}
60-
}
67+
}

search_isolate.dart renamed to lib/search_isolate.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:isolate';
22
import 'dart:math';
33

44
import 'package:flutter/material.dart';
5+
import 'package:flutter/widgets.dart';
56

67
List<Map<String, dynamic>> list = []; // computedData
78
List<Map<String, dynamic>> filteredList = [];
@@ -18,6 +19,7 @@ Future<void> searchFromIsolate() async {
1819
searching = true;
1920
final ReceivePort receivePort = ReceivePort();
2021
final isolate = await Isolate.spawn(filterDataIsolate, receivePort.sendPort);
22+
print(isolate.debugName);
2123
await for (var response in receivePort) {
2224
if (response == null) {
2325
break;
@@ -43,7 +45,11 @@ Future<void> emulateTextInput() async {
4345
for (int i = 0; i < list.length; i++) {
4446
words.addAll(list[i].values.map((e) => e.value as String).toSet().toList());
4547
}
46-
words = words.map((String word) => word.substring(0, min(word.length, 3))).toSet().take(3).toList();
48+
words = words
49+
.map((String word) => word.substring(0, min(word.length, 3)))
50+
.toSet()
51+
.take(3)
52+
.toList();
4753

4854
for (var word in words) {
4955
final List<String> letters = word.split('');
@@ -67,4 +73,4 @@ Future<void> inputText(String word) async {
6773
inputController.value = TextEditingValue(text: word);
6874
await Future.delayed(const Duration(milliseconds: 500));
6975
}
70-
}
76+
}

search_main_thread.dart renamed to lib/search_main_thread.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:math';
22

33
import 'package:flutter/material.dart';
4+
import 'package:flutter/widgets.dart';
45

56
List<Map<String, dynamic>> list = []; // computedData
67
List<Map<String, dynamic>> filteredList = [];
@@ -30,7 +31,11 @@ Future<void> emulateTextInput() async {
3031
for (int i = 0; i < list.length; i++) {
3132
words.addAll(list[i].values.map((e) => e.value as String).toSet().toList());
3233
}
33-
words = words.map((String word) => word.substring(0, min(word.length, 3))).toSet().take(3).toList();
34+
words = words
35+
.map((String word) => word.substring(0, min(word.length, 3)))
36+
.toSet()
37+
.take(3)
38+
.toList();
3439

3540
for (var word in words) {
3641
final List<String> letters = word.split('');
@@ -54,4 +59,4 @@ Future<void> inputText(String word) async {
5459
inputController.value = TextEditingValue(text: word);
5560
await Future.delayed(const Duration(milliseconds: 500));
5661
}
57-
}
62+
}

pubspec.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: isolate_examples
2+
version: 0.1.0
3+
4+
description:
5+
Isolate examples
6+
7+
homepage: https://github.com/jwinarske/isolates.dart
8+
9+
environment:
10+
sdk: '>=3.5.0 <4.0.0'
11+
12+
platforms:
13+
linux:
14+
15+
dependencies:
16+
args: ^2.5.0
17+
compute: ^1.0.2
18+
ffi: ^2.1.3
19+
flutter:
20+
sdk: flutter
21+
http: ^1.2.2
22+
23+
dev_dependencies:
24+
lints: ^4.0.0
25+
test: ^1.25.8
26+
test_cov: ^1.0.1
27+
28+
executables:
29+
loading-compute: loading_compute
30+
loading-isolate: loading_isolate
31+
loading-main-thread: loading_main_thread
32+
search-compute: search_compute
33+
search-isolate: search_isolate
34+
search-main-thread: search_main_thread

0 commit comments

Comments
 (0)