expo-modules-core 0.8.0 → 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (26) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/android/build.gradle +2 -2
  3. package/android/src/main/java/expo/modules/core/interfaces/ReactNativeHostHandler.java +11 -0
  4. package/android/src/main/java/expo/modules/kotlin/functions/AsyncFunctionBuilder.kt +127 -18
  5. package/android/src/main/java/expo/modules/kotlin/modules/ModuleDefinitionBuilder.kt +210 -26
  6. package/android/src/main/java/expo/modules/kotlin/views/ViewGroupDefinitionBuilder.kt +55 -5
  7. package/android/src/main/java/expo/modules/kotlin/views/ViewManagerDefinitionBuilder.kt +43 -5
  8. package/ios/NativeModulesProxy/EXNativeModulesProxy.mm +3 -0
  9. package/ios/Swift/AppContext.swift +1 -8
  10. package/ios/Swift/Arguments/AnyArgumentType.swift +1 -1
  11. package/ios/Swift/Functions/AnyFunction.swift +5 -0
  12. package/ios/Swift/Functions/AsyncFunctionComponent.swift +182 -0
  13. package/ios/Swift/Functions/ConcreteFunction.swift +34 -67
  14. package/ios/Swift/Functions/SyncFunctionComponent.swift +181 -0
  15. package/ios/Swift/JavaScriptUtils.swift +51 -6
  16. package/ios/Swift/ModuleHolder.swift +11 -10
  17. package/ios/Swift/Modules/ModuleDefinitionComponents.swift +66 -44
  18. package/ios/Swift/Objects/ObjectDefinitionComponents.swift +45 -172
  19. package/ios/Swift/Views/ViewManagerDefinitionComponents.swift +23 -0
  20. package/ios/Tests/ConstantsSpec.swift +4 -4
  21. package/ios/Tests/ExpoModulesSpec.swift +3 -4
  22. package/ios/Tests/FunctionSpec.swift +11 -12
  23. package/ios/Tests/FunctionWithConvertiblesSpec.swift +2 -2
  24. package/ios/Tests/ModuleEventListenersSpec.swift +13 -13
  25. package/package.json +2 -2
  26. package/ios/Swift/Functions/AsyncFunction.swift +0 -17
@@ -36,17 +36,62 @@ internal func createSyncFunctionBlock(holder: ModuleHolder, name functionName: S
36
36
  }
37
37
  }
38
38
 
39
+ // MARK: - Arguments
40
+
39
41
  /**
40
- If given argument is a JavaScriptValue, it's unpacked and converted to corresponding Foundation type.
41
- Otherwise, the argument is returned as is.
42
+ Tries to cast given argument to the type that is wrapped by the argument type.
43
+ - Parameters:
44
+ - argument: A value to be cast. If it's a ``JavaScriptValue``, it's first unpacked to the raw value.
45
+ - argumentType: Something that implements ``AnyArgumentType`` and knows how to cast the argument.
46
+ - Returns: A new value converted according to the argument type.
47
+ - Throws: Rethrows various exceptions that could be thrown by the argument type wrappers.
42
48
  */
