startup.dart 3.5 KB

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