1 import UIKit 2 3 /** 4 Extends all modules with the functions used to build a module definition. 5 Unfortunately they need to be scoped here, but hopefully this proposal 6 https://github.com/apple/swift-evolution/blob/main/proposals/0289-result-builders.md#builder-scoped-name-lookup 7 will be implemented in the future. 8 */ 9 extension AnyModule { 10 // MARK: - Module name 11 12 /** 13 Sets the name of the module that is exported to the JavaScript world. 14 */ 15 public func name(_ name: String) -> AnyDefinition { 16 return ModuleNameDefinition(name: name) 17 } 18 19 // MARK: - Module's lifecycle 20 21 /** 22 Creates module's lifecycle listener that is called right after module initialization. 23 */ 24 public func onCreate(_ closure: @escaping () -> Void) -> AnyDefinition { 25 return EventListener(.moduleCreate, closure) 26 } 27 28 /** 29 Creates module's lifecycle listener that is called when the module is about to be deallocated. 30 */ 31 public func onDestroy(_ closure: @escaping () -> Void) -> AnyDefinition { 32 return EventListener(.moduleDestroy, closure) 33 } 34 35 /** 36 Creates module's lifecycle listener that is called when the app context owning the module is about to be deallocated. 37 */ 38 public func onAppContextDestroys(_ closure: @escaping () -> Void) -> AnyDefinition { 39 return EventListener(.appContextDestroys, closure) 40 } 41 42 /** 43 Creates a listener that is called when the app is about to enter the foreground mode. 44 */ 45 public func onAppEntersForeground(_ closure: @escaping () -> Void) -> AnyDefinition { 46 return EventListener(.appEntersForeground, closure) 47 } 48 49 /** 50 Creates a listener that is called when the app becomes active again. 51 */ 52 public func onAppBecomesActive(_ closure: @escaping () -> Void) -> AnyDefinition { 53 return EventListener(.appBecomesActive, closure) 54 } 55 56 /** 57 Creates a listener that is called when the app enters the background mode. 58 */ 59 public func onAppEntersBackground(_ closure: @escaping () -> Void) -> AnyDefinition { 60 return EventListener(.appEntersBackground, closure) 61 } 62 63 // MARK: - View Manager 64 65 /** 66 Creates the view manager definition that scopes other view-related definitions. 67 */ 68 public func viewManager(@ViewManagerDefinitionBuilder _ closure: @escaping () -> ViewManagerDefinition) -> AnyDefinition { 69 return closure() 70 } 71 } 72 73 // TODO: - Remove deprecated `method` component once SDK44 is out. 74 public extension AnyModule { 75 /** 76 Function without arguments. 77 */ 78 @available(*, deprecated, renamed: "function") 79 func method<R>( 80 _ name: String, 81 _ closure: @escaping () -> R 82 ) -> AnyFunction { 83 return ConcreteFunction( 84 name, 85 argTypes: [], 86 closure 87 ) 88 } 89 90 /** 91 Function with one argument. 92 */ 93 @available(*, deprecated, renamed: "function") 94 func method<R, A0: AnyArgument>( 95 _ name: String, 96 _ closure: @escaping (A0) -> R 97 ) -> AnyFunction { 98 return ConcreteFunction( 99 name, 100 argTypes: [ArgumentType(A0.self)], 101 closure 102 ) 103 } 104 105 /** 106 Function with two arguments. 107 */ 108 @available(*, deprecated, renamed: "function") 109 func method<R, A0: AnyArgument, A1: AnyArgument>( 110 _ name: String, 111 _ closure: @escaping (A0, A1) -> R 112 ) -> AnyFunction { 113 return ConcreteFunction( 114 name, 115 argTypes: [ArgumentType(A0.self), ArgumentType(A1.self)], 116 closure 117 ) 118 } 119 } 120