1 import Foundation
2 
3 public class NativeModulesProxyModule: Module {
4   public static let moduleName = "NativeModulesProxy"
5 
6   public func definition() -> ModuleDefinition {
7     Name(Self.moduleName)
8 
9     Constants { () -> [String: Any?] in
10       guard let config = self.appContext?.legacyModulesProxy?.nativeModulesConfig else {
11         // TODO: Throw, but what?
12         return [:]
13       }
14       return config.toDictionary()
15     }
16 
17     AsyncFunction("callMethod") { (moduleName: String, methodName: String, arguments: [Any], promise: Promise) in
18       guard let appContext = self.appContext else {
19         return promise.reject(Exceptions.AppContextLost())
20       }
21 
22       // Call a method on the new module if exists
23       if appContext.hasModule(moduleName) {
24         appContext.callFunction(methodName, onModule: moduleName, withArgs: arguments, resolve: promise.resolver, reject: promise.legacyRejecter)
25         return
26       }
27 
28       // Call a method on the legacy module
29       guard let legacyModule = appContext.legacyModuleRegistry?.getExportedModule(forName: moduleName) else {
30         return promise.reject(ModuleHolder.ModuleNotFoundException(moduleName))
31       }
32       legacyModule.methodQueue().async {
33         legacyModule.callExportedMethod(methodName, withArguments: arguments, resolver: promise.resolver, rejecter: promise.legacyRejecter)
34       }
35     }
36   }
37 }
38