isolate sample maintenance (#167)

* isolate sample maintenance

- use slightly more meaningful names
- rename files
- misc small readability changes

* update MAINTENANCE.md

* fix gh username

* formatting

* upgrade dependencies
pull/170/head
John Ryan 5 years ago committed by GitHub
parent 23d71e057d
commit f325e685d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -10,7 +10,7 @@ match any new language/SDK features, etc.).
| chrome-os-best-practices | | |
| experimental | | |
| flutter_maps_firestore | | |
| isolate_example | redbrogdon | 9/12/19 |
| isolate_example | johnpryan | 11/21/19 |
| jsonexample | | |
| material_studies/shrine | | |
| place_tracker | | |

@ -142,7 +142,9 @@ class DataTransferIsolateController extends ChangeNotifier {
}
Future<void> generateRandomNumbers(bool transferableTyped) async {
if (running) return;
if (running) {
return;
}
if (transferableTyped) {
runningTest = 2;
@ -150,7 +152,7 @@ class DataTransferIsolateController extends ChangeNotifier {
runningTest = 1;
}
Random rng = Random();
var random = Random();
currentProgress.clear();
@ -162,7 +164,7 @@ class DataTransferIsolateController extends ChangeNotifier {
randNums.clear();
for (int j = 0; j < 1000000; j++) {
randNums.add(rng.nextInt(100));
randNums.add(random.nextInt(100));
}
if (transferableTyped) {
@ -221,28 +223,28 @@ class RunningList extends StatelessWidget {
}
}
Future<void> _secondIsolateEntryPoint(SendPort callerSP) async {
ReceivePort newIceRP = ReceivePort();
callerSP.send(newIceRP.sendPort);
Future<void> _secondIsolateEntryPoint(SendPort sendPort) async {
var receivePort = ReceivePort();
sendPort.send(receivePort.sendPort);
int length = 1;
newIceRP.listen(
receivePort.listen(
(dynamic message) async {
if (message is String && message == 'start') {
await generateAndSum(callerSP, createNums(), length);
await generateAndSum(sendPort, createNums(), length);
callerSP.send('done');
sendPort.send('done');
} else if (message is TransferableTypedData) {
await generateAndSum(
callerSP, message.materialize().asInt32List(), length);
sendPort, message.materialize().asInt32List(), length);
length++;
} else if (message is List<int>) {
await generateAndSum(callerSP, message, length);
await generateAndSum(sendPort, message, length);
length++;
}
if (length == 101) {
callerSP.send('done');
sendPort.send('done');
length = 1;
}
},
@ -250,9 +252,9 @@ Future<void> _secondIsolateEntryPoint(SendPort callerSP) async {
}
Iterable<int> createNums() sync* {
final rng = Random();
var random = Random();
for (int i = 0; i < 100000000; i++) {
yield rng.nextInt(100);
yield random.nextInt(100);
}
}

@ -102,7 +102,7 @@ class InfiniteProcessPage extends StatelessWidget {
class InfiniteProcessIsolateController extends ChangeNotifier {
Isolate newIsolate;
ReceivePort mIceRP;
ReceivePort receivePort;
SendPort newIceSP;
Capability capability;
@ -120,12 +120,13 @@ class InfiniteProcessIsolateController extends ChangeNotifier {
List<int> get currentResults => _currentResults;
Future<void> createIsolate() async {
mIceRP = ReceivePort();
newIsolate = await Isolate.spawn(_secondIsolateEntryPoint, mIceRP.sendPort);
receivePort = ReceivePort();
newIsolate =
await Isolate.spawn(_secondIsolateEntryPoint, receivePort.sendPort);
}
void listen() {
mIceRP.listen((dynamic message) {
receivePort.listen((dynamic message) {
if (message is SendPort) {
newIceSP = message;
newIceSP.send(_currentMultiplier);
@ -224,7 +225,9 @@ Future<void> _secondIsolateEntryPoint(SendPort callerSP) async {
callerSP.send(newIceRP.sendPort);
newIceRP.listen((dynamic message) {
if (message is int) multiplyValue = message;
if (message is int) {
multiplyValue = message;
}
});
// This runs until the isolate is terminated.

@ -14,19 +14,19 @@
import 'package:flutter/material.dart';
import 'page_one.dart';
import 'page_three.dart';
import 'page_two.dart';
import 'data_transfer_page.dart';
import 'infinite_process_page.dart';
import 'performance_page.dart';
void main() {
runApp(
MaterialApp(
home: StartApp(),
home: HomePage(),
),
);
}
class StartApp extends StatelessWidget {
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(

@ -17,15 +17,15 @@ import 'package:flutter/material.dart';
// Computes the nth number in the Fibonacci sequence.
int fib(int n) {
int number1 = n - 1;
int number2 = n - 2;
var a = n - 1;
var b = n - 2;
if (n == 1) {
return 0;
} else if (n == 0) {
return 1;
} else {
return (fib(number1) + fib(number2));
return (fib(a) + fib(b));
}
}
@ -49,25 +49,29 @@ class _PerformancePageState extends State<PerformancePage> {
padding: EdgeInsets.only(top: 150),
child: Column(
children: [
FutureBuilder<void>(
FutureBuilder(
future: computeFuture,
builder: (context, snapshot) {
return RaisedButton(
child: const Text('Compute on Main'),
elevation: 8.0,
onPressed: createMainIsolateCallBack(context, snapshot),
onPressed:
snapshot.connectionState == ConnectionState.done
? () => handleComputeOnMain(context)
: null,
);
},
),
FutureBuilder<void>(
FutureBuilder(
future: computeFuture,
builder: (context, snapshot) {
return RaisedButton(
child: const Text('Compute on Secondary'),
elevation: 8.0,
onPressed:
createSecondaryIsolateCallBack(context, snapshot),
);
snapshot.connectionState == ConnectionState.done
? () => handleComputeOnSecondary(context)
: null);
},
),
],
@ -78,54 +82,40 @@ class _PerformancePageState extends State<PerformancePage> {
);
}
VoidCallback createMainIsolateCallBack(
BuildContext context,
AsyncSnapshot snapshot,
) {
if (snapshot.connectionState == ConnectionState.done) {
return () {
setState(() {
computeFuture = computeOnMainIsolate()
void handleComputeOnMain(BuildContext context) {
var future = computeOnMainIsolate()
..then((_) {
final snackBar = SnackBar(
var snackBar = SnackBar(
content: Text('Main Isolate Done!'),
);
Scaffold.of(context).showSnackBar(snackBar);
});
setState(() {
computeFuture = future;
});
};
} else {
return null;
}
}
VoidCallback createSecondaryIsolateCallBack(
BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return () {
setState(() {
computeFuture = computeOnSecondaryIsolate()
void handleComputeOnSecondary(BuildContext context) {
var future = computeOnSecondaryIsolate()
..then((_) {
final snackBar = SnackBar(
var snackBar = SnackBar(
content: Text('Secondary Isolate Done!'),
);
Scaffold.of(context).showSnackBar(snackBar);
});
setState(() {
computeFuture = future;
});
};
} else {
return null;
}
}
Future<void> computeOnMainIsolate() async {
// A delay is added here to give Flutter the chance to redraw the UI at least
// once before the computation (which, since it's run on the main isolate,
// will lock up the app) begins executing.
await Future.delayed(
Duration(milliseconds: 100),
() => fib(45),
);
// A delay is added here to give Flutter the chance to redraw the UI at
// least once before the computation (which, since it's run on the main
// isolate, will lock up the app) begins executing.
await Future<void>.delayed(Duration(milliseconds: 100));
fib(45);
}
Future<void> computeOnSecondaryIsolate() async {
@ -141,24 +131,22 @@ class SmoothAnimationWidget extends StatefulWidget {
class SmoothAnimationWidgetState extends State<SmoothAnimationWidget>
with TickerProviderStateMixin {
AnimationController _controller;
AnimationController _animationController;
Animation<BorderRadius> _borderAnimation;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(seconds: 1),
vsync: this,
);
_animationController =
AnimationController(duration: const Duration(seconds: 1), vsync: this);
_borderAnimation = BorderRadiusTween(
begin: BorderRadius.circular(100.0),
end: BorderRadius.circular(0.0),
).animate(_controller);
end: BorderRadius.circular(0.0))
.animate(_animationController);
_controller.repeat(reverse: true);
_animationController.repeat(reverse: true);
}
@override
@ -192,7 +180,7 @@ class SmoothAnimationWidgetState extends State<SmoothAnimationWidget>
@override
void dispose() {
_controller.dispose();
_animationController.dispose();
super.dispose();
}
}

@ -73,7 +73,7 @@ packages:
name: provider
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.0"
version: "3.1.0+1"
quiver:
dependency: transitive
description:

Loading…
Cancel
Save