expo-modules-core 0.6.4 → 0.8.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 (217) hide show
  1. package/CHANGELOG.md +53 -0
  2. package/README.md +1 -1
  3. package/android/ExpoModulesCorePlugin.gradle +15 -0
  4. package/android/build.gradle +46 -32
  5. package/android/src/main/java/expo/modules/adapters/react/NativeModulesProxy.java +5 -5
  6. package/android/src/main/java/expo/modules/adapters/react/services/UIManagerModuleWrapper.java +13 -0
  7. package/android/src/main/java/expo/modules/core/ViewManager.java +9 -0
  8. package/android/src/main/java/expo/modules/core/interfaces/JavaScriptContextProvider.java +4 -0
  9. package/android/src/main/java/expo/modules/core/interfaces/ReactActivityHandler.java +37 -1
  10. package/android/src/main/java/expo/modules/core/interfaces/ReactNativeHostHandler.java +19 -0
  11. package/android/src/main/java/expo/modules/core/interfaces/services/UIManager.java +2 -0
  12. package/android/src/main/java/expo/modules/kotlin/AppContext.kt +23 -5
  13. package/android/src/main/java/expo/modules/kotlin/DynamicExtenstions.kt +5 -3
  14. package/android/src/main/java/expo/modules/kotlin/KPromiseWrapper.kt +3 -8
  15. package/android/src/main/java/expo/modules/kotlin/KotlinInteropModuleRegistry.kt +24 -9
  16. package/android/src/main/java/expo/modules/kotlin/ModuleHolder.kt +12 -7
  17. package/android/src/main/java/expo/modules/kotlin/ModuleRegistry.kt +23 -1
  18. package/android/src/main/java/expo/modules/kotlin/Promise.kt +1 -1
  19. package/android/src/main/java/expo/modules/kotlin/callbacks/Callback.kt +5 -0
  20. package/android/src/main/java/expo/modules/kotlin/callbacks/ViewCallback.kt +39 -0
  21. package/android/src/main/java/expo/modules/kotlin/callbacks/ViewCallbackDelegate.kt +27 -0
  22. package/android/src/main/java/expo/modules/kotlin/defaultmodules/ErrorManagerModule.kt +25 -0
  23. package/android/src/main/java/expo/modules/kotlin/events/EventEmitter.kt +13 -0
  24. package/android/src/main/java/expo/modules/kotlin/events/KModuleEventEmitterWrapper.kt +102 -0
  25. package/android/src/main/java/expo/modules/kotlin/exception/CodedException.kt +93 -9
  26. package/android/src/main/java/expo/modules/kotlin/exception/ExceptionDecorator.kt +11 -0
  27. package/android/src/main/java/expo/modules/kotlin/{methods/AnyMethod.kt → functions/AnyFunction.kt} +18 -18
  28. package/android/src/main/java/expo/modules/kotlin/functions/AsyncFunction.kt +15 -0
  29. package/android/src/main/java/expo/modules/kotlin/functions/AsyncFunctionBuilder.kt +61 -0
  30. package/android/src/main/java/expo/modules/kotlin/functions/AsyncFunctionWithPromise.kt +15 -0
  31. package/android/src/main/java/expo/modules/kotlin/functions/AsyncSuspendFunction.kt +36 -0
  32. package/android/src/main/java/expo/modules/kotlin/modules/DefinitionMarker.kt +4 -0
  33. package/android/src/main/java/expo/modules/kotlin/modules/Module.kt +17 -2
  34. package/android/src/main/java/expo/modules/kotlin/modules/ModuleDefinitionBuilder.kt +219 -30
  35. package/android/src/main/java/expo/modules/kotlin/modules/ModuleDefinitionData.kt +2 -2
  36. package/android/src/main/java/expo/modules/kotlin/records/FieldValidator.kt +139 -0
  37. package/android/src/main/java/expo/modules/kotlin/records/RecordTypeConverter.kt +71 -15
  38. package/android/src/main/java/expo/modules/kotlin/records/Required.kt +5 -0
  39. package/android/src/main/java/expo/modules/kotlin/records/ValidationBinder.kt +110 -0
  40. package/android/src/main/java/expo/modules/kotlin/records/Validators.kt +61 -0
  41. package/android/src/main/java/expo/modules/kotlin/types/ArrayTypeConverter.kt +11 -5
  42. package/android/src/main/java/expo/modules/kotlin/types/JSTypeConverter.kt +35 -0
  43. package/android/src/main/java/expo/modules/kotlin/types/JSTypeConverterHelper.kt +148 -0
  44. package/android/src/main/java/expo/modules/kotlin/types/ListTypeConverter.kt +10 -4
  45. package/android/src/main/java/expo/modules/kotlin/types/MapTypeConverter.kt +12 -6
  46. package/android/src/main/java/expo/modules/kotlin/types/PairTypeConverter.kt +29 -13
  47. package/android/src/main/java/expo/modules/kotlin/types/TypeConverter.kt +2 -1
  48. package/android/src/main/java/expo/modules/kotlin/types/TypeConverterProvider.kt +9 -1
  49. package/android/src/main/java/expo/modules/kotlin/views/CallbacksDefinition.kt +3 -0
  50. package/android/src/main/java/expo/modules/kotlin/views/GroupViewManagerWrapper.kt +71 -0
  51. package/android/src/main/java/expo/modules/kotlin/views/SimpleViewManagerWrapper.kt +22 -0
  52. package/android/src/main/java/expo/modules/kotlin/views/ViewGroupDefinition.kt +18 -0
  53. package/android/src/main/java/expo/modules/kotlin/views/ViewGroupDefinitionBuilder.kt +64 -0
  54. package/android/src/main/java/expo/modules/kotlin/views/ViewManagerDefinition.kt +30 -2
  55. package/android/src/main/java/expo/modules/kotlin/views/ViewManagerDefinitionBuilder.kt +42 -1
  56. package/android/src/main/java/expo/modules/kotlin/views/ViewManagerWrapperDelegate.kt +62 -2
  57. package/build/EventEmitter.d.ts +1 -0
  58. package/build/EventEmitter.d.ts.map +1 -0
  59. package/build/NativeModulesProxy.d.ts +1 -0
  60. package/build/NativeModulesProxy.d.ts.map +1 -0
  61. package/build/NativeModulesProxy.native.d.ts +1 -4
  62. package/build/NativeModulesProxy.native.d.ts.map +1 -0
  63. package/build/NativeModulesProxy.native.js +1 -14
  64. package/build/NativeModulesProxy.native.js.map +1 -1
  65. package/build/NativeModulesProxy.types.d.ts +1 -3
  66. package/build/NativeModulesProxy.types.d.ts.map +1 -0
  67. package/build/NativeModulesProxy.types.js.map +1 -1
  68. package/build/NativeViewManagerAdapter.d.ts +1 -0
  69. package/build/NativeViewManagerAdapter.d.ts.map +1 -0
  70. package/build/NativeViewManagerAdapter.native.d.ts +1 -0
  71. package/build/NativeViewManagerAdapter.native.d.ts.map +1 -0
  72. package/build/NativeViewManagerAdapter.native.js +9 -33
  73. package/build/NativeViewManagerAdapter.native.js.map +1 -1
  74. package/build/PermissionsHook.d.ts +1 -0
  75. package/build/PermissionsHook.d.ts.map +1 -0
  76. package/build/PermissionsInterface.d.ts +1 -0
  77. package/build/PermissionsInterface.d.ts.map +1 -0
  78. package/build/Platform.d.ts +1 -0
  79. package/build/Platform.d.ts.map +1 -0
  80. package/build/SyntheticPlatformEmitter.d.ts +1 -0
  81. package/build/SyntheticPlatformEmitter.d.ts.map +1 -0
  82. package/build/SyntheticPlatformEmitter.web.d.ts +1 -0
  83. package/build/SyntheticPlatformEmitter.web.d.ts.map +1 -0
  84. package/build/deprecate.d.ts +1 -0
  85. package/build/deprecate.d.ts.map +1 -0
  86. package/build/environment/browser.d.ts +1 -0
  87. package/build/environment/browser.d.ts.map +1 -0
  88. package/build/environment/browser.web.d.ts +1 -0
  89. package/build/environment/browser.web.d.ts.map +1 -0
  90. package/build/errors/CodedError.d.ts +1 -0
  91. package/build/errors/CodedError.d.ts.map +1 -0
  92. package/build/errors/UnavailabilityError.d.ts +1 -0
  93. package/build/errors/UnavailabilityError.d.ts.map +1 -0
  94. package/build/index.d.ts +3 -0
  95. package/build/index.d.ts.map +1 -0
  96. package/build/index.js +2 -0
  97. package/build/index.js.map +1 -1
  98. package/build/requireNativeModule.d.ts +16 -0
  99. package/build/requireNativeModule.d.ts.map +1 -0
  100. package/build/requireNativeModule.js +18 -0
  101. package/build/requireNativeModule.js.map +1 -0
  102. package/build/sweet/NativeErrorManager.d.ts +3 -0
  103. package/build/sweet/NativeErrorManager.d.ts.map +1 -0
  104. package/build/sweet/NativeErrorManager.js +3 -0
  105. package/build/sweet/NativeErrorManager.js.map +1 -0
  106. package/build/sweet/setUpErrorManager.fx.d.ts +2 -0
  107. package/build/sweet/setUpErrorManager.fx.d.ts.map +1 -0
  108. package/build/sweet/setUpErrorManager.fx.js +11 -0
  109. package/build/sweet/setUpErrorManager.fx.js.map +1 -0
  110. package/ios/AppDelegates/EXAppDelegatesLoader.m +4 -8
  111. package/ios/AppDelegates/ExpoAppDelegate.swift +22 -20
  112. package/ios/EXAppDefines.h +1 -0
  113. package/ios/EXAppDefines.m +6 -0
  114. package/ios/EXUtilities.h +2 -0
  115. package/ios/EXUtilities.m +12 -0
  116. package/ios/ExpoModulesCore.h +4 -0
  117. package/ios/ExpoModulesCore.podspec +4 -2
  118. package/ios/Interfaces/FileSystem/EXFileSystemInterface.h +1 -1
  119. package/ios/Interfaces/TaskManager/EXTaskServiceInterface.h +1 -0
  120. package/ios/JSI/{JSIConversions.h → EXJSIConversions.h} +5 -0
  121. package/ios/JSI/{JSIConversions.mm → EXJSIConversions.mm} +21 -1
  122. package/ios/JSI/{JSIInstaller.h → EXJSIInstaller.h} +10 -0
  123. package/ios/JSI/EXJSIInstaller.mm +17 -0
  124. package/ios/JSI/EXJSIUtils.h +19 -0
  125. package/ios/JSI/EXJSIUtils.mm +89 -0
  126. package/ios/JSI/EXJavaScriptObject.h +97 -0
  127. package/ios/JSI/EXJavaScriptObject.mm +121 -0
  128. package/ios/JSI/EXJavaScriptRuntime.h +73 -0
  129. package/ios/JSI/EXJavaScriptRuntime.mm +153 -0
  130. package/ios/JSI/EXJavaScriptValue.h +57 -0
  131. package/ios/JSI/EXJavaScriptValue.mm +166 -0
  132. package/ios/JSI/ExpoModulesHostObject.h +33 -0
  133. package/ios/JSI/ExpoModulesHostObject.mm +41 -0
  134. package/ios/JSI/JavaScriptRuntime.swift +32 -0
  135. package/ios/JSI/JavaScriptValue.swift +94 -0
  136. package/ios/ModuleRegistryAdapter/EXModuleRegistryAdapter.m +3 -23
  137. package/ios/NativeModulesProxy/EXNativeModulesProxy.h +2 -2
  138. package/ios/NativeModulesProxy/EXNativeModulesProxy.mm +101 -75
  139. package/ios/RCTComponentData+Privates.h +12 -0
  140. package/ios/ReactDelegates/EXReactCompatibleHelpers.h +18 -0
  141. package/ios/ReactDelegates/EXReactCompatibleHelpers.m +19 -0
  142. package/ios/ReactDelegates/ExpoReactDelegate.swift +3 -3
  143. package/ios/ReactDelegates/ExpoReactDelegateHandler.swift +4 -4
  144. package/ios/ReactDelegates/ModulePriorities.swift +1 -1
  145. package/ios/Swift/AppContext.swift +64 -4
  146. package/ios/Swift/Arguments/ArgumentType.swift +4 -0
  147. package/ios/Swift/Arguments/Convertibles.swift +13 -13
  148. package/ios/Swift/Arguments/Types/EnumArgumentType.swift +11 -17
  149. package/ios/Swift/Arguments/Types/PromiseArgumentType.swift +1 -1
  150. package/ios/Swift/Arguments/Types/RawArgumentType.swift +2 -2
  151. package/ios/Swift/Conversions.swift +51 -56
  152. package/ios/Swift/EventListener.swift +8 -10
  153. package/ios/Swift/Events/Callback.swift +66 -0
  154. package/ios/Swift/Events/Event.swift +43 -0
  155. package/ios/Swift/Exceptions/ChainableException.swift +51 -0
  156. package/ios/Swift/{CodedError.swift → Exceptions/CodedError.swift} +1 -12
  157. package/ios/Swift/Exceptions/Exception.swift +62 -0
  158. package/ios/Swift/Exceptions/ExceptionOrigin.swift +28 -0
  159. package/ios/Swift/Exceptions/GenericException.swift +20 -0
  160. package/ios/Swift/Exceptions/UnexpectedException.swift +16 -0
  161. package/ios/Swift/Functions/AnyFunction.swift +11 -1
  162. package/ios/Swift/Functions/AsyncFunction.swift +17 -0
  163. package/ios/Swift/Functions/ConcreteFunction.swift +43 -17
  164. package/ios/Swift/JavaScriptUtils.swift +54 -0
  165. package/ios/Swift/ModuleHolder.swift +66 -16
  166. package/ios/Swift/ModuleRegistry.swift +4 -1
  167. package/ios/Swift/Modules/AnyModule.swift +0 -1
  168. package/ios/Swift/Modules/ModuleDefinition.swift +4 -13
  169. package/ios/Swift/Modules/ModuleDefinitionBuilder.swift +0 -1
  170. package/ios/Swift/Modules/ModuleDefinitionComponents.swift +0 -188
  171. package/ios/Swift/ModulesProvider.swift +3 -11
  172. package/ios/Swift/Objects/ObjectDefinition.swift +30 -0
  173. package/ios/Swift/Objects/ObjectDefinitionComponents.swift +384 -0
  174. package/ios/Swift/Promise.swift +8 -3
  175. package/ios/Swift/Records/AnyField.swift +7 -0
  176. package/ios/Swift/Records/Field.swift +24 -19
  177. package/ios/Swift/Records/FieldOption.swift +1 -1
  178. package/ios/Swift/Records/Record.swift +12 -4
  179. package/ios/Swift/SwiftInteropBridge.swift +53 -15
  180. package/ios/Swift/Views/AnyViewProp.swift +1 -1
  181. package/ios/Swift/Views/ComponentData.swift +96 -0
  182. package/ios/Swift/Views/ConcreteViewProp.swift +6 -8
  183. package/ios/Swift/Views/ExpoView.swift +8 -0
  184. package/ios/Swift/Views/ViewFactory.swift +1 -1
  185. package/ios/Swift/Views/ViewManagerDefinition.swift +23 -2
  186. package/ios/Swift/Views/ViewManagerDefinitionBuilder.swift +0 -1
  187. package/ios/Swift/Views/ViewManagerDefinitionComponents.swift +26 -0
  188. package/ios/Swift/Views/ViewModuleWrapper.swift +5 -2
  189. package/ios/Swift.h +5 -0
  190. package/ios/Tests/ArgumentTypeSpec.swift +5 -7
  191. package/ios/Tests/ConstantsSpec.swift +6 -7
  192. package/ios/Tests/ConvertiblesSpec.swift +35 -36
  193. package/ios/Tests/ExceptionsSpec.swift +111 -0
  194. package/ios/Tests/ExpoModulesSpec.swift +76 -0
  195. package/ios/Tests/FunctionSpec.swift +22 -25
  196. package/ios/Tests/FunctionWithConvertiblesSpec.swift +4 -5
  197. package/ios/Tests/JavaScriptObjectSpec.swift +97 -0
  198. package/ios/Tests/JavaScriptRuntimeSpec.swift +94 -0
  199. package/ios/Tests/Mocks/ModuleMocks.swift +1 -1
  200. package/ios/Tests/Mocks/ModulesProviderMock.swift +0 -1
  201. package/ios/Tests/ModuleEventListenersSpec.swift +3 -4
  202. package/ios/Tests/ModuleRegistrySpec.swift +2 -3
  203. package/ios/Tests/RecordSpec.swift +9 -20
  204. package/package.json +3 -3
  205. package/src/NativeModulesProxy.native.ts +2 -22
  206. package/src/NativeModulesProxy.types.ts +0 -8
  207. package/src/NativeViewManagerAdapter.native.tsx +12 -28
  208. package/src/index.ts +4 -0
  209. package/src/requireNativeModule.ts +29 -0
  210. package/src/sweet/NativeErrorManager.ts +2 -0
  211. package/src/sweet/setUpErrorManager.fx.ts +12 -0
  212. package/android/src/main/java/expo/modules/kotlin/events/KEventEmitterWrapper.kt +0 -26
  213. package/android/src/main/java/expo/modules/kotlin/methods/Method.kt +0 -14
  214. package/android/src/main/java/expo/modules/kotlin/methods/PromiseMethod.kt +0 -15
  215. package/ios/JSI/ExpoModulesProxySpec.h +0 -24
  216. package/ios/JSI/ExpoModulesProxySpec.mm +0 -135
  217. package/ios/JSI/JSIInstaller.mm +0 -22
