AppKernel.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { ANDROID, HTML5, IOS } from 'cc/env';
  2. import type { AppEventMap } from './AppEvents';
  3. import { EventManager } from '../framework/event/EventManager';
  4. import { ResourceManager } from '../framework/resource/ResourceManager';
  5. import { SceneNavigator } from '../framework/scene/SceneNavigator';
  6. import { UIManager } from '../framework/ui/UIManager';
  7. import { AndroidPlatformBridge } from '../platform/AndroidPlatformBridge';
  8. import { H5PlatformBridge } from '../platform/H5PlatformBridge';
  9. import { IOSPlatformBridge } from '../platform/IOSPlatformBridge';
  10. import { PlatformBridge } from '../platform/PlatformBridge';
  11. import { HotUpdateService } from '../services/hotupdate/HotUpdateService';
  12. import { VersionService } from '../services/version/VersionService';
  13. import * as stores from '../store';
  14. import * as services from '../services';
  15. export class AppKernel {
  16. private static instance: AppKernel | null = null;
  17. static initialize(): AppKernel {
  18. if (!this.instance) {
  19. this.instance = new AppKernel();
  20. }
  21. return this.instance;
  22. }
  23. static get Instance(): AppKernel {
  24. if (!this.instance) throw new Error('AppKernel has not been initialized');
  25. return this.instance;
  26. }
  27. static get events(): EventManager<AppEventMap> {
  28. return this.Instance.events;
  29. }
  30. static get resources(): ResourceManager {
  31. return this.Instance.resources;
  32. }
  33. static get ui(): UIManager {
  34. return this.Instance.ui;
  35. }
  36. static get scenes(): SceneNavigator {
  37. return this.Instance.scenes;
  38. }
  39. static get version(): VersionService {
  40. return this.Instance.version;
  41. }
  42. static get hotUpdate(): HotUpdateService {
  43. return this.Instance.hotUpdate;
  44. }
  45. static get platform(): PlatformBridge {
  46. return this.Instance.platform;
  47. }
  48. static get stores(): typeof stores {
  49. return this.Instance.stores;
  50. }
  51. static get services(): typeof services {
  52. return this.Instance.services;
  53. }
  54. readonly events = new EventManager<AppEventMap>();
  55. readonly resources = new ResourceManager();
  56. readonly ui = new UIManager(this.resources);
  57. readonly scenes = new SceneNavigator(this.resources);
  58. readonly version = new VersionService();
  59. readonly hotUpdate = new HotUpdateService();
  60. readonly platform: PlatformBridge;
  61. readonly stores = stores;
  62. readonly services = services;
  63. private constructor() {
  64. this.platform = this.createPlatformBridge();
  65. }
  66. private createPlatformBridge(): PlatformBridge {
  67. if (ANDROID) return new AndroidPlatformBridge();
  68. if (IOS) return new IOSPlatformBridge();
  69. return new H5PlatformBridge();
  70. }
  71. }