1 /// This file implements definition components that are allowed in any object-based definition — `ObjectDefinition`.
2 /// So far only constants and functions belong to plain object.
3 
4 // MARK: - Constants
5 
6 /**
7  Definition function setting the module's constants to export.
8  */
9 @available(*, deprecated, renamed: "Constants")
10 public func constants(_ body: @escaping () -> [String: Any?]) -> AnyDefinition {
11   return ConstantsDefinition(body: body)
12 }
13 
14 /**
15  Definition function setting the module's constants to export.
16  */
17 public func Constants(_ body: @escaping () -> [String: Any?]) -> AnyDefinition {
18   return ConstantsDefinition(body: body)
19 }
20 
21 /**
22  Definition function setting the module's constants to export.
23  */
24 @available(*, deprecated, renamed: "Constants")
25 public func constants(_ body: @autoclosure @escaping () -> [String: Any?]) -> AnyDefinition {
26   return ConstantsDefinition(body: body)
27 }
28 
29 /**
30  Definition function setting the module's constants to export.
31  */
32 public func Constants(_ body: @autoclosure @escaping () -> [String: Any?]) -> AnyDefinition {
33   return ConstantsDefinition(body: body)
34 }
35 
36 // MARK: - Functions
37 
38 /**
39  Function without arguments.
40  */
41 @available(*, deprecated, renamed: "AsyncFunction")
42 public func function<R>(
43   _ name: String,
44   _ closure: @escaping () throws -> R
45 ) -> AsyncFunctionComponent<(), Void, R> {
46   return AsyncFunctionComponent(
47     name,
48     firstArgType: Void.self,
49     dynamicArgumentTypes: [],
50     closure
51   )
52 }
53 
54 /**
55  Function with one argument.
56  */
57 @available(*, deprecated, renamed: "AsyncFunction")
58 public func function<R, A0: AnyArgument>(
59   _ name: String,
60   _ closure: @escaping (A0) throws -> R
61 ) -> AsyncFunctionComponent<(A0), A0, R> {
62   return AsyncFunctionComponent(
63     name,
64     firstArgType: A0.self,
65     dynamicArgumentTypes: [~A0.self],
66     closure
67   )
68 }
69 
70 /**
71  Function with two arguments.
72  */
73 @available(*, deprecated, renamed: "AsyncFunction")
74 public func function<R, A0: AnyArgument, A1: AnyArgument>(
75   _ name: String,
76   _ closure: @escaping (A0, A1) throws -> R
77 ) -> AsyncFunctionComponent<(A0, A1), A0, R> {
78   return AsyncFunctionComponent(
79     name,
80     firstArgType: A0.self,
81     dynamicArgumentTypes: [~A0.self, ~A1.self],
82     closure
83   )
84 }
85 
86 /**
87  Function with three arguments.
88  */
89 @available(*, deprecated, renamed: "AsyncFunction")
90 public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument>(
91   _ name: String,
92   _ closure: @escaping (A0, A1, A2) throws -> R
93 ) -> AsyncFunctionComponent<(A0, A1, A2), A0, R> {
94   return AsyncFunctionComponent(
95     name,
96     firstArgType: A0.self,
97     dynamicArgumentTypes: [
98       ~A0.self,
99       ~A1.self,
100       ~A2.self
101     ],
102     closure
103   )
104 }
105 
106 /**
107  Function with four arguments.
108  */
109 @available(*, deprecated, renamed: "AsyncFunction")
110 public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument>(
111   _ name: String,
112   _ closure: @escaping (A0, A1, A2, A3) throws -> R
113 ) -> AsyncFunctionComponent<(A0, A1, A2, A3), A0, R> {
114   return AsyncFunctionComponent(
115     name,
116     firstArgType: A0.self,
117     dynamicArgumentTypes: [
118       ~A0.self,
119       ~A1.self,
120       ~A2.self,
121       ~A3.self
122     ],
123     closure
124   )
125 }
126 
127 /**
128  Function with five arguments.
129  */
130 @available(*, deprecated, renamed: "AsyncFunction")
131 public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument>(
132   _ name: String,
133   _ closure: @escaping (A0, A1, A2, A3, A4) throws -> R
134 ) -> AsyncFunctionComponent<(A0, A1, A2, A3, A4), A0, R> {
135   return AsyncFunctionComponent(
136     name,
137     firstArgType: A0.self,
138     dynamicArgumentTypes: [
139       ~A0.self,
140       ~A1.self,
141       ~A2.self,
142       ~A3.self,
143       ~A4.self
144     ],
145     closure
146   )
147 }
148 
149 /**
150  Function with six arguments.
151  */
152 @available(*, deprecated, renamed: "AsyncFunction")
153 public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument, A5: AnyArgument>(
154   _ name: String,
155   _ closure: @escaping (A0, A1, A2, A3, A4, A5) throws -> R
156 ) -> AsyncFunctionComponent<(A0, A1, A2, A3, A4, A5), A0, R> {
157   return AsyncFunctionComponent(
158     name,
159     firstArgType: A0.self,
160     dynamicArgumentTypes: [
161       ~A0.self,
162       ~A1.self,
163       ~A2.self,
164       ~A3.self,
165       ~A4.self,
166       ~A5.self
167     ],
168     closure
169   )
170 }
171 
172 /**
173  Function with seven arguments.
174  */
175 @available(*, deprecated, renamed: "AsyncFunction")
176 public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument, A5: AnyArgument, A6: AnyArgument>(
177   _ name: String,
178   _ closure: @escaping (A0, A1, A2, A3, A4, A5, A6) throws -> R
179 ) -> AsyncFunctionComponent<(A0, A1, A2, A3, A4, A5, A6), A0, R> {
180   return AsyncFunctionComponent(
181     name,
182     firstArgType: A0.self,
183     dynamicArgumentTypes: [
184       ~A0.self,
185       ~A1.self,
186       ~A2.self,
187       ~A3.self,
188       ~A4.self,
189       ~A5.self,
190       ~A6.self
191     ],
192     closure
193   )
194 }
195 
196 /**
197  Function with eight arguments.
198  */
199 @available(*, deprecated, renamed: "AsyncFunction")
200 public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument, A5: AnyArgument, A6: AnyArgument, A7: AnyArgument>(
201   _ name: String,
202   _ closure: @escaping (A0, A1, A2, A3, A4, A5, A6, A7) throws -> R
203 ) -> AsyncFunctionComponent<(A0, A1, A2, A3, A4, A5, A6, A7), A0, R> {
204   return AsyncFunctionComponent(
205     name,
206     firstArgType: A0.self,
207     dynamicArgumentTypes: [
208       ~A0.self,
209       ~A1.self,
210       ~A2.self,
211       ~A3.self,
212       ~A4.self,
213       ~A5.self,
214       ~A6.self,
215       ~A7.self
216     ],
217     closure
218   )
219 }
220 
221 // MARK: - Events
222 
223 /**
224  Defines event names that the object can send to JavaScript.
225  */
226 @available(*, deprecated, renamed: "Events")
227 public func events(_ names: String...) -> AnyDefinition {
228   return EventsDefinition(names: names)
229 }
230 
231 /**
232  Defines event names that the object can send to JavaScript.
233  */
234 public func Events(_ names: String...) -> AnyDefinition {
235   return EventsDefinition(names: names)
236 }
237 
238 /**
239  Function that is invoked when the first event listener is added.
240  */
241 @available(*, deprecated, renamed: "OnStartObserving")
242 public func onStartObserving(_ body: @escaping () -> Void) -> AsyncFunctionComponent<(), Void, Void> {
243   return AsyncFunctionComponent("startObserving", firstArgType: Void.self, dynamicArgumentTypes: [], body)
244 }
245 
246 /**
247  Function that is invoked when the first event listener is added.
248  */
249 public func OnStartObserving(_ body: @escaping () -> Void) -> AsyncFunctionComponent<(), Void, Void> {
250   return AsyncFunctionComponent("startObserving", firstArgType: Void.self, dynamicArgumentTypes: [], body)
251 }
252 
253 /**
254  Function that is invoked when all event listeners are removed.
255  */
256 @available(*, deprecated, renamed: "OnStopObserving")
257 public func onStopObserving(_ body: @escaping () -> Void) -> AsyncFunctionComponent<(), Void, Void> {
258   return AsyncFunctionComponent("stopObserving", firstArgType: Void.self, dynamicArgumentTypes: [], body)
259 }
260 
261 /**
262  Function that is invoked when all event listeners are removed.
263  */
264 public func OnStopObserving(_ body: @escaping () -> Void) -> AsyncFunctionComponent<(), Void, Void> {
265   return AsyncFunctionComponent("stopObserving", firstArgType: Void.self, dynamicArgumentTypes: [], body)
266 }
267