1 /**
2  Extends all modules with the functions used to build a module definition.
3  Unfortunately they need to be scoped here, but hopefully this proposal
4  https://github.com/apple/swift-evolution/blob/main/proposals/0289-result-builders.md#builder-scoped-name-lookup
5  will be implemented in the future.
6  */
7 extension AnyModule {
8   /**
9    Sets the name of the module that is exported to the JavaScript world.
10    */
11   public func name(_ name: String) -> AnyDefinition {
12     return ModuleNameDefinition(name: name)
13   }
14 
15   /**
16    Definition function setting the module's constants to export.
17    */
18   public func constants(_ closure: () -> [String : Any?]) -> AnyDefinition {
19     return ConstantsDefinition(constants: closure())
20   }
21 
22   /**
23    Factory function for methods without arguments.
24    */
25   public func method<R>(
26     _ name: String,
27     _ closure: @escaping () -> R
28   ) -> AnyMethod {
29     return ConcreteMethod(
30       name,
31       argTypes: [],
32       closure
33     )
34   }
35 
36   /**
37    Factory function for methods with one argument.
38    */
39   public func method<R, A0: AnyMethodArgument>(
40     _ name: String,
41     _ closure: @escaping (A0) -> R
42   ) -> AnyMethod {
43     return ConcreteMethod(
44       name,
45       argTypes: [AnyArgumentType(A0.self)],
46       closure
47     )
48   }
49 
50   /**
51    Factory function for methods with 2 arguments.
52    */
53   public func method<R, A0: AnyMethodArgument, A1: AnyMethodArgument>(
54     _ name: String,
55     _ closure: @escaping (A0, A1) -> R
56   ) -> AnyMethod {
57     return ConcreteMethod(
58       name,
59       argTypes: [AnyArgumentType(A0.self), AnyArgumentType(A1.self)],
60       closure
61     )
62   }
63 
64   /**
65    Factory function for methods with 3 arguments.
66    */
67   public func method<R, A0: AnyMethodArgument, A1: AnyMethodArgument, A2: AnyMethodArgument>(
68     _ name: String,
69     _ closure: @escaping (A0, A1, A2) -> R
70   ) -> AnyMethod {
71     return ConcreteMethod(
72       name,
73       argTypes: [AnyArgumentType(A0.self), AnyArgumentType(A1.self), AnyArgumentType(A2.self)],
74       closure
75     )
76   }
77 
78   /**
79    Factory function for methods with 4 arguments.
80    */
81   public func method<R, A0: AnyMethodArgument, A1: AnyMethodArgument, A2: AnyMethodArgument, A3: AnyMethodArgument>(
82     _ name: String,
83     _ closure: @escaping (A0, A1, A2, A3) -> R
84   ) -> AnyMethod {
85     return ConcreteMethod(
86       name,
87       argTypes: [AnyArgumentType(A0.self), AnyArgumentType(A1.self), AnyArgumentType(A2.self), AnyArgumentType(A3.self)],
88       closure
89     )
90   }
91 
92   /**
93    Factory function for methods with 5 arguments.
94    */
95   public func method<R, A0: AnyMethodArgument, A1: AnyMethodArgument, A2: AnyMethodArgument, A3: AnyMethodArgument, A4: AnyMethodArgument>(
96     _ name: String,
97     _ closure: @escaping (A0, A1, A2, A3, A4) -> R
98   ) -> AnyMethod {
99     return ConcreteMethod(
100       name,
101       argTypes: [AnyArgumentType(A0.self), AnyArgumentType(A1.self), AnyArgumentType(A2.self), AnyArgumentType(A3.self), AnyArgumentType(A4.self)],
102       closure
103     )
104   }
105 
106   /**
107    Factory function for methods with 6 arguments.
108    */
109   public func method<R, A0: AnyMethodArgument, A1: AnyMethodArgument, A2: AnyMethodArgument, A3: AnyMethodArgument, A4: AnyMethodArgument, A5: AnyMethodArgument>(
110     _ name: String,
111     _ closure: @escaping (A0, A1, A2, A3, A4, A5) -> R
112   ) -> AnyMethod {
113     return ConcreteMethod(
114       name,
115       argTypes: [AnyArgumentType(A0.self), AnyArgumentType(A1.self), AnyArgumentType(A2.self), AnyArgumentType(A3.self), AnyArgumentType(A4.self), AnyArgumentType(A5.self)],
116       closure
117     )
118   }
119 
120   /**
121    Factory function for methods with 7 arguments.
122    */
123   public func method<R, A0: AnyMethodArgument, A1: AnyMethodArgument, A2: AnyMethodArgument, A3: AnyMethodArgument, A4: AnyMethodArgument, A5: AnyMethodArgument, A6: AnyMethodArgument>(
124     _ name: String,
125     _ closure: @escaping (A0, A1, A2, A3, A4, A5, A6) -> R
126   ) -> AnyMethod {
127     return ConcreteMethod(
128       name,
129       argTypes: [AnyArgumentType(A0.self), AnyArgumentType(A1.self), AnyArgumentType(A2.self), AnyArgumentType(A3.self), AnyArgumentType(A4.self), AnyArgumentType(A5.self), AnyArgumentType(A6.self)],
130       closure
131     )
132   }
133 
134   /**
135    Factory function for methods with 8 arguments.
136    */
137   public func method<R, A0: AnyMethodArgument, A1: AnyMethodArgument, A2: AnyMethodArgument, A3: AnyMethodArgument, A4: AnyMethodArgument, A5: AnyMethodArgument, A6: AnyMethodArgument, A7: AnyMethodArgument>(
138     _ name: String,
139     _ closure: @escaping (A0, A1, A2, A3, A4, A5, A6, A7) -> R
140   ) -> AnyMethod {
141     return ConcreteMethod(
142       name,
143       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)],
144       closure
145     )
146   }
147 
148   /**
149    Creates module's lifecycle listener that is called right after module initialization.
150    */
151   public func onCreate(_ closure: @escaping () -> Void) -> AnyDefinition {
152     return EventListener(.moduleCreate, closure)
153   }
154 
155   /**
156    Creates module's lifecycle listener that is called when the module is about to be deallocated.
157    */
158   public func onDestroy(_ closure: @escaping () -> Void) -> AnyDefinition {
159     return EventListener(.moduleDestroy, closure)
160   }
161 
162   /**
163    Creates module's lifecycle listener that is called when the app context owning the module is about to be deallocated.
164    */
165   public func onAppContextDestroys(_ closure: @escaping () -> Void) -> AnyDefinition {
166     return EventListener(.appContextDestroys, closure)
167   }
168 
169   /**
170    Creates a listener that is called when the app is about to enter the foreground mode.
171    */
172   public func onAppEntersForeground(_ closure: @escaping () -> Void) -> AnyDefinition {
173     return EventListener(.appEntersForeground, closure)
174   }
175 
176   /**
177    Creates a listener that is called when the app becomes active again.
178    */
179   public func onAppBecomesActive(_ closure: @escaping () -> Void) -> AnyDefinition {
180     return EventListener(.appBecomesActive, closure)
181   }
182 
183   /**
184    Creates a listener that is called when the app enters the background mode.
185    */
186   public func onAppEntersBackground(_ closure: @escaping () -> Void) -> AnyDefinition {
187     return EventListener(.appEntersBackground, closure)
188   }
189 }
190