startup.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import 'dart:io';
  2. import 'package:app_flowy/startup/tasks/prelude.dart';
  3. import 'package:flutter/foundation.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:get_it/get_it.dart';
  6. import 'package:app_flowy/workspace/infrastructure/deps_resolver.dart';
  7. import 'package:app_flowy/user/infrastructure/deps_resolver.dart';
  8. import 'package:flowy_sdk/flowy_sdk.dart';
  9. // [[diagram: flowy startup flow]]
  10. // ┌──────────┐
  11. // │ FlowyApp │
  12. // └──────────┘
  13. // │ impl
  14. // ▼
  15. // ┌────────┐ 1.run ┌──────────┐
  16. // │ System │───┬───▶│EntryPoint│
  17. // └────────┘ │ └──────────┘ ┌─────────────────┐
  18. // │ ┌──▶ │ RustSDKInitTask │
  19. // │ ┌───────────┐ │ └─────────────────┘
  20. // └──▶ │AppLauncher│───┤
  21. // 2.launch └───────────┘ │ ┌─────────────┐ ┌──────────────────┐ ┌───────────────┐
  22. // └───▶│AppWidgetTask│────────▶│ApplicationWidget │─────▶│ SplashScreen │
  23. // └─────────────┘ └──────────────────┘ └───────────────┘
  24. //
  25. // 3.build MeterialApp
  26. final getIt = GetIt.instance;
  27. abstract class EntryPoint {
  28. Widget create();
  29. }
  30. class FlowySystem {
  31. static Future<void> run(EntryPoint f) async {
  32. // Specify the env
  33. final env = integrationEnv();
  34. initGetIt(getIt, env, f);
  35. // add task
  36. getIt<AppLauncher>().addTask(InitRustSDKTask());
  37. if (!env.isTest()) {
  38. getIt<AppLauncher>().addTask(ApplicationWidgetTask());
  39. getIt<AppLauncher>().addTask(InitPlatformService());
  40. }
  41. // execute the tasks
  42. getIt<AppLauncher>().launch();
  43. }
  44. }
  45. Future<void> initGetIt(
  46. GetIt getIt,
  47. IntegrationEnv env,
  48. EntryPoint f,
  49. ) async {
  50. getIt.registerFactory<EntryPoint>(() => f);
  51. getIt.registerLazySingleton<FlowySDK>(() => const FlowySDK());
  52. getIt.registerLazySingleton<AppLauncher>(() => AppLauncher(env, getIt));
  53. await UserDepsResolver.resolve(getIt);
  54. await HomeDepsResolver.resolve(getIt);
  55. }
  56. class LaunchContext {
  57. GetIt getIt;
  58. IntegrationEnv env;
  59. LaunchContext(this.getIt, this.env);
  60. }
  61. enum LaunchTaskType {
  62. dataProcessing,
  63. appLauncher,
  64. }
  65. /// The interface of an app launch task, which will trigger
  66. /// some nonresident indispensable task in app launching task.
  67. abstract class LaunchTask {
  68. LaunchTaskType get type => LaunchTaskType.dataProcessing;
  69. Future<void> initialize(LaunchContext context);
  70. }
  71. class AppLauncher {
  72. List<LaunchTask> tasks;
  73. IntegrationEnv env;
  74. GetIt getIt;
  75. AppLauncher(this.env, this.getIt) : tasks = List.from([]);
  76. void addTask(LaunchTask task) {
  77. tasks.add(task);
  78. }
  79. Future<void> launch() async {
  80. final context = LaunchContext(getIt, env);
  81. for (var task in tasks) {
  82. await task.initialize(context);
  83. }
  84. }
  85. }
  86. enum IntegrationEnv {
  87. develop,
  88. release,
  89. test,
  90. }
  91. extension IntegrationEnvExt on IntegrationEnv {
  92. bool isTest() {
  93. return this == IntegrationEnv.test;
  94. }
  95. }
  96. IntegrationEnv integrationEnv() {
  97. if (Platform.environment.containsKey('FLUTTER_TEST')) {
  98. return IntegrationEnv.test;
  99. }
  100. if (kReleaseMode) {
  101. return IntegrationEnv.release;
  102. }
  103. return IntegrationEnv.develop;
  104. }