expo-modules-core 0.6.1 → 0.7.0

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 (161) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/README.md +1 -1
  3. package/android/build.gradle +5 -5
  4. package/android/src/main/java/expo/modules/adapters/react/apploader/RNHeadlessAppLoader.kt +7 -1
  5. package/android/src/main/java/expo/modules/core/interfaces/ReactActivityLifecycleListener.java +23 -0
  6. package/android/src/main/java/expo/modules/kotlin/AppContext.kt +11 -1
  7. package/android/src/main/java/expo/modules/kotlin/DynamicExtenstions.kt +5 -3
  8. package/android/src/main/java/expo/modules/kotlin/KPromiseWrapper.kt +8 -2
  9. package/android/src/main/java/expo/modules/kotlin/KotlinInteropModuleRegistry.kt +13 -4
  10. package/android/src/main/java/expo/modules/kotlin/ModuleHolder.kt +7 -6
  11. package/android/src/main/java/expo/modules/kotlin/ModuleRegistry.kt +6 -1
  12. package/android/src/main/java/expo/modules/kotlin/Promise.kt +1 -1
  13. package/android/src/main/java/expo/modules/kotlin/callbacks/Callback.kt +5 -0
  14. package/android/src/main/java/expo/modules/kotlin/callbacks/ViewCallback.kt +28 -0
  15. package/android/src/main/java/expo/modules/kotlin/callbacks/ViewCallbackDelegate.kt +27 -0
  16. package/android/src/main/java/expo/modules/kotlin/defaultmodules/ErrorManagerModule.kt +25 -0
  17. package/android/src/main/java/expo/modules/kotlin/exception/CodedException.kt +68 -8
  18. package/android/src/main/java/expo/modules/kotlin/exception/ExceptionDecorator.kt +11 -0
  19. package/android/src/main/java/expo/modules/kotlin/methods/AnyMethod.kt +13 -14
  20. package/android/src/main/java/expo/modules/kotlin/modules/DefinitionMarker.kt +4 -0
  21. package/android/src/main/java/expo/modules/kotlin/modules/Module.kt +3 -2
  22. package/android/src/main/java/expo/modules/kotlin/modules/ModuleDefinitionBuilder.kt +44 -4
  23. package/android/src/main/java/expo/modules/kotlin/records/Record.kt +39 -0
  24. package/android/src/main/java/expo/modules/kotlin/records/RecordTypeConverter.kt +14 -7
  25. package/android/src/main/java/expo/modules/kotlin/types/ArrayTypeConverter.kt +11 -5
  26. package/android/src/main/java/expo/modules/kotlin/types/ListTypeConverter.kt +10 -4
  27. package/android/src/main/java/expo/modules/kotlin/types/MapTypeConverter.kt +12 -6
  28. package/android/src/main/java/expo/modules/kotlin/types/PairTypeConverter.kt +29 -13
  29. package/android/src/main/java/expo/modules/kotlin/types/TypeConverter.kt +2 -1
  30. package/android/src/main/java/expo/modules/kotlin/views/CallbacksDefinition.kt +3 -0
  31. package/android/src/main/java/expo/modules/kotlin/views/GroupViewManagerWrapper.kt +22 -0
  32. package/android/src/main/java/expo/modules/kotlin/views/SimpleViewManagerWrapper.kt +22 -0
  33. package/android/src/main/java/expo/modules/kotlin/views/ViewManagerDefinition.kt +27 -2
  34. package/android/src/main/java/expo/modules/kotlin/views/ViewManagerDefinitionBuilder.kt +29 -1
  35. package/android/src/main/java/expo/modules/kotlin/views/ViewManagerWrapperDelegate.kt +59 -2
  36. package/build/EventEmitter.d.ts +1 -0
  37. package/build/EventEmitter.d.ts.map +1 -0
  38. package/build/NativeModulesProxy.d.ts +1 -0
  39. package/build/NativeModulesProxy.d.ts.map +1 -0
  40. package/build/NativeModulesProxy.native.d.ts +1 -0
  41. package/build/NativeModulesProxy.native.d.ts.map +1 -0
  42. package/build/NativeModulesProxy.types.d.ts +1 -0
  43. package/build/NativeModulesProxy.types.d.ts.map +1 -0
  44. package/build/NativeViewManagerAdapter.d.ts +1 -0
  45. package/build/NativeViewManagerAdapter.d.ts.map +1 -0
  46. package/build/NativeViewManagerAdapter.native.d.ts +1 -0
  47. package/build/NativeViewManagerAdapter.native.d.ts.map +1 -0
  48. package/build/PermissionsHook.d.ts +1 -0
  49. package/build/PermissionsHook.d.ts.map +1 -0
  50. package/build/PermissionsInterface.d.ts +1 -0
  51. package/build/PermissionsInterface.d.ts.map +1 -0
  52. package/build/Platform.d.ts +1 -0
  53. package/build/Platform.d.ts.map +1 -0
  54. package/build/SyntheticPlatformEmitter.d.ts +1 -0
  55. package/build/SyntheticPlatformEmitter.d.ts.map +1 -0
  56. package/build/SyntheticPlatformEmitter.web.d.ts +1 -0
  57. package/build/SyntheticPlatformEmitter.web.d.ts.map +1 -0
  58. package/build/deprecate.d.ts +1 -0
  59. package/build/deprecate.d.ts.map +1 -0
  60. package/build/environment/browser.d.ts +1 -0
  61. package/build/environment/browser.d.ts.map +1 -0
  62. package/build/environment/browser.web.d.ts +1 -0
  63. package/build/environment/browser.web.d.ts.map +1 -0
  64. package/build/errors/CodedError.d.ts +1 -0
  65. package/build/errors/CodedError.d.ts.map +1 -0
  66. package/build/errors/UnavailabilityError.d.ts +1 -0
  67. package/build/errors/UnavailabilityError.d.ts.map +1 -0
  68. package/build/index.d.ts +3 -0
  69. package/build/index.d.ts.map +1 -0
  70. package/build/index.js +2 -0
  71. package/build/index.js.map +1 -1
  72. package/build/requireNativeModule.d.ts +16 -0
  73. package/build/requireNativeModule.d.ts.map +1 -0
  74. package/build/requireNativeModule.js +18 -0
  75. package/build/requireNativeModule.js.map +1 -0
  76. package/build/sweet/NativeErrorManager.d.ts +3 -0
  77. package/build/sweet/NativeErrorManager.d.ts.map +1 -0
  78. package/build/sweet/NativeErrorManager.js +3 -0
  79. package/build/sweet/NativeErrorManager.js.map +1 -0
  80. package/build/sweet/setUpErrorManager.fx.d.ts +2 -0
  81. package/build/sweet/setUpErrorManager.fx.d.ts.map +1 -0
  82. package/build/sweet/setUpErrorManager.fx.js +11 -0
  83. package/build/sweet/setUpErrorManager.fx.js.map +1 -0
  84. package/ios/AppDelegates/ExpoAppDelegate.swift +27 -8
  85. package/ios/JSI/ExpoModulesHostObject.h +33 -0
  86. package/ios/JSI/ExpoModulesHostObject.mm +40 -0
  87. package/ios/JSI/ExpoModulesProxySpec.h +4 -0
  88. package/ios/JSI/ExpoModulesProxySpec.mm +1 -3
  89. package/ios/JSI/JSIConversions.h +2 -0
  90. package/ios/JSI/JSIConversions.mm +9 -0
  91. package/ios/JSI/JSIInstaller.h +10 -0
  92. package/ios/JSI/JSIInstaller.mm +14 -2
  93. package/ios/JSI/JavaScriptObject.h +60 -0
  94. package/ios/JSI/JavaScriptObject.mm +93 -0
  95. package/ios/JSI/JavaScriptRuntime.h +54 -0
  96. package/ios/JSI/JavaScriptRuntime.mm +102 -0
  97. package/ios/ModuleRegistryAdapter/EXModuleRegistryAdapter.m +2 -12
  98. package/ios/NativeModulesProxy/EXComponentDataCompatibleWrapper.h +16 -0
  99. package/ios/NativeModulesProxy/EXComponentDataCompatibleWrapper.m +28 -0
  100. package/ios/NativeModulesProxy/EXNativeModulesProxy.mm +90 -66
  101. package/ios/RCTComponentData+Privates.h +12 -0
  102. package/ios/ReactDelegates/ExpoReactDelegate.swift +2 -2
  103. package/ios/ReactDelegates/ExpoReactDelegateHandler.swift +3 -3
  104. package/ios/ReactDelegates/ModulePriorities.swift +1 -1
  105. package/ios/Swift/AppContext.swift +38 -4
  106. package/ios/Swift/Arguments/ArgumentType.swift +4 -0
  107. package/ios/Swift/Arguments/Convertibles.swift +13 -13
  108. package/ios/Swift/Arguments/Types/EnumArgumentType.swift +11 -17
  109. package/ios/Swift/Arguments/Types/PromiseArgumentType.swift +1 -1
  110. package/ios/Swift/Arguments/Types/RawArgumentType.swift +2 -2
  111. package/ios/Swift/Conversions.swift +51 -56
  112. package/ios/Swift/EventListener.swift +8 -10
  113. package/ios/Swift/Events/Callback.swift +66 -0
  114. package/ios/Swift/Events/Event.swift +43 -0
  115. package/ios/Swift/Exceptions/ChainableException.swift +51 -0
  116. package/ios/Swift/{CodedError.swift → Exceptions/CodedError.swift} +1 -12
  117. package/ios/Swift/Exceptions/Exception.swift +62 -0
  118. package/ios/Swift/Exceptions/ExceptionOrigin.swift +28 -0
  119. package/ios/Swift/Exceptions/GenericException.swift +20 -0
  120. package/ios/Swift/Exceptions/UnexpectedException.swift +16 -0
  121. package/ios/Swift/Functions/AnyFunction.swift +11 -1
  122. package/ios/Swift/Functions/ConcreteFunction.swift +37 -16
  123. package/ios/Swift/JavaScriptUtils.swift +43 -0
  124. package/ios/Swift/ModuleHolder.swift +53 -14
  125. package/ios/Swift/ModuleRegistry.swift +4 -1
  126. package/ios/Swift/Modules/AnyModule.swift +0 -1
  127. package/ios/Swift/Modules/ModuleDefinition.swift +4 -13
  128. package/ios/Swift/Modules/ModuleDefinitionBuilder.swift +0 -1
  129. package/ios/Swift/Modules/ModuleDefinitionComponents.swift +0 -188
  130. package/ios/Swift/ModulesProvider.swift +0 -1
  131. package/ios/Swift/Objects/ObjectDefinition.swift +30 -0
  132. package/ios/Swift/Objects/ObjectDefinitionComponents.swift +208 -0
  133. package/ios/Swift/Promise.swift +8 -3
  134. package/ios/Swift/Records/AnyField.swift +7 -0
  135. package/ios/Swift/Records/Field.swift +24 -19
  136. package/ios/Swift/Records/FieldOption.swift +1 -1
  137. package/ios/Swift/Records/Record.swift +12 -4
  138. package/ios/Swift/SwiftInteropBridge.swift +39 -10
  139. package/ios/Swift/Views/AnyViewProp.swift +1 -1
  140. package/ios/Swift/Views/ComponentData.swift +95 -0
  141. package/ios/Swift/Views/ConcreteViewProp.swift +6 -8
  142. package/ios/Swift/Views/ViewFactory.swift +1 -1
  143. package/ios/Swift/Views/ViewManagerDefinition.swift +23 -2
  144. package/ios/Swift/Views/ViewManagerDefinitionBuilder.swift +0 -1
  145. package/ios/Swift/Views/ViewManagerDefinitionComponents.swift +26 -0
  146. package/ios/Swift/Views/ViewModuleWrapper.swift +5 -2
  147. package/ios/Tests/ArgumentTypeSpec.swift +3 -4
  148. package/ios/Tests/ConstantsSpec.swift +4 -4
  149. package/ios/Tests/ConvertiblesSpec.swift +33 -33
  150. package/ios/Tests/ExceptionsSpec.swift +112 -0
  151. package/ios/Tests/FunctionSpec.swift +20 -22
  152. package/ios/Tests/FunctionWithConvertiblesSpec.swift +2 -2
  153. package/ios/Tests/Mocks/ModuleMocks.swift +1 -1
  154. package/ios/Tests/Mocks/ModulesProviderMock.swift +0 -1
  155. package/ios/Tests/ModuleEventListenersSpec.swift +1 -1
  156. package/ios/Tests/RecordSpec.swift +7 -17
  157. package/package.json +3 -3
  158. package/src/index.ts +4 -0
  159. package/src/requireNativeModule.ts +29 -0
  160. package/src/sweet/NativeErrorManager.ts +2 -0
  161. package/src/sweet/setUpErrorManager.fx.ts +12 -0
