1 // Copyright 2022-present 650 Industries. All rights reserved.
2 
3 import ABI47_0_0React
4 import Foundation
5 
6 /**
7  The classic bridge module that is responsible for:
8  - Creating and owning the `AppContext` when the Expo modules architecture is automatically initialized
9    by ABI47_0_0React Native (as opposed to native unit tests, where ABI47_0_0React Native is not used at all).
10  - Installing the host object to the runtime.
11  */
12 @objc(ABI47_0_0ExpoBridgeModule)
13 public final class ABI47_0_0ExpoBridgeModule: NSObject, ABI47_0_0RCTBridgeModule {
14   @objc
15   public let appContext: AppContext
16 
17   /**
18    The initializer that is used by ABI47_0_0React Native when it loads bridge modules.
19    In this scenario, we create an `AppContext` that manages the
20    architecture of Expo modules and the app itself.
21    */
22   override init() {
23     appContext = AppContext()
24     super.init()
25 
26     // Listen to ABI47_0_0React Native notifications posted just before the JS is executed.
27     NotificationCenter.default.addObserver(self,
28                                            selector: #selector(javaScriptWillStartExecutingNotification(_:)),
29                                            name: NSNotification.Name.ABI47_0_0RCTJavaScriptWillStartExecuting,
30                                            object: nil)
31   }
32 
33   deinit {
34     NotificationCenter.default.removeObserver(self)
35   }
36 
37   // MARK: - ABI47_0_0RCTBridgeModule
38 
moduleNamenull39   public static func moduleName() -> String! {
40     return "Expo"
41   }
42 
requiresMainQueueSetupnull43   public static func requiresMainQueueSetup() -> Bool {
44     return true
45   }
46 
47   public var bridge: ABI47_0_0RCTBridge! {
48     didSet {
49       appContext.reactBridge = bridge
50     }
51   }
52 
53   /**
54    This should be called inside ABI47_0_0EXNativeModulesProxy.setBridge()
55    */
56   @objc
57   public func legacyProxyDidSetBridge(legacyModulesProxy: LegacyNativeModulesProxy,
58                                       legacyModuleRegistry: ABI47_0_0EXModuleRegistry) {
59     appContext.legacyModuleRegistry = legacyModuleRegistry
60     appContext.legacyModulesProxy = legacyModulesProxy
61 
62     // we need to register all the modules after the legacy module registry is set
63     // otherwise legacy modules (e.g. permissions) won't be available in OnCreate { }
64     appContext.useModulesProvider("ABI47_0_0ExpoModulesProvider")
65     appContext.moduleRegistry.register(moduleType: NativeModulesProxyModule.self)
66   }
67 
68   // MARK: - Notifications
69 
70   @objc
javaScriptWillStartExecutingNotificationnull71   public func javaScriptWillStartExecutingNotification(_ notification: Notification) {
72     if (notification.object as? ABI47_0_0RCTBridge)?.batched == bridge {
73       // The JavaScript bundle will start executing in a moment,
74       // so the runtime is already initialized and we can get it from the bridge.
75       // This should automatically install the ExpoModules host object.
76       appContext.runtime = ABI47_0_0EXJavaScriptRuntimeManager.runtime(fromBridge: bridge)
77     }
78   }
79 }
80