startup.dart 3.9 KB

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