@@ -16,150 +16,6 @@ extension AnyModule {
16
16
  return ModuleNameDefinition(name: name)
17
17
  }
18
18
 
19
- // MARK: - Constants
20
-
21
- /**
22
- Definition function setting the module's constants to export.
23
- */
24
- public func constants(_ body: @escaping () -> [String: Any?]) -> AnyDefinition {
25
- return ConstantsDefinition(body: body)
26
- }
27
-
28
- /**
29
- Definition function setting the module's constants to export.
30
- */
31
- public func constants(_ body: @autoclosure @escaping () -> [String: Any?]) -> AnyDefinition {
32
- return ConstantsDefinition(body: body)
33
- }
34
-
35
- // MARK: - Functions
36
-
37
- /**
38
- Function without arguments.
39
- */
40
- public func function<R>(
41
- _ name: String,
42
- _ closure: @escaping () -> R
43
- ) -> AnyFunction {
44
- return ConcreteFunction(
45
- name,
46
- argTypes: [],
47
- closure
48
- )
49
- }
50
-
51
- /**
52
- Function with one argument.
53
- */
54
- public func function<R, A0: AnyArgument>(
55
- _ name: String,
56
- _ closure: @escaping (A0) -> R
57
- ) -> AnyFunction {
58
- return ConcreteFunction(
59
- name,
60
- argTypes: [ArgumentType(A0.self)],
61
- closure
62
- )
63
- }
64
-
65
- /**
66
- Function with two arguments.
67
- */
68
- public func function<R, A0: AnyArgument, A1: AnyArgument>(
69
- _ name: String,
70
- _ closure: @escaping (A0, A1) -> R
71
- ) -> AnyFunction {
72
- return ConcreteFunction(
73
- name,
74
- argTypes: [ArgumentType(A0.self), ArgumentType(A1.self)],
75
- closure
76
- )
77
- }
78
-
79
- /**
80
- Function with three arguments.
81
- */
82
- public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument>(
83
- _ name: String,
84
- _ closure: @escaping (A0, A1, A2) -> R
85
- ) -> AnyFunction {
86
- return ConcreteFunction(
87
- name,
88
- argTypes: [ArgumentType(A0.self), ArgumentType(A1.self), ArgumentType(A2.self)],
89
- closure
90
- )
91
- }
92
-
93
- /**
94
- Function with four arguments.
95
- */
96
- public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument>(
97
- _ name: String,
98
- _ closure: @escaping (A0, A1, A2, A3) -> R
99
- ) -> AnyFunction {
100
- return ConcreteFunction(
101
- name,
102
- argTypes: [ArgumentType(A0.self), ArgumentType(A1.self), ArgumentType(A2.self), ArgumentType(A3.self)],
103
- closure
104
- )
105
- }
106
-
107
- /**
108
- Function with five arguments.
109
- */
110
- public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument>(
111
- _ name: String,
112
- _ closure: @escaping (A0, A1, A2, A3, A4) -> R
113
- ) -> AnyFunction {
114
- return ConcreteFunction(
115
- name,
116
- argTypes: [ArgumentType(A0.self), ArgumentType(A1.self), ArgumentType(A2.self), ArgumentType(A3.self), ArgumentType(A4.self)],
117
- closure
118
- )
119
- }
120
-
121
- /**
122
- Function with six arguments.
123
- */
124
- public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument, A5: AnyArgument>(
125
- _ name: String,
126
- _ closure: @escaping (A0, A1, A2, A3, A4, A5) -> R
127
- ) -> AnyFunction {
128
- return ConcreteFunction(
129
- name,
130
- argTypes: [ArgumentType(A0.self), ArgumentType(A1.self), ArgumentType(A2.self), ArgumentType(A3.self), ArgumentType(A4.self), ArgumentType(A5.self)],
131
- closure
132
- )
133
- }
134
-
135
- /**
136
- Function with seven arguments.
137
- */
138
- public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument, A5: AnyArgument, A6: AnyArgument>(
139
- _ name: String,
140
- _ closure: @escaping (A0, A1, A2, A3, A4, A5, A6) -> R
141
- ) -> AnyFunction {
142
- return ConcreteFunction(
143
- name,
144
- argTypes: [ArgumentType(A0.self), ArgumentType(A1.self), ArgumentType(A2.self), ArgumentType(A3.self), ArgumentType(A4.self), ArgumentType(A5.self), ArgumentType(A6.self)],
145
- closure
146
- )
147
- }
148
-
149
- /**
150
- Function with eight arguments.
151
- */
152
- public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument, A5: AnyArgument, A6: AnyArgument, A7: AnyArgument>(
153
- _ name: String,
154
- _ closure: @escaping (A0, A1, A2, A3, A4, A5, A6, A7) -> R
155
- ) -> AnyFunction {
156
- return ConcreteFunction(
157
- name,
158
- argTypes: [ArgumentType(A0.self), ArgumentType(A1.self), ArgumentType(A2.self), ArgumentType(A3.self), ArgumentType(A4.self), ArgumentType(A5.self), ArgumentType(A6.self), ArgumentType(A7.self)],
159
- closure
160
- )
161
- }
162
-
163
19
  // MARK: - Module's lifecycle