@@ -0,0 +1,384 @@
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
+ @available(*, deprecated, renamed: "asyncFunction")
26
+ public func function<R>(
27
+ _ name: String,
28
+ _ closure: @escaping () throws -> R
29
+ ) -> AnyFunction {
30
+ return ConcreteFunction(
31
+ name,
32
+ argTypes: [],
33
+ closure
34
+ )
35
+ }
36
+
37
+ /**
38
+ Function with one argument.
39
+ */
40
+ @available(*, deprecated, renamed: "asyncFunction")
41
+ public func function<R, A0: AnyArgument>(
42
+ _ name: String,
43
+ _ closure: @escaping (A0) throws -> R
44
+ ) -> AnyFunction {
45
+ return ConcreteFunction(
46
+ name,
47
+ argTypes: [ArgumentType(A0.self)],
48
+ closure
49
+ )
50
+ }
51
+
52
+ /**
53
+ Function with two arguments.
54
+ */
55
+ @available(*, deprecated, renamed: "asyncFunction")
56
+ public func function<R, A0: AnyArgument, A1: AnyArgument>(
57
+ _ name: String,
58
+ _ closure: @escaping (A0, A1) throws -> R
59
+ ) -> AnyFunction {
60
+ return ConcreteFunction(
61
+ name,
62
+ argTypes: [ArgumentType(A0.self), ArgumentType(A1.self)],
63
+ closure
64
+ )
65
+ }
66
+
67
+ /**
68
+ Function with three arguments.
69
+ */
70
+ @available(*, deprecated, renamed: "asyncFunction")
71
+ public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument>(
72
+ _ name: String,
73
+ _ closure: @escaping (A0, A1, A2) throws -> R
74
+ ) -> AnyFunction {
75
+ return ConcreteFunction(
76
+ name,
77
+ argTypes: [
78
+ ArgumentType(A0.self),
79
+ ArgumentType(A1.self),
80
+ ArgumentType(A2.self)
81
+ ],
82
+ closure
83
+ )
84
+ }
85
+
86
+ /**
87
+ Function with four arguments.
88
+ */
89
+ @available(*, deprecated, renamed: "asyncFunction")
90
+ public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument>(
91
+ _ name: String,
92
+ _ closure: @escaping (A0, A1, A2, A3) throws -> R
93
+ ) -> AnyFunction {
94
+ return ConcreteFunction(
95
+ name,
96
+ argTypes: [
97
+ ArgumentType(A0.self),
98
+ ArgumentType(A1.self),
99
+ ArgumentType(A2.self),
100
+ ArgumentType(A3.self)
101
+ ],
102
+ closure
103
+ )
104
+ }
105
+
106
+ /**
107
+ Function with five arguments.
108
+ */
109
+ @available(*, deprecated, renamed: "asyncFunction")
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) throws -> R
113
+ ) -> AnyFunction {
114
+ return ConcreteFunction(
115
+ name,
116
+ argTypes: [
117
+ ArgumentType(A0.self),
118
+ ArgumentType(A1.self),
119
+ ArgumentType(A2.self),
120
+ ArgumentType(A3.self),
121
+ ArgumentType(A4.self)
122
+ ],
123
+ closure
124
+ )
125
+ }
126
+
127
+ /**
128
+ Function with six arguments.
129
+ */
130
+ @available(*, deprecated, renamed: "asyncFunction")
131
+ public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument, A5: AnyArgument>(
132
+ _ name: String,
133
+ _ closure: @escaping (A0, A1, A2, A3, A4, A5) throws -> R
134
+ ) -> AnyFunction {
135
+ return ConcreteFunction(
136
+ name,
137
+ argTypes: [
138
+ ArgumentType(A0.self),
139
+ ArgumentType(A1.self),
140
+ ArgumentType(A2.self),
141
+ ArgumentType(A3.self),
142
+ ArgumentType(A4.self),
143
+ ArgumentType(A5.self)
144
+ ],
145
+ closure
146
+ )
147
+ }
148
+
149
+ /**
150
+ Function with seven arguments.
151
+ */
152
+ @available(*, deprecated, renamed: "asyncFunction")
153
+ public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument, A5: AnyArgument, A6: AnyArgument>(
154
+ _ name: String,
155
+ _ closure: @escaping (A0, A1, A2, A3, A4, A5, A6) throws -> R
156
+ ) -> AnyFunction {
157
+ return ConcreteFunction(
158
+ name,
159
+ argTypes: [
160
+ ArgumentType(A0.self),
161
+ ArgumentType(A1.self),
162
+ ArgumentType(A2.self),
163
+ ArgumentType(A3.self),
164
+ ArgumentType(A4.self),
165
+ ArgumentType(A5.self),
166
+ ArgumentType(A6.self)
167
+ ],
168
+ closure
169
+ )
170
+ }
171
+
172
+ /**
173
+ Function with eight 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, A7: AnyArgument>(
177
+ _ name: String,
178
+ _ closure: @escaping (A0, A1, A2, A3, A4, A5, A6, A7) throws -> R
179
+ ) -> AnyFunction {
180
+ return ConcreteFunction(
181
+ name,
182
+ argTypes: [
183
+ ArgumentType(A0.self),
184
+ ArgumentType(A1.self),
185
+ ArgumentType(A2.self),
186
+ ArgumentType(A3.self),
187
+ ArgumentType(A4.self),
188
+ ArgumentType(A5.self),
189
+ ArgumentType(A6.self),
190
+ ArgumentType(A7.self)
191
+ ],
192
+ closure
193
+ )
194
+ }
195
+
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
+ }
296
+
297
+ /**
298
+ Asynchronous function with six arguments.
299
+ */
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
+ )
316
+ }
317
+
318
+ /**
319
+ Asynchronous function with seven arguments.
320
+ */
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
+ )
338
+ }
339
+
340
+ /**
341
+ Asynchronous function with eight arguments.
342
+ */
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
+ )
361
+ }
362
+
363
+ // MARK: - Events
364
+
365
+ /**
366
+ Defines event names that the object can send to JavaScript.
367
+ */
368
+ public func events(_ names: String...) -> AnyDefinition {
369
+ return EventsDefinition(names: names)
370
+ }
371
+
372
+ /**
373
+ Function that is invoked when the first event listener is added.
374
+ */
375
+ public func onStartObserving(_ body: @escaping () -> Void) -> AnyFunction {
376
+ return ConcreteFunction("startObserving", argTypes: [], body)
377
+ }
378
+
379
+ /**
380
+ Function that is invoked when all event listeners are removed.
381
+ */
382
+ public func onStopObserving(_ body: @escaping () -> Void) -> AnyFunction {
383
+ return ConcreteFunction("stopObserving", argTypes: [], body)
384
+ }
@@ -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 {
@@ -9,9 +11,14 @@ public final class SwiftInteropBridge: NSObject {
9
11
  appContext.moduleRegistry
10
12
  }
11
13
 
14
+ internal init(appContext: AppContext) {
15
+ self.appContext = appContext
16
+ super.init()
17
+ }
18
+
12
19
  @objc
13
- public init(modulesProvider: ModulesProviderObjCProtocol, legacyModuleRegistry: EXModuleRegistry) {
14
- self.appContext = AppContext(withModulesProvider: modulesProvider as! ModulesProviderProtocol, legacyModuleRegistry: legacyModuleRegistry)
20
+ public init(modulesProvider: ModulesProvider, legacyModuleRegistry: EXModuleRegistry) {
21
+ self.appContext = AppContext(withModulesProvider: modulesProvider, legacyModuleRegistry: legacyModuleRegistry)
15
22
  super.init()
16
23
  }
17
24
 
@@ -21,11 +28,18 @@ public final class SwiftInteropBridge: NSObject {
21
28
  }
22
29
 
23
30
  @objc
24
- public func callFunction(_ functionName: String,
25
- onModule moduleName: String,
26
- withArgs args: [Any],
27
- resolve: @escaping EXPromiseResolveBlock,
28
- reject: @escaping EXPromiseRejectBlock) {
31
+ public func setReactBridge(_ reactBridge: RCTBridge) {
32
+ appContext.reactBridge = reactBridge
33
+ }
34
+
35
+ @objc
36
+ public func callFunction(
37
+ _ functionName: String,
38
+ onModule moduleName: String,
39
+ withArgs args: [Any],
40
+ resolve: @escaping EXPromiseResolveBlock,
41
+ reject: @escaping EXPromiseRejectBlock
42
+ ) {
29
43
  registry
30
44
  .get(moduleHolderForName: moduleName)?
31
45
  .call(function: functionName, args: args) { value, error in
@@ -40,9 +54,11 @@ public final class SwiftInteropBridge: NSObject {
40
54
  }
41
55
 
42
56
  @objc
43
- public func callFunctionSync(_ functionName: String,
44
- onModule moduleName: String,
45
- withArgs args: [Any]) -> Any? {
57
+ public func callFunctionSync(
58
+ _ functionName: String,
59
+ onModule moduleName: String,
60
+ withArgs args: [Any]
61
+ ) -> Any? {
46
62
  return registry
47
63
  .get(moduleHolderForName: moduleName)?
48
64
  .callSync(function: functionName, args: args)
@@ -53,11 +69,11 @@ public final class SwiftInteropBridge: NSObject {
53
69
  var constants = [String: [[String: Any]]]()
54
70
 
55
71
  for holder in registry {
56
- constants[holder.name] = holder.definition.functions.map({ (functionName, function) in
72
+ constants[holder.name] = holder.definition.functions.map({ functionName, function in
57
73
  return [
58
74
  "name": functionName,
59
75
  "argumentsCount": function.argumentsCount,
60
- "key": functionName,
76
+ "key": functionName
61
77
  ]
62
78
  })
63
79
  }
@@ -72,9 +88,13 @@ public final class SwiftInteropBridge: NSObject {
72
88
  }
73
89
 
74
90
  @objc
75
- public func exportedViewManagersNames() -> [String] {
76
- return registry.compactMap { holder in
77
- return holder.definition.viewManager != nil ? holder.name : nil
91
+ public func viewManagersMetadata() -> [String: Any] {
92
+ return registry.reduce(into: [String: Any]()) { acc, holder in
93
+ if let viewManager = holder.definition.viewManager {
94
+ acc[holder.name] = [
95
+ "propsNames": viewManager.props.map { $0.name }
96
+ ]
97
+ }
78
98
  }
79
99
  }
80
100
 
@@ -92,6 +112,24 @@ public final class SwiftInteropBridge: NSObject {
92
112
  }
93
113
  }
94
114
 
115
+ /**
116
+ Sets the JSI runtime on the operating `AppContext`.
117
+ */
118
+ @objc
119
+ public func setRuntime(_ runtime: JavaScriptRuntime?) {
120
+ appContext.runtime = runtime
121
+ }
122
+
123
+ @objc
124
+ public func getModuleNames() -> [String] {
125
+ return registry.getModuleNames()
126
+ }
127
+
128
+ @objc
129
+ public func getNativeModuleObject(_ moduleName: String) -> JavaScriptObject? {
130
+ return registry.get(moduleHolderForName: moduleName)?.javaScriptObject
131
+ }
132
+
95
133
  // MARK: - Events
96
134
 
97
135
  /**
@@ -3,7 +3,7 @@ import UIKit
3
3
  /**
4
4
  Type-erased protocol for view props classes.
5
5
  */
6
- public protocol AnyViewProp: AnyDefinition {
6
+ public protocol AnyViewProp: ViewManagerDefinitionComponent {
7
7
  /**
8
8
  Name of the view prop that JavaScript refers to.
9
9
  */