-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #390 from cunarist/rusty-widget
Provide `finalizeRust` to shut down the tokio runtime
- Loading branch information
Showing
15 changed files
with
254 additions
and
152 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,49 @@ | ||
# Graceful Shutdown | ||
|
||
When the Flutter app is closed, the entire `tokio` async runtime on the Rust side will be terminated automatically. Even if the app is force-closed, the `tokio` async runtime will be properly dropped. | ||
When the Flutter app is closed, the entire `tokio` async runtime on the Rust side doesn't get dropped by default. | ||
|
||
When using Rinf, the lifetime of the `tokio` runtime follows that of the Dart runtime. This behavior is different from typical `tokio` executables where its async runtime lives throughout the async `main()` function of Rust. | ||
In some cases, you might need to drop all Rust resources properly before closing the app. This could include instances of structs that implement the `Drop` trait, which have roles like saving files or disposing of resources. | ||
|
||
In some cases, you might need to run some finalization code in Rust before the app closes. This might involve saving files or disposing of resources. To achieve this, you can use Flutter's `AppLifecycleListener` to run something or to get user confirmation before closing the Flutter app. | ||
To achieve this, you can utilize Flutter's `AppLifecycleListener` to call the `finalizeRust` function before closing the Flutter app. | ||
|
||
```dart title="lib/main.dart" | ||
import 'dart:ui'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:rinf/rinf.dart'; | ||
class MyApp extends StatefulWidget { | ||
const MyApp({super.key}); | ||
@override | ||
State<MyApp> createState() => _MyAppState(); | ||
} | ||
class _MyAppState extends State<MyApp> { | ||
final _appLifecycleListener = AppLifecycleListener( | ||
onExitRequested: () async { | ||
// Do something here before the app is exited. | ||
return AppExitResponse.exit; | ||
}, | ||
); | ||
late final AppLifecycleListener _listener; | ||
@override | ||
void initState() { | ||
super.initState(); | ||
_listener = AppLifecycleListener( | ||
onExitRequested: () async { | ||
finalizeRust(); // Shut down the `tokio` Rust runtime. | ||
return AppExitResponse.exit; | ||
}, | ||
); | ||
} | ||
@override | ||
void dispose() { | ||
_appLifecycleListener.dispose(); | ||
_listener.dispose(); | ||
super.dispose(); | ||
} | ||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
title: 'Some App', | ||
home: MyHomePage(), | ||
); | ||
// Return a widget. | ||
} | ||
} | ||
``` | ||
|
||
It's worth noting that `AppLifecycleListener` or `dispose` cannot always be relied upon for app closings. Below is a text snippet quoted from the official [Flutter docs](https://api.flutter.dev/flutter/widgets/State/dispose.html): | ||
It's worth noting that `AppLifecycleListener` cannot always be relied upon for app closings. Below is a text snippet quoted from the official [Flutter docs](https://api.flutter.dev/flutter/widgets/State/dispose.html): | ||
|
||
> There is no way to predict when application shutdown will happen. For example, a user's battery could catch fire, or the user could drop the device into a swimming pool, or the operating system could unilaterally terminate the application process due to memory pressure. Applications are responsible for ensuring they behave well even in the face of rapid, unscheduled termination. |
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
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
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
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
Oops, something went wrong.