164
20
 
165
21
  /**
@@ -212,50 +68,6 @@ extension AnyModule {
212
68
  public func viewManager(@ViewManagerDefinitionBuilder _ closure: @escaping () -> ViewManagerDefinition) -> AnyDefinition {
213
69
  return closure()
214
70
  }
215
-
216
- // MARK: - Events
217
-
218
- /**
219
- Defines event names that this module can send to JavaScript.
220
- */
221
- public func events(_ names: String...) -> AnyDefinition {
222
- return EventsDefinition(names: names)
223
- }
224
-
225
- /**
226
- Function that is invoked when the first event listener is added.
227
- */
228
- public func onStartObserving(_ body: @escaping () -> ()) -> AnyFunction {
229
- return ConcreteFunction("startObserving", argTypes: [], body)
230
- }
231
-
232
- /**
233
- Function that is invoked when all event listeners are removed.
234
- */
235
- public func onStopObserving(_ body: @escaping () -> ()) -> AnyFunction {
236
- return ConcreteFunction("stopObserving", argTypes: [], body)
237
- }
238
- }
239
-
240
- /**
241
- Defines the factory creating a native view when the module is used as a view.
242
- */
243
- public func view(_ closure: @escaping () -> UIView) -> AnyDefinition {
244
- return ViewFactory(closure)
245
- }
246
-
247
- /**
248
- Creates a view prop that defines its name and setter.
249
- */
250
- public func prop<ViewType: UIView, PropType: AnyArgument>(
251
- _ name: String,
252
- _ setter: @escaping (ViewType, PropType) -> Void
253
- ) -> AnyDefinition {
254
- return ConcreteViewProp(
255
- name: name,
256
- propType: ArgumentType(PropType.self),
257
- setter: setter
258
- )
259
71
  }