43
- internal func unpackIfJavaScriptValue(_ value: Any) -> Any {
44
- if let value = value as? JavaScriptValue {
45
- return value.getRaw() as Any
49
+ internal func castArgument(_ argument: Any, toType argumentType: AnyArgumentType) throws -> Any {
50
+ // TODO: Accept JavaScriptValue and JavaScriptObject as argument types.
51
+ if let argument = argument as? JavaScriptValue {
52
+ return try argumentType.cast(argument.getRaw())
53
+ }
54
+ return try argumentType.cast(argument)
55
+ }
56
+
57
+ /**
58
+ Same as ``castArgument(_:argumentType:)`` but for an array of arguments.
59
+ - Parameters:
60
+ - arguments: An array of arguments to be cast.
61
+ - argumentTypes: An array of argument types in the same order as the array of arguments.
62
+ - Returns: An array of arguments after casting. Its size is the same as the input arrays.
63
+ - Throws: ``InvalidArgsNumberException`` when the sizes of arrays passed as parameters are not equal.
64
+ Rethrows exceptions thrown by ``castArgument(_:argumentType:)``.
65
+ */
66
+ internal func castArguments(_ arguments: [Any], toTypes argumentTypes: [AnyArgumentType]) throws -> [Any] {
67
+ if arguments.count != argumentTypes.count {
68
+ throw InvalidArgsNumberException((received: arguments.count, expected: argumentTypes.count))
69
+ }
70
+ return try arguments.enumerated().map { index, argument in
71
+ let argumentType = argumentTypes[index]
72
+
73
+ do {
74
+ return try castArgument(argument, toType: argumentType)
75
+ } catch {
76
+ throw ArgumentCastException((index: index, type: argumentType)).causedBy(error)
77
+ }
46
78
  }
47
- return value
48
79
  }
49
80
 
81
+ internal class InvalidArgsNumberException: GenericException<(received: Int, expected: Int)> {
82
+ override var reason: String {
83
+ "Received \(param.received) arguments, but \(param.expected) was expected"
84
+ }
85
+ }
86
+
87
+ internal class ArgumentCastException: GenericException<(index: Int, type: AnyArgumentType)> {
88
+ override var reason: String {
89
+ "Argument at index '\(param.index)' couldn't be cast to type \(param.type.description)"
90
+ }
91
+ }
92
+
93
+ // MARK: - Exceptions
94
+
50
95
  private class ModuleUnavailableException: GenericException<String> {
51
96
  override var reason: String {
52
97
  "Module '\(param)' is no longer available"
@@ -74,12 +74,11 @@ public final class ModuleHolder {
74
74
  // - Swift primitives when invoked through the bridge and in unit tests
75
75
  // - `JavaScriptValue`s when the function is called through the JSI
76
76
  // The latter need to be unpacked to Swift primitives on the JS thread,
77
- // so before the function call is scheduled on the queue.
78
- // TODO: Move arguments conversion mechanism to JS thread and allow JS types as function arguments.
79
- let unpackedArgs = args.map { arg in unpackIfJavaScriptValue(arg) }
77
+ // so we do the casting before the function call is scheduled on the queue.
78
+ let arguments = try castArguments(args, toTypes: function.argumentTypes)
80
79
 
81
80
  queue.async {
82
- function.call(args: unpackedArgs, promise: promise)
81
+ function.call(args: arguments, promise: promise)
83
82
  }
84
83
  } catch let error as CodedError {
85
84
  promise.reject(error)
@@ -99,13 +98,15 @@ public final class ModuleHolder {
99
98
 
100
99
  @discardableResult
101
100
  func callSync(function functionName: String, args: [Any]) -> Any? {
102
- if let function = definition.functions[functionName] {
103
- // The comment in `call(function:args:promise)` is partially applicable here as well.
104
- // TODO: Move unpacking JS values to `callSync` in function's instance
105
- let unpackedArgs = args.map { arg in unpackIfJavaScriptValue(arg) }
106
- return function.callSync(args: unpackedArgs)
101
+ guard let function = definition.functions[functionName] else {
102
+ return nil
103
+ }
104
+ do {
105
+ let arguments = try castArguments(args, toTypes: function.argumentTypes)
106
+ return function.callSync(args: arguments)
107
+ } catch {
108
+ return error
107
109
  }
108
- return nil
109
110
  }
110
111
 
111
112
  // MARK: JavaScript Module Object
@@ -12,6 +12,7 @@ extension AnyModule {
12
12
  /**
13
13
  Sets the name of the module that is exported to the JavaScript world.
14
14
  */
15
+ @available(*, deprecated, renamed: "Name")
15
16
  public func name(_ name: String) -> AnyDefinition {
16
17
  return ModuleNameDefinition(name: name)
17
18
  }
@@ -21,6 +22,7 @@ extension AnyModule {
21
22
  /**
22
23
  Creates module's lifecycle listener that is called right after module initialization.
23
24
  */
25
+ @available(*, deprecated, renamed: "OnCreate")
24
26
  public func onCreate(_ closure: @escaping () -> Void) -> AnyDefinition {
25
27
  return EventListener(.moduleCreate, closure)
26
28
  }
@@ -28,6 +30,7 @@ extension AnyModule {
28
30
  /**
29
31
  Creates module's lifecycle listener that is called when the module is about to be deallocated.
30
32
  */
33
+ @available(*, deprecated, renamed: "OnDestroy")
31
34
  public func onDestroy(_ closure: @escaping () -> Void) -> AnyDefinition {
32
35
  return EventListener(.moduleDestroy, closure)
33
36
  }
@@ -35,6 +38,7 @@ extension AnyModule {
35
38
  /**
36
39
  Creates module's lifecycle listener that is called when the app context owning the module is about to be deallocated.
37
40
  */
41
+ @available(*, deprecated, renamed: "OnAppContextDestroys")
38
42
  public func onAppContextDestroys(_ closure: @escaping () -> Void) -> AnyDefinition {
39
43
  return EventListener(.appContextDestroys, closure)
40
44
  }
@@ -42,6 +46,7 @@ extension AnyModule {
42
46
  /**
43
47
  Creates a listener that is called when the app is about to enter the foreground mode.
44
48
  */
49
+ @available(*, deprecated, renamed: "OnAppEntersBackground")
45
50
  public func onAppEntersForeground(_ closure: @escaping () -> Void) -> AnyDefinition {
46
51
  return EventListener(.appEntersForeground, closure)
47
52
  }
@@ -49,6 +54,7 @@ extension AnyModule {
49
54
  /**
50
55
  Creates a listener that is called when the app becomes active again.
51
56
  */
57
+ @available(*, deprecated, renamed: "OnAppBecomesActive")
52
58
  public func onAppBecomesActive(_ closure: @escaping () -> Void) -> AnyDefinition {
53
59
  return EventListener(.appBecomesActive, closure)
54
60
  }
@@ -56,6 +62,7 @@ extension AnyModule {
56
62
  /**
57
63
  Creates a listener that is called when the app enters the background mode.
58
64
  */
65
+ @available(*, deprecated, renamed: "OnAppEntersBackground")
59
66
  public func onAppEntersBackground(_ closure: @escaping () -> Void) -> AnyDefinition {
60
67
  return EventListener(.appEntersBackground, closure)
61
68
  }
@@ -65,55 +72,70 @@ extension AnyModule {
65
72
  /**
66
73
  Creates the view manager definition that scopes other view-related definitions.
67
74
  */
75
+ @available(*, deprecated, renamed: "ViewManager")
68
76
  public func viewManager(@ViewManagerDefinitionBuilder _ closure: @escaping () -> ViewManagerDefinition) -> AnyDefinition {
69
77
  return closure()
70
78
  }
71
79
  }
72
80
 
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
- }
81
+ // MARK: - Module name
89
82
 
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
- }
83
+ /**
84
+ Sets the name of the module that is exported to the JavaScript world.
85
+ */
86
+ public func Name(_ name: String) -> AnyDefinition {
87
+ return ModuleNameDefinition(name: name)
88
+ }
104
89
 
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
- }
90
+ // MARK: - Module's lifecycle
91
+
92
+ /**
93
+ Creates module's lifecycle listener that is called right after module initialization.
94
+ */
95
+ public func OnCreate(_ closure: @escaping () -> Void) -> AnyDefinition {
96
+ return EventListener(.moduleCreate, closure)
97
+ }
98
+
99
+ /**
100
+ Creates module's lifecycle listener that is called when the module is about to be deallocated.
101
+ */
102
+ public func OnDestroy(_ closure: @escaping () -> Void) -> AnyDefinition {
103
+ return EventListener(.moduleDestroy, closure)
104
+ }
105
+
106
+ /**
107
+ Creates module's lifecycle listener that is called when the app context owning the module is about to be deallocated.
108
+ */
109
+ public func OnAppContextDestroys(_ closure: @escaping () -> Void) -> AnyDefinition {
110
+ return EventListener(.appContextDestroys, closure)
111
+ }
112
+
113
+ /**
114
+ Creates a listener that is called when the app is about to enter the foreground mode.
115
+ */
116
+ public func OnAppEntersForeground(_ closure: @escaping () -> Void) -> AnyDefinition {
117
+ return EventListener(.appEntersForeground, closure)
118
+ }
119
+
120
+ /**
121
+ Creates a listener that is called when the app becomes active again.
122
+ */
123
+ public func OnAppBecomesActive(_ closure: @escaping () -> Void) -> AnyDefinition {
124
+ return EventListener(.appBecomesActive, closure)
125
+ }
126
+
127
+ /**
128
+ Creates a listener that is called when the app enters the background mode.
129
+ */
130
+ public func OnAppEntersBackground(_ closure: @escaping () -> Void) -> AnyDefinition {
131
+ return EventListener(.appEntersBackground, closure)
132
+ }
133
+
134
+ // MARK: - View Manager
135
+
136
+ /**
137
+ Creates the view manager definition that scopes other view-related definitions.
138
+ */
139
+ public func ViewManager(@ViewManagerDefinitionBuilder _ closure: @escaping () -> ViewManagerDefinition) -> AnyDefinition {
140
+ return closure()
119
141
  }
@@ -6,6 +6,7 @@
6
6
  /**
7
7
  Definition function setting the module's constants to export.
8
8
  */
9
+ @available(*, deprecated, renamed: "Constants")
9
10
  public func constants(_ body: @escaping () -> [String: Any?]) -> AnyDefinition {
10
11
  return ConstantsDefinition(body: body)
11
12
  }
@@ -13,16 +14,31 @@ public func constants(_ body: @escaping () -> [String: Any?]) -> AnyDefinition {
13
14
  /**
14
15
  Definition function setting the module's constants to export.
15
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")
16
25
  public func constants(_ body: @autoclosure @escaping () -> [String: Any?]) -> AnyDefinition {
17
26
  return ConstantsDefinition(body: body)
18
27
  }
19
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
+
20
36
  // MARK: - Functions
21
37
 
22
38
  /**
23
39
  Function without arguments.
24
40
  */
25
- @available(*, deprecated, renamed: "asyncFunction")
41
+ @available(*, deprecated, renamed: "AsyncFunction")
26
42
  public func function<R>(
27
43
  _ name: String,
28
44
  _ closure: @escaping () throws -> R
@@ -37,7 +53,7 @@ public func function<R>(
37
53
  /**
38
54
  Function with one argument.
39
55
  */
40
- @available(*, deprecated, renamed: "asyncFunction")
56
+ @available(*, deprecated, renamed: "AsyncFunction")
41
57
  public func function<R, A0: AnyArgument>(
42
58
  _ name: String,
43
59
  _ closure: @escaping (A0) throws -> R
@@ -52,7 +68,7 @@ public func function<R, A0: AnyArgument>(
52
68
  /**
53
69
  Function with two arguments.
54
70
  */
55
- @available(*, deprecated, renamed: "asyncFunction")
71
+ @available(*, deprecated, renamed: "AsyncFunction")
56
72
  public func function<R, A0: AnyArgument, A1: AnyArgument>(
57
73
  _ name: String,
58
74
  _ closure: @escaping (A0, A1) throws -> R
@@ -67,7 +83,7 @@ public func function<R, A0: AnyArgument, A1: AnyArgument>(
67
83
  /**
68
84
  Function with three arguments.
69
85
  */
70
- @available(*, deprecated, renamed: "asyncFunction")
86
+ @available(*, deprecated, renamed: "AsyncFunction")
71
87
  public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument>(
72
88
  _ name: String,
73
89
  _ closure: @escaping (A0, A1, A2) throws -> R
@@ -86,7 +102,7 @@ public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument>(
86
102
  /**
87
103
  Function with four arguments.
88
104
  */
89
- @available(*, deprecated, renamed: "asyncFunction")
105
+ @available(*, deprecated, renamed: "AsyncFunction")
90
106
  public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument>(
91
107
  _ name: String,
92
108
  _ closure: @escaping (A0, A1, A2, A3) throws -> R
@@ -106,7 +122,7 @@ public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: A
106
122
  /**
107
123
  Function with five arguments.
108
124
  */
109
- @available(*, deprecated, renamed: "asyncFunction")
125
+ @available(*, deprecated, renamed: "AsyncFunction")
110
126
  public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument>(
111
127
  _ name: String,
112
128
  _ closure: @escaping (A0, A1, A2, A3, A4) throws -> R
@@ -127,7 +143,7 @@ public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: A
127
143
  /**
128
144
  Function with six arguments.
129
145
  */
130
- @available(*, deprecated, renamed: "asyncFunction")
146
+ @available(*, deprecated, renamed: "AsyncFunction")
131
147
  public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument, A5: AnyArgument>(
132
148
  _ name: String,
133
149
  _ closure: @escaping (A0, A1, A2, A3, A4, A5) throws -> R
@@ -149,7 +165,7 @@ public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: A
149
165
  /**
150
166
  Function with seven arguments.
151
167
  */
152
- @available(*, deprecated, renamed: "asyncFunction")
168
+ @available(*, deprecated, renamed: "AsyncFunction")
153
169
  public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument, A5: AnyArgument, A6: AnyArgument>(
154
170
  _ name: String,
155
171
  _ closure: @escaping (A0, A1, A2, A3, A4, A5, A6) throws -> R
@@ -172,7 +188,7 @@ public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: A
172
188
  /**
173
189
  Function with eight arguments.
174
190
  */
175
- @available(*, deprecated, renamed: "asyncFunction")
191
+ @available(*, deprecated, renamed: "AsyncFunction")
176
192
  public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument, A5: AnyArgument, A6: AnyArgument, A7: AnyArgument>(
177
193
  _ name: String,
178
194
  _ closure: @escaping (A0, A1, A2, A3, A4, A5, A6, A7) throws -> R
@@ -193,192 +209,49 @@ public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: A
193
209
  )
194
210
  }
195
211
 
196
- // MARK: - Asynchronous functions
197
-
198
- /**
199
- Asynchronous function without arguments.
200
- */
201
- public func asyncFunction<R>(
202
- _ name: String,
203
- _ closure: @escaping () throws -> R
204
- ) -> AnyFunction {
205
- return AsyncFunction(
206
- name,
207
- argTypes: [],
208
- closure
209
- )
210
- }
211
-
212
- /**
213
- Asynchronous function with one argument.
214
- */
215
- public func asyncFunction<R, A0: AnyArgument>(
216
- _ name: String,
217
- _ closure: @escaping (A0) throws -> R
218
- ) -> AnyFunction {
219
- return AsyncFunction(
220
- name,
221
- argTypes: [ArgumentType(A0.self)],
222
- closure
223
- )
224
- }
225
-
226
- /**
227
- Asynchronous function with two arguments.
228
- */
229
- public func asyncFunction<R, A0: AnyArgument, A1: AnyArgument>(
230
- _ name: String,
231
- _ closure: @escaping (A0, A1) throws -> R
232
- ) -> AnyFunction {
233
- return AsyncFunction(
234
- name,
235
- argTypes: [ArgumentType(A0.self), ArgumentType(A1.self)],
236
- closure
237
- )
238
- }
239
-
240
- /**
241
- Asynchronous function with three arguments.
242
- */
243
- public func asyncFunction<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument>(
244
- _ name: String,
245
- _ closure: @escaping (A0, A1, A2) throws -> R
246
- ) -> AnyFunction {
247
- return AsyncFunction(
248
- name,
249
- argTypes: [
250
- ArgumentType(A0.self),
251
- ArgumentType(A1.self),
252
- ArgumentType(A2.self)
253
- ],
254
- closure
255
- )
256
- }
257
-
258
- /**
259
- Asynchronous function with four arguments.
260
- */
261
- public func asyncFunction<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument>(
262
- _ name: String,
263
- _ closure: @escaping (A0, A1, A2, A3) throws -> R
264
- ) -> AnyFunction {
265
- return AsyncFunction(
266
- name,
267
- argTypes: [
268
- ArgumentType(A0.self),
269
- ArgumentType(A1.self),
270
- ArgumentType(A2.self),
271
- ArgumentType(A3.self)
272
- ],
273
- closure
274
- )
275
- }
276
-
277
- /**
278
- Asynchronous function with five arguments.
279
- */
280
- public func asyncFunction<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument>(
281
- _ name: String,
282
- _ closure: @escaping (A0, A1, A2, A3, A4) throws -> R
283
- ) -> AnyFunction {
284
- return AsyncFunction(
285
- name,
286
- argTypes: [
287
- ArgumentType(A0.self),
288
- ArgumentType(A1.self),
289
- ArgumentType(A2.self),
290
- ArgumentType(A3.self),
291
- ArgumentType(A4.self)
292
- ],
293
- closure
294
- )
295
- }
212
+ // MARK: - Events
296
213
 
297
214
  /**
298
- Asynchronous function with six arguments.
215
+ Defines event names that the object can send to JavaScript.
299
216
  */
300
- public func asyncFunction<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument, A5: AnyArgument>(
301
- _ name: String,
302
- _ closure: @escaping (A0, A1, A2, A3, A4, A5) throws -> R
303
- ) -> AnyFunction {
304
- return AsyncFunction(
305
- name,
306
- argTypes: [
307
- ArgumentType(A0.self),
308
- ArgumentType(A1.self),
309
- ArgumentType(A2.self),
310
- ArgumentType(A3.self),
311
- ArgumentType(A4.self),
312
- ArgumentType(A5.self)
313
- ],
314
- closure
315
- )
217
+ @available(*, deprecated, renamed: "Events")
218
+ public func events(_ names: String...) -> AnyDefinition {
219
+ return EventsDefinition(names: names)
316
220
  }
317
221
 
318
222
  /**
319
- Asynchronous function with seven arguments.
223
+ Defines event names that the object can send to JavaScript.
320
224
  */
321
- public func asyncFunction<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument, A5: AnyArgument, A6: AnyArgument>(
322
- _ name: String,
323
- _ closure: @escaping (A0, A1, A2, A3, A4, A5, A6) throws -> R
324
- ) -> AnyFunction {
325
- return AsyncFunction(
326
- name,
327
- argTypes: [
328
- ArgumentType(A0.self),
329
- ArgumentType(A1.self),
330
- ArgumentType(A2.self),
331
- ArgumentType(A3.self),
332
- ArgumentType(A4.self),
333
- ArgumentType(A5.self),
334
- ArgumentType(A6.self)
335
- ],
336
- closure
337
- )
225
+ public func Events(_ names: String...) -> AnyDefinition {
226
+ return EventsDefinition(names: names)
338
227
  }
339
228
 
340
229
  /**
341
- Asynchronous function with eight arguments.
230
+ Function that is invoked when the first event listener is added.
342
231
  */
343
- public func asyncFunction<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument, A5: AnyArgument, A6: AnyArgument, A7: AnyArgument>(
344
- _ name: String,
345
- _ closure: @escaping (A0, A1, A2, A3, A4, A5, A6, A7) throws -> R
346
- ) -> AnyFunction {
347
- return AsyncFunction(
348
- name,
349
- argTypes: [
350
- ArgumentType(A0.self),
351
- ArgumentType(A1.self),
352
- ArgumentType(A2.self),
353
- ArgumentType(A3.self),
354
- ArgumentType(A4.self),
355
- ArgumentType(A5.self),
356
- ArgumentType(A6.self),
357
- ArgumentType(A7.self)
358
- ],
359
- closure
360
- )
232
+ @available(*, deprecated, renamed: "OnStartObserving")
233
+ public func onStartObserving(_ body: @escaping () -> Void) -> AnyFunction {
234
+ return ConcreteFunction("startObserving", argTypes: [], body)
361
235
  }
362
236
 
363
- // MARK: - Events
364
-
365
237
  /**
366
- Defines event names that the object can send to JavaScript.
238
+ Function that is invoked when the first event listener is added.
367
239
  */
368
- public func events(_ names: String...) -> AnyDefinition {
369
- return EventsDefinition(names: names)
240
+ public func OnStartObserving(_ body: @escaping () -> Void) -> AnyFunction {
241
+ return ConcreteFunction("startObserving", argTypes: [], body)
370
242
  }
371
243
 
372
244
  /**
373
- Function that is invoked when the first event listener is added.
245
+ Function that is invoked when all event listeners are removed.
374
246
  */
375
- public func onStartObserving(_ body: @escaping () -> Void) -> AnyFunction {
376
- return ConcreteFunction("startObserving", argTypes: [], body)
247
+ @available(*, deprecated, renamed: "OnStopObserving")
248
+ public func onStopObserving(_ body: @escaping () -> Void) -> AnyFunction {
249
+ return ConcreteFunction("stopObserving", argTypes: [], body)
377
250
  }
378
251
 
379
252
  /**
380
253
  Function that is invoked when all event listeners are removed.
381
254
  */
382
- public func onStopObserving(_ body: @escaping () -> Void) -> AnyFunction {
255
+ public func OnStopObserving(_ body: @escaping () -> Void) -> AnyFunction {
383
256
  return ConcreteFunction("stopObserving", argTypes: [], body)
384
257
  }
@@ -5,15 +5,24 @@
5
5
  /**
6
6
  Defines the factory creating a native view when the module is used as a view.
7
7
  */
8
+ @available(*, deprecated, renamed: "View")
8
9
  public func view(_ closure: @escaping () -> UIView) -> ViewManagerDefinitionComponent {
9
10
  return ViewFactory(closure)
10
11
  }
11
12
 
13
+ /**
14
+ Defines the factory creating a native view when the module is used as a view.
15
+ */
16
+ public func View(_ closure: @escaping () -> UIView) -> ViewManagerDefinitionComponent {
17
+ return ViewFactory(closure)
18
+ }
19
+
12
20
  // MARK: Props
13
21
 
14
22
  /**
15
23
  Creates a view prop that defines its name and setter.
16
24
  */
25
+ @available(*, deprecated, renamed: "Prop")
17
26
  public func prop<ViewType: UIView, PropType: AnyArgument>(
18
27
  _ name: String,
19
28
  _ setter: @escaping (ViewType, PropType) -> Void
@@ -24,3 +33,17 @@ public func prop<ViewType: UIView, PropType: AnyArgument>(
24
33
  setter: setter
25
34
  )
26
35
  }
36
+
37
+ /**
38
+ Creates a view prop that defines its name and setter.
39
+ */
40
+ public func Prop<ViewType: UIView, PropType: AnyArgument>(
41
+ _ name: String,
42
+ _ setter: @escaping (ViewType, PropType) -> Void
43
+ ) -> ViewManagerDefinitionComponent {
44
+ return ConcreteViewProp(
45
+ name: name,
46
+ propType: ArgumentType(PropType.self),
47
+ setter: setter
48
+ )
49
+ }
@@ -8,7 +8,7 @@ class ConstantsSpec: ExpoSpec {
8
8
 
9
9
  it("takes closure resolving to dictionary") {
10
10
  let holder = mockModuleHolder(appContext) {
11
- constants {
11
+ Constants {
12
12
  return ["test": 123]
13
13
  }
14
14
  }
@@ -17,15 +17,15 @@ class ConstantsSpec: ExpoSpec {
17
17
 
18
18
  it("takes the dictionary") {
19
19
  let holder = mockModuleHolder(appContext) {
20
- constants(["test": 123])
20
+ Constants(["test": 123])
21
21
  }
22
22
  expect(holder.getConstants()["test"] as? Int) == 123
23
23
  }
24
24
 
25
25
  it("merges multiple constants definitions") {
26
26
  let holder = mockModuleHolder(appContext) {
27
- constants(["test": 456, "test2": 789])
28
- constants(["test": 123])
27
+ Constants(["test": 456, "test2": 789])
28
+ Constants(["test": 123])
29
29
  }
30
30
  let consts = holder.getConstants()
31
31
  expect(consts["test"] as? Int) == 123
@@ -20,12 +20,11 @@ class ExpoModulesSpec: ExpoSpec {
20
20
  try! appContext.installExpoModulesHostObject(interopBridge)
21
21
 
22
22
  appContext.moduleRegistry.register(holder: mockModuleHolder(appContext) {
23
- $0.name(testModuleName)
23
+ Name(testModuleName)
24
24
 
25
- constants(constantsDict)
25
+ Constants(constantsDict)
26
26
 
27
- function(testFunctionName) { Double.pi }
28
- .runSynchronously()
27
+ Function(testFunctionName) { Double.pi }
29
28
  })
30
29
  }
31
30