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   /**
11    Sets the name of the module that is exported to the JavaScript world.
12    */
13   public func name(_ name: String) -> AnyDefinition {
14     return ModuleNameDefinition(name: name)
15   }
16 
17   /**
18    Definition function setting the module's constants to export.
19    */
20   public func constants(_ closure: () -> [String : Any?]) -> AnyDefinition {
21     return ConstantsDefinition(constants: closure())
22   }
23 
24   /**
25    Factory function for methods without arguments.
26    */
27   public func method<R>(
28     _ name: String,
29     _ closure: @escaping () -> R
30   ) -> AnyMethod {
31     return ConcreteMethod(
32       name,
33       argTypes: [],
34       closure
35     )
36   }
37 
38   /**
39    Factory function for methods with one argument.
40    */
41   public func method<R, A0: AnyMethodArgument>(
42     _ name: String,
43     _ closure: @escaping (A0) -> R
44   ) -> AnyMethod {
45     return ConcreteMethod(
46       name,
47       argTypes: [AnyArgumentType(A0.self)],
48       closure
49     )
50   }
51 
52   /**
53    Factory function for methods with 2 arguments.
54    */
55   public func method<R, A0: AnyMethodArgument, A1: AnyMethodArgument>(
56     _ name: String,
57     _ closure: @escaping (A0, A1) -> R
58   ) -> AnyMethod {
59     return ConcreteMethod(
60       name,
61       argTypes: [AnyArgumentType(A0.self), AnyArgumentType(A1.self)],
62       closure
63     )
64   }
65 
66   /**
67    Factory function for methods with 3 arguments.
68    */
69   public func method<R, A0: AnyMethodArgument, A1: AnyMethodArgument, A2: AnyMethodArgument>(
70     _ name: String,
71     _ closure: @escaping (A0, A1, A2) -> R
72   ) -> AnyMethod {
73     return ConcreteMethod(
74       name,
75       argTypes: [AnyArgumentType(A0.self), AnyArgumentType(A1.self), AnyArgumentType(A2.self)],
76       closure
77     )
78   }
79 
80   /**
81    Factory function for methods with 4 arguments.
82    */
83   public func method<R, A0: AnyMethodArgument, A1: AnyMethodArgument, A2: AnyMethodArgument, A3: AnyMethodArgument>(
84     _ name: String,
85     _ closure: @escaping (A0, A1, A2, A3) -> R
86   ) -> AnyMethod {
87     return ConcreteMethod(
88       name,
89       argTypes: [AnyArgumentType(A0.self), AnyArgumentType(A1.self), AnyArgumentType(A2.self), AnyArgumentType(A3.self)],
90       closure
91     )
92   }
93 
94   /**
95    Factory function for methods with 5 arguments.
96    */
97   public func method<R, A0: AnyMethodArgument, A1: AnyMethodArgument, A2: AnyMethodArgument, A3: AnyMethodArgument, A4: AnyMethodArgument>(
98     _ name: String,
99     _ closure: @escaping (A0, A1, A2, A3, A4) -> R
100   ) -> AnyMethod {
101     return ConcreteMethod(
102       name,
103       argTypes: [AnyArgumentType(A0.self), AnyArgumentType(A1.self), AnyArgumentType(A2.self), AnyArgumentType(A3.self), AnyArgumentType(A4.self)],
104       closure
105     )
106   }
107 
108   /**
109    Factory function for methods with 6 arguments.
110    */
111   public func method<R, A0: AnyMethodArgument, A1: AnyMethodArgument, A2: AnyMethodArgument, A3: AnyMethodArgument, A4: AnyMethodArgument, A5: AnyMethodArgument>(
112     _ name: String,
113     _ closure: @escaping (A0, A1, A2, A3, A4, A5) -> R
114   ) -> AnyMethod {
115     return ConcreteMethod(
116       name,
117       argTypes: [AnyArgumentType(A0.self), AnyArgumentType(A1.self), AnyArgumentType(A2.self), AnyArgumentType(A3.self), AnyArgumentType(A4.self), AnyArgumentType(A5.self)],
118       closure
119     )
120   }
121 
122   /**
123    Factory function for methods with 7 arguments.
124    */
125   public func method<R, A0: AnyMethodArgument, A1: AnyMethodArgument, A2: AnyMethodArgument, A3: AnyMethodArgument, A4: AnyMethodArgument, A5: AnyMethodArgument, A6: AnyMethodArgument>(
126     _ name: String,
127     _ closure: @escaping (A0, A1, A2, A3, A4, A5, A6) -> R
128   ) -> AnyMethod {
129     return ConcreteMethod(
130       name,
131       argTypes: [AnyArgumentType(A0.self), AnyArgumentType(A1.self), AnyArgumentType(A2.self), AnyArgumentType(A3.self), AnyArgumentType(A4.self), AnyArgumentType(A5.self), AnyArgumentType(A6.self)],
132       closure
133     )
134   }
135 
136   /**
137    Factory function for methods with 8 arguments.
138    */
139   public func method<R, A0: AnyMethodArgument, A1: AnyMethodArgument, A2: AnyMethodArgument, A3: AnyMethodArgument, A4: AnyMethodArgument, A5: AnyMethodArgument, A6: AnyMethodArgument, A7: AnyMethodArgument>(
140     _ name: String,
141     _ closure: @escaping (A0, A1, A2, A3, A4, A5, A6, A7) -> R
142   ) -> AnyMethod {
143     return ConcreteMethod(
144       name,
145       argTypes: [AnyArgumentType(A0.self), AnyArgumentType(A1.self), AnyArgumentType(A2.self), AnyArgumentType(A3.self), AnyArgumentType(A4.self), AnyArgumentType(A5.self), AnyArgumentType(A6.self), AnyArgumentType(A7.self)],
146       closure
147     )
148   }
149 
150   /**
151    Creates module's lifecycle listener that is called right after module initialization.
152    */
153   public func onCreate(_ closure: @escaping () -> Void) -> AnyDefinition {
154     return EventListener(.moduleCreate, closure)
155   }
156 
157   /**
158    Creates module's lifecycle listener that is called when the module is about to be deallocated.
159    */
160   public func onDestroy(_ closure: @escaping () -> Void) -> AnyDefinition {
161     return EventListener(.moduleDestroy, closure)
162   }
163 
164   /**
165    Creates module's lifecycle listener that is called when the app context owning the module is about to be deallocated.
166    */
167   public func onAppContextDestroys(_ closure: @escaping () -> Void) -> AnyDefinition {
168     return EventListener(.appContextDestroys, closure)
169   }
170 
171   /**
172    Creates a listener that is called when the app is about to enter the foreground mode.
173    */
174   public func onAppEntersForeground(_ closure: @escaping () -> Void) -> AnyDefinition {
175     return EventListener(.appEntersForeground, closure)
176   }
177 
178   /**
179    Creates a listener that is called when the app becomes active again.
180    */
181   public func onAppBecomesActive(_ closure: @escaping () -> Void) -> AnyDefinition {
182     return EventListener(.appBecomesActive, closure)
183   }
184 
185   /**
186    Creates a listener that is called when the app enters the background mode.
187    */
188   public func onAppEntersBackground(_ closure: @escaping () -> Void) -> AnyDefinition {
189     return EventListener(.appEntersBackground, closure)
190   }
191 }
192 
193 /**
194  Creates the view manager definition that scopes other view-related definitions.
195  */
196 public func viewManager(@ViewManagerDefinitionBuilder _ closure: @escaping () -> ViewManagerDefinition) -> AnyDefinition {
197   return closure()
198 }
199 
200 /**
201  Defines the factory creating a native view when the module is used as a view.
202  */
203 public func view(_ closure: @escaping () -> UIView) -> AnyDefinition {
204   return ViewFactory(closure)
205 }
206 
207 /**
208  Creates a view prop that defines its name and setter.
209  */
210 public func prop<ViewType: UIView, PropType>(_ name: String, _ setter: @escaping (ViewType, PropType) -> Void) -> AnyDefinition {
211   return ConcreteViewProp(name, setter)
212 }
213