-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add transport failed init state (#343)
* feat: add bootstrap failed page * feat: complete bootstrap rerun mechanism * fix: router guard rules for bootstrap failed state * fix: newline before return --------- Co-authored-by: nesquikm <[email protected]>
- Loading branch information
Showing
19 changed files
with
482 additions
and
91 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
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
21 changes: 21 additions & 0 deletions
21
lib/app/router/routs/bootstrap_failed/bootstrap_failed.dart
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import 'package:app/app/router/router.dart'; | ||
import 'package:app/app/service/service.dart'; | ||
import 'package:app/feature/bootstrap_failed/bootstrap_failed.dart'; | ||
import 'package:go_router/go_router.dart'; | ||
|
||
const bootstrapFailedIndexPathParam = 'bootstrapStepIndexPathParam'; | ||
|
||
GoRoute get bootstrapFailedRoute { | ||
return GoRoute( | ||
name: AppRoute.bootstrapFailedInit.name, | ||
path: AppRoute.bootstrapFailedInit.path, | ||
builder: (_, state) => BootstrapFailedPage( | ||
step: BootstrapSteps.values[int.parse( | ||
state.pathParameters[bootstrapFailedIndexPathParam]!, | ||
)], | ||
), | ||
routes: [ | ||
configureNetworksRoute, | ||
], | ||
); | ||
} |
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,4 +1,5 @@ | ||
export 'add_seed/add_seed.dart'; | ||
export 'bootstrap_failed/bootstrap_failed.dart'; | ||
export 'browser/browser.dart'; | ||
export 'profile/profile.dart'; | ||
export 'wallet/wallet.dart'; |
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 |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import 'package:app/bootstrap.dart'; | ||
import 'package:app/bootstrap/bootstrap.dart'; | ||
import 'package:injectable/injectable.dart'; | ||
import 'package:logging/logging.dart'; | ||
import 'package:rxdart/rxdart.dart'; | ||
|
||
typedef AsyncFunc = Future<void> Function(); | ||
|
||
/// Steps that will be handled during bootstrap process. | ||
/// If app fails during some step, then it will be easy to rerun this process. | ||
/// [storage] - failed at storage step initialization | ||
/// [connection] - failed during creating connection | ||
/// [features] - failed during creating features | ||
/// [completed] - everything is ok, app works normally | ||
enum BootstrapSteps { | ||
storage, | ||
connection, | ||
features, | ||
completed, | ||
} | ||
|
||
/// Service that allows initialize app step by step and re-run some operations | ||
/// if they failed. | ||
@singleton | ||
class BootstrapService { | ||
final _log = Logger('bootstrap'); | ||
|
||
final _bootstrapStepSubject = BehaviorSubject<BootstrapSteps>(); | ||
|
||
Stream<BootstrapSteps> get bootstrapStepStream => _bootstrapStepSubject; | ||
|
||
BootstrapSteps get bootstrapStep => _bootstrapStepSubject.value; | ||
|
||
Future<void> init(AppBuildType appBuildType) async { | ||
try { | ||
await coreStep(appBuildType); | ||
|
||
_bootstrapStepSubject.add(BootstrapSteps.storage); | ||
await storageStep(); | ||
|
||
_bootstrapStepSubject.add(BootstrapSteps.connection); | ||
await connectionStep(); | ||
|
||
_bootstrapStepSubject.add(BootstrapSteps.features); | ||
await featureStep(); | ||
|
||
_bootstrapStepSubject.add(BootstrapSteps.completed); | ||
} catch (e, t) { | ||
_log.severe('init', e, t); | ||
} | ||
} | ||
|
||
Future<void> rerunFailedSteps() async { | ||
var failedStep = bootstrapStep; | ||
final failedStepIndex = failedStep.index; | ||
|
||
if (failedStep == BootstrapSteps.completed) return; | ||
|
||
// ignore: avoid-missing-enum-constant-in-map | ||
final steps = <BootstrapSteps, AsyncFunc>{ | ||
BootstrapSteps.storage: storageStep, | ||
BootstrapSteps.connection: connectionStep, | ||
BootstrapSteps.features: featureStep, | ||
}; | ||
|
||
try { | ||
for (var index = failedStepIndex; | ||
index < BootstrapSteps.completed.index; | ||
index++) { | ||
final currentStep = BootstrapSteps.values[index]; | ||
// we do not update step during initialization to avoid changing page | ||
// in ui | ||
failedStep = currentStep; | ||
await steps[currentStep]!(); | ||
} | ||
|
||
_bootstrapStepSubject.add(BootstrapSteps.completed); | ||
} catch (e, t) { | ||
_log.severe('rerunFailedSteps:${failedStep.name}', e, t); | ||
// update ui | ||
_bootstrapStepSubject.add(failedStep); | ||
|
||
// allow cubit catch this error | ||
rethrow; | ||
} | ||
} | ||
|
||
/// This step can not be failed during initialization, so we do not let | ||
/// it to be re-runed (if failed - that's gg). | ||
Future<void> coreStep(AppBuildType appBuildType) async { | ||
await configureAppVersion(); | ||
await configureLogger(appBuildType); | ||
} | ||
|
||
Future<void> storageStep() async { | ||
await configureEncryptedStorage(); | ||
await configureNavigationService(); | ||
await migrateStorage(); | ||
await configureStorageServices(); | ||
await configureNtpService(); | ||
// SetUp nekoton after storage migrations | ||
await configureNekoton(); | ||
await configureBiometry(); | ||
} | ||
|
||
Future<void> connectionStep() async { | ||
await configureConnectionService(); | ||
} | ||
|
||
Future<void> featureStep() async { | ||
await configureFeatureServices(); | ||
} | ||
} |
Oops, something went wrong.