260
72
 
261
73
  // TODO: - Remove deprecated `method` component once SDK44 is out.
@@ -1,4 +1,3 @@
1
-
2
1
  import Foundation
3
2
 
4
3
  /**
@@ -0,0 +1,30 @@
1
+ // Copyright 2021-present 650 Industries. All rights reserved.
2
+
3
+ /**
4
+ Base class for other definitions representing an object, such as `ModuleDefinition`.
5
+ */
6
+ public class ObjectDefinition: AnyDefinition {
7
+ /**
8
+ A dictionary of functions defined by the object.
9
+ */
10
+ let functions: [String: AnyFunction]
11
+
12
+ /**
13
+ An array of constants definitions.
14
+ */
15
+ let constants: [ConstantsDefinition]
16
+
17
+ /**
18
+ Default initializer receiving children definitions from the result builder.
19
+ */
20
+ init(definitions: [AnyDefinition]) {
21
+ self.functions = definitions
22
+ .compactMap { $0 as? AnyFunction }
23
+ .reduce(into: [String: AnyFunction]()) { dict, function in
24
+ dict[function.name] = function
25
+ }
26
+
27
+ self.constants = definitions
28
+ .compactMap { $0 as? ConstantsDefinition }
29
+ }
30
+ }
@@ -0,0 +1,208 @@
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
+ public func constants(_ body: @escaping () -> [String: Any?]) -> AnyDefinition {
10
+ return ConstantsDefinition(body: body)
11
+ }
12
+
13
+ /**
14
+ Definition function setting the module's constants to export.
15
+ */
16
+ public func constants(_ body: @autoclosure @escaping () -> [String: Any?]) -> AnyDefinition {
17
+ return ConstantsDefinition(body: body)
18
+ }
19
+
20
+ // MARK: - Functions
21
+
22
+ /**
23
+ Function without arguments.
24
+ */
25
+ public func function<R>(
26
+ _ name: String,
27
+ _ closure: @escaping () throws -> R
28
+ ) -> AnyFunction {
29
+ return ConcreteFunction(
30
+ name,
31
+ argTypes: [],
32
+ closure
33
+ )
34
+ }
35
+
36
+ /**
37
+ Function with one argument.
38
+ */
39
+ public func function<R, A0: AnyArgument>(
40
+ _ name: String,
41
+ _ closure: @escaping (A0) throws -> R
42
+ ) -> AnyFunction {
43
+ return ConcreteFunction(
44
+ name,
45
+ argTypes: [ArgumentType(A0.self)],
46
+ closure
47
+ )
48
+ }
49
+
50
+ /**
51
+ Function with two arguments.
52
+ */
53
+ public func function<R, A0: AnyArgument, A1: AnyArgument>(
54
+ _ name: String,
55
+ _ closure: @escaping (A0, A1) throws -> R
56
+ ) -> AnyFunction {
57
+ return ConcreteFunction(
58
+ name,
59
+ argTypes: [ArgumentType(A0.self), ArgumentType(A1.self)],
60
+ closure
61
+ )
62
+ }
63
+
64
+ /**
65
+ Function with three arguments.
66
+ */
67
+ public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument>(
68
+ _ name: String,
69
+ _ closure: @escaping (A0, A1, A2) throws -> R
70
+ ) -> AnyFunction {
71
+ return ConcreteFunction(
72
+ name,
73
+ argTypes: [
74
+ ArgumentType(A0.self),
75
+ ArgumentType(A1.self),
76
+ ArgumentType(A2.self)
77
+ ],
78
+ closure
79
+ )
80
+ }
81
+
82
+ /**
83
+ Function with four arguments.
84
+ */
85
+ public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument>(
86
+ _ name: String,
87
+ _ closure: @escaping (A0, A1, A2, A3) throws -> R
88
+ ) -> AnyFunction {
89
+ return ConcreteFunction(
90
+ name,
91
+ argTypes: [
92
+ ArgumentType(A0.self),
93
+ ArgumentType(A1.self),
94
+ ArgumentType(A2.self),
95
+ ArgumentType(A3.self)
96
+ ],
97
+ closure
98
+ )
99
+ }
100
+
101
+ /**
102
+ Function with five arguments.
103
+ */
104
+ public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument>(
105
+ _ name: String,
106
+ _ closure: @escaping (A0, A1, A2, A3, A4) throws -> R
107
+ ) -> AnyFunction {
108
+ return ConcreteFunction(
109
+ name,
110
+ argTypes: [
111
+ ArgumentType(A0.self),
112
+ ArgumentType(A1.self),
113
+ ArgumentType(A2.self),
114
+ ArgumentType(A3.self),
115
+ ArgumentType(A4.self)
116
+ ],
117
+ closure
118
+ )
119
+ }
120
+
121
+ /**
122
+ Function with six arguments.
123
+ */
124
+ public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument, A5: AnyArgument>(
125
+ _ name: String,
126
+ _ closure: @escaping (A0, A1, A2, A3, A4, A5) throws -> R
127
+ ) -> AnyFunction {
128
+ return ConcreteFunction(
129
+ name,
130
+ argTypes: [
131
+ ArgumentType(A0.self),
132
+ ArgumentType(A1.self),
133
+ ArgumentType(A2.self),
134
+ ArgumentType(A3.self),
135
+ ArgumentType(A4.self),
136
+ ArgumentType(A5.self)
137
+ ],
138
+ closure
139
+ )
140
+ }
141
+
142
+ /**
143
+ Function with seven arguments.
144
+ */
145
+ public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument, A5: AnyArgument, A6: AnyArgument>(
146
+ _ name: String,
147
+ _ closure: @escaping (A0, A1, A2, A3, A4, A5, A6) throws -> R
148
+ ) -> AnyFunction {
149
+ return ConcreteFunction(
150
+ name,
151
+ argTypes: [
152
+ ArgumentType(A0.self),
153
+ ArgumentType(A1.self),
154
+ ArgumentType(A2.self),
155
+ ArgumentType(A3.self),
156
+ ArgumentType(A4.self),
157
+ ArgumentType(A5.self),
158
+ ArgumentType(A6.self)
159
+ ],
160
+ closure
161
+ )
162
+ }
163
+
164
+ /**
165
+ Function with eight arguments.
166
+ */
167
+ public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument, A5: AnyArgument, A6: AnyArgument, A7: AnyArgument>(
168
+ _ name: String,
169
+ _ closure: @escaping (A0, A1, A2, A3, A4, A5, A6, A7) throws -> R
170
+ ) -> AnyFunction {
171
+ return ConcreteFunction(
172
+ name,
173
+ argTypes: [
174
+ ArgumentType(A0.self),
175
+ ArgumentType(A1.self),
176
+ ArgumentType(A2.self),
177
+ ArgumentType(A3.self),
178
+ ArgumentType(A4.self),
179
+ ArgumentType(A5.self),
180
+ ArgumentType(A6.self),
181
+ ArgumentType(A7.self)
182
+ ],
183
+ closure
184
+ )
185
+ }
186
+
187
+ // MARK: - Events
188
+
189
+ /**
190
+ Defines event names that the object can send to JavaScript.
191
+ */
192
+ public func events(_ names: String...) -> AnyDefinition {
193
+ return EventsDefinition(names: names)
194
+ }
195
+
196
+ /**
197
+ Function that is invoked when the first event listener is added.
198
+ */
199
+ public func onStartObserving(_ body: @escaping () -> Void) -> AnyFunction {
200
+ return ConcreteFunction("startObserving", argTypes: [], body)
201
+ }
202
+
203
+ /**
204
+ Function that is invoked when all event listeners are removed.
205
+ */
206
+ public func onStopObserving(_ body: @escaping () -> Void) -> AnyFunction {
207
+ return ConcreteFunction("stopObserving", argTypes: [], body)
208
+ }
@@ -1,3 +1,4 @@
1
+ // Copyright 2021-present 650 Industries. All rights reserved.
1
2
 
2
3
  public struct Promise: AnyArgument {
3
4
  public typealias ResolveClosure = (Any?) -> Void
@@ -11,20 +12,24 @@ public struct Promise: AnyArgument {
11
12
  Necessary in some places not converted to Swift, such as `EXPermissionsMethodsDelegate`.
12
13
  */
13
14
  public var legacyRejecter: EXPromiseRejectBlock {
14
- return { code, description, error in
15
+ return { code, description, _ in
15
16
  reject(code ?? "", description ?? "")
16
17
  }
17
18
  }
18
19
 
19
- public func resolve(_ value: Any? = nil) -> Void {
20
+ public func resolve(_ value: Any? = nil) {
20
21
  resolver(value)
21
22
  }
22
23
 
24
+ public func reject(_ error: Error) {
25
+ rejecter(UnexpectedException(error))
26
+ }
27
+
23
28
  public func reject(_ error: CodedError) {
24
29
  rejecter(error)
25
30
  }
26
31
 
27
- public func reject(_ code: String, _ description: String) -> Void {
32
+ public func reject(_ code: String, _ description: String) {
28
33
  rejecter(SimpleCodedError(code, description))
29
34
  }
30
35
  }
@@ -12,5 +12,12 @@ internal protocol AnyFieldInternal: AnyField {
12
12
  var key: String? { get }
13
13
  var options: Set<FieldOption> { get set }
14
14
 
15
+ /**
16
+ Whether the value for this field must be explicitly provided.
17
+ The record throws an error when the source dictionary is missing a required value.
18
+ Note that it's NOT the opposite to `isOptional`.
19
+ */
20
+ var isRequired: Bool { get }
21
+
15
22
  func set(_ newValue: Any?) throws
16
23
  }
@@ -8,6 +8,8 @@ public final class Field<Type>: AnyFieldInternal {
8
8
  */
9
9
  public var wrappedValue: Type
10
10
 
11
+ private let fieldType: AnyArgumentType = ArgumentType(Type.self)
12
+
11
13
  /**
12
14
  Field's key in the dictionary, which by default is a label of the wrapped property.
13
15
  Sadly, property wrappers don't receive properties' label, so we must wait until it's assigned by `Record`.
@@ -23,11 +25,13 @@ public final class Field<Type>: AnyFieldInternal {
23
25
 
24
26
  /**
25
27
  Whether the generic field type accepts `nil` values.
26
- We can't check it directly with `Optional` because it has associated type,
27
- but all optionals implement non-generic `ExpressibleByNilLiteral` protocol.
28
28
  */
29
29
  internal var isOptional: Bool {
30
- return Type.self is ExpressibleByNilLiteral.Type
30
+ return fieldType is OptionalArgumentType
31
+ }
32
+
33
+ internal var isRequired: Bool {
34
+ options.contains(.required)
31
35
  }
32
36
 
33
37
  /**
@@ -71,29 +75,30 @@ public final class Field<Type>: AnyFieldInternal {
71
75
  Sets the wrapped value with a value of `Any` type.
72
76
  */
73
77
  internal func set(_ newValue: Any?) throws {
74
- if newValue == nil && (!isOptional || options.contains(.required)) {
75
- throw FieldRequiredError(fieldKey: key!)
78
+ if newValue == nil && (!isOptional || isRequired) {
79
+ throw FieldRequiredException(key!)
76
80
  }
77
- if let value = newValue as? Type {
78
- wrappedValue = value
79
- return
81
+ do {
82
+ if let value = try fieldType.cast(newValue) as? Type {
83
+ wrappedValue = value
84
+ }
85
+ } catch {
86
+ throw FieldInvalidTypeException((fieldKey: key!, value: newValue, desiredType: Type.self)).causedBy(error)
80
87
  }
81
- throw FieldInvalidTypeError(fieldKey: key!, value: newValue, desiredType: Type.self)
82
88
  }
83
89
  }
84
90
 
85
- internal struct FieldRequiredError: CodedError {
86
- let fieldKey: String
87
- var description: String {
88
- "Value for field `\(fieldKey)` is required, got `nil`"
91
+ internal class FieldRequiredException: GenericException<String> {
92
+ override var reason: String {
93
+ "Value for field '\(param)' is required, got nil"
89
94
  }
90
95
  }
91
96
 
92
- internal struct FieldInvalidTypeError: CodedError {
93
- let fieldKey: String
94
- let value: Any?
95
- let desiredType: Any.Type
96
- var description: String {
97
- "Cannot cast value `\(String(describing: value!))` (\(type(of: value!))) for field `\(fieldKey)` (\(String(describing: desiredType)))"
97
+ internal class FieldInvalidTypeException: GenericException<(fieldKey: String, value: Any?, desiredType: Any.Type)> {
98
+ override var reason: String {
99
+ let value = String(describing: param.value ?? "null")
100
+ let desiredType = String(describing: param.desiredType)
101
+
102
+ return "Cannot cast '\(value)' for field '\(param.fieldKey)' of type \(desiredType)"
98
103
  }
99
104
  }
@@ -15,7 +15,7 @@ public struct FieldOption: Equatable, Hashable, ExpressibleByIntegerLiteral, Exp
15
15
  /**
16
16
  Field options are equal when their raw values and parameters are equal.
17
17
  */
18
- public static func ==(lhs: Self, rhs: Self) -> Bool {
18
+ public static func == (lhs: Self, rhs: Self) -> Bool {
19
19
  return lhs.rawValue == rhs.rawValue && lhs.key == rhs.key
20
20
  }
21
21
 
@@ -31,14 +31,22 @@ public extension Record {
31
31
  if let value = value as? Dict {
32
32
  return try Self(from: value)
33
33
  }
34
- throw Conversions.ConvertingError<Self>(value: value)
34
+ throw Conversions.ConvertingException<Self>(value)
35
35
  }
36
36
 
37
37
  init(from dict: Dict) throws {
38
38
  self.init()
39
39
 
40
+ let dictKeys = dict.keys
41
+
40
42
  try fieldsOf(self).forEach { field in
41
- try field.set(dict[field.key!])
43
+ guard let key = field.key else {
44
+ // This should never happen, but just in case skip fields without the key.
45
+ return
46
+ }
47
+ if dictKeys.contains(key) || field.isRequired {
48
+ try field.set(dict[key])
49
+ }
42
50
  }
43
51
  }
44
52
 
@@ -53,7 +61,7 @@ public extension Record {
53
61
  Returns an array of fields found in record's mirror. If the field is missing the `key`,
54
62
  it gets assigned to the property label, so after all it's safe to enforce unwrapping it (using `key!`).
55
63
  */
56
- fileprivate func fieldsOf(_ record: Record) -> [AnyFieldInternal] {
64
+ private func fieldsOf(_ record: Record) -> [AnyFieldInternal] {
57
65
  return Mirror(reflecting: record).children.compactMap { (label: String?, value: Any) in
58
66
  guard var field = value as? AnyFieldInternal, let key = field.key ?? convertLabelToKey(label) else {
59
67
  return nil
@@ -66,6 +74,6 @@ fileprivate func fieldsOf(_ record: Record) -> [AnyFieldInternal] {
66
74
  /**
67
75
  Converts mirror's label to field's key by dropping the "_" prefix from wrapped property label.
68
76
  */
69
- fileprivate func convertLabelToKey(_ label: String?) -> String? {
77
+ private func convertLabelToKey(_ label: String?) -> String? {
70
78
  return (label != nil && label!.starts(with: "_")) ? String(label!.dropFirst()) : label
71
79
  }
@@ -1,5 +1,7 @@
1
+ // Copyright 2021-present 650 Industries. All rights reserved.
1
2
 
2
3
  import Foundation
4
+ import React
3
5
 
4
6
  @objc
5
7
  public final class SwiftInteropBridge: NSObject {
@@ -21,11 +23,18 @@ public final class SwiftInteropBridge: NSObject {
21
23
  }
22
24
 
23
25
  @objc
24
- public func callFunction(_ functionName: String,
25
- onModule moduleName: String,
26
- withArgs args: [Any],
27
- resolve: @escaping EXPromiseResolveBlock,
28
- reject: @escaping EXPromiseRejectBlock) {
26
+ public func setReactBridge(_ reactBridge: RCTBridge) {
27
+ appContext.reactBridge = reactBridge
28
+ }
29
+
30
+ @objc
31
+ public func callFunction(
32
+ _ functionName: String,
33
+ onModule moduleName: String,
34
+ withArgs args: [Any],
35
+ resolve: @escaping EXPromiseResolveBlock,
36
+ reject: @escaping EXPromiseRejectBlock
37
+ ) {
29
38
  registry
30
39
  .get(moduleHolderForName: moduleName)?
31
40
  .call(function: functionName, args: args) { value, error in
@@ -40,9 +49,11 @@ public final class SwiftInteropBridge: NSObject {
40
49
  }
41
50
 
42
51
  @objc
43
- public func callFunctionSync(_ functionName: String,
44
- onModule moduleName: String,
45
- withArgs args: [Any]) -> Any? {
52
+ public func callFunctionSync(
53
+ _ functionName: String,
54
+ onModule moduleName: String,
55
+ withArgs args: [Any]
56
+ ) -> Any? {
46
57
  return registry
47
58
  .get(moduleHolderForName: moduleName)?
48
59
  .callSync(function: functionName, args: args)
@@ -53,11 +64,11 @@ public final class SwiftInteropBridge: NSObject {
53
64
  var constants = [String: [[String: Any]]]()
54
65
 
55
66
  for holder in registry {
56
- constants[holder.name] = holder.definition.functions.map({ (functionName, function) in
67
+ constants[holder.name] = holder.definition.functions.map({ functionName, function in
57
68
  return [
58
69
  "name": functionName,
59
70
  "argumentsCount": function.argumentsCount,
60
- "key": functionName,
71
+ "key": functionName
61
72
  ]
62
73
  })
63
74
  }
@@ -92,6 +103,24 @@ public final class SwiftInteropBridge: NSObject {
92
103
  }
93
104
  }
94
105
 
106
+ /**
107
+ Sets the JSI runtime on the operating `AppContext`.
108
+ */
109
+ @objc
110
+ public func setRuntime(_ runtime: JavaScriptRuntime?) {
111
+ appContext.runtime = runtime
112
+ }
113
+
114
+ @objc
115
+ public func getModuleNames() -> [String] {
116
+ return registry.getModuleNames()
117
+ }
118
+
119
+ @objc
120
+ public func getNativeModuleObject(_ moduleName: String) -> JavaScriptObject? {
121
+ return registry.get(moduleHolderForName: moduleName)?.javaScriptObject
122
+ }
123
+
95
124
  // MARK: - Events
96
125
 
97
126
  /**