expo-modules-core 0.6.5 → 0.9.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 (219) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/README.md +1 -1
  3. package/android/ExpoModulesCorePlugin.gradle +15 -0
  4. package/android/build.gradle +31 -15
  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 +30 -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 +170 -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 +416 -43
  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 +114 -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 +81 -2
  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 +57 -4
  146. package/ios/Swift/Arguments/AnyArgumentType.swift +1 -1
  147. package/ios/Swift/Arguments/ArgumentType.swift +4 -0
  148. package/ios/Swift/Arguments/Convertibles.swift +13 -13
  149. package/ios/Swift/Arguments/Types/EnumArgumentType.swift +11 -17
  150. package/ios/Swift/Arguments/Types/PromiseArgumentType.swift +1 -1
  151. package/ios/Swift/Arguments/Types/RawArgumentType.swift +2 -2
  152. package/ios/Swift/Conversions.swift +51 -56
  153. package/ios/Swift/EventListener.swift +8 -10
  154. package/ios/Swift/Events/Callback.swift +66 -0
  155. package/ios/Swift/Events/Event.swift +43 -0
  156. package/ios/Swift/Exceptions/ChainableException.swift +51 -0
  157. package/ios/Swift/{CodedError.swift → Exceptions/CodedError.swift} +1 -12
  158. package/ios/Swift/Exceptions/Exception.swift +62 -0
  159. package/ios/Swift/Exceptions/ExceptionOrigin.swift +28 -0
  160. package/ios/Swift/Exceptions/GenericException.swift +20 -0
  161. package/ios/Swift/Exceptions/UnexpectedException.swift +16 -0
  162. package/ios/Swift/Functions/AnyFunction.swift +16 -1
  163. package/ios/Swift/Functions/AsyncFunctionComponent.swift +182 -0
  164. package/ios/Swift/Functions/ConcreteFunction.swift +52 -59
  165. package/ios/Swift/Functions/SyncFunctionComponent.swift +181 -0
  166. package/ios/Swift/JavaScriptUtils.swift +99 -0
  167. package/ios/Swift/ModuleHolder.swift +69 -18
  168. package/ios/Swift/ModuleRegistry.swift +4 -1
  169. package/ios/Swift/Modules/AnyModule.swift +0 -1
  170. package/ios/Swift/Modules/ModuleDefinition.swift +4 -13
  171. package/ios/Swift/Modules/ModuleDefinitionBuilder.swift +0 -1
  172. package/ios/Swift/Modules/ModuleDefinitionComponents.swift +54 -220
  173. package/ios/Swift/ModulesProvider.swift +3 -11
  174. package/ios/Swift/Objects/ObjectDefinition.swift +30 -0
  175. package/ios/Swift/Objects/ObjectDefinitionComponents.swift +257 -0
  176. package/ios/Swift/Promise.swift +8 -3
  177. package/ios/Swift/Records/AnyField.swift +7 -0
  178. package/ios/Swift/Records/Field.swift +24 -19
  179. package/ios/Swift/Records/FieldOption.swift +1 -1
  180. package/ios/Swift/Records/Record.swift +12 -4
  181. package/ios/Swift/SwiftInteropBridge.swift +53 -15
  182. package/ios/Swift/Views/AnyViewProp.swift +1 -1
  183. package/ios/Swift/Views/ComponentData.swift +96 -0
  184. package/ios/Swift/Views/ConcreteViewProp.swift +6 -8
  185. package/ios/Swift/Views/ExpoView.swift +8 -0
  186. package/ios/Swift/Views/ViewFactory.swift +1 -1
  187. package/ios/Swift/Views/ViewManagerDefinition.swift +23 -2
  188. package/ios/Swift/Views/ViewManagerDefinitionBuilder.swift +0 -1
  189. package/ios/Swift/Views/ViewManagerDefinitionComponents.swift +49 -0
  190. package/ios/Swift/Views/ViewModuleWrapper.swift +5 -2
  191. package/ios/Swift.h +5 -0
  192. package/ios/Tests/ArgumentTypeSpec.swift +5 -7
  193. package/ios/Tests/ConstantsSpec.swift +6 -7
  194. package/ios/Tests/ConvertiblesSpec.swift +35 -36
  195. package/ios/Tests/ExceptionsSpec.swift +111 -0
  196. package/ios/Tests/ExpoModulesSpec.swift +75 -0
  197. package/ios/Tests/FunctionSpec.swift +21 -25
  198. package/ios/Tests/FunctionWithConvertiblesSpec.swift +4 -5
  199. package/ios/Tests/JavaScriptObjectSpec.swift +97 -0
  200. package/ios/Tests/JavaScriptRuntimeSpec.swift +94 -0
  201. package/ios/Tests/Mocks/ModuleMocks.swift +1 -1
  202. package/ios/Tests/Mocks/ModulesProviderMock.swift +0 -1
  203. package/ios/Tests/ModuleEventListenersSpec.swift +16 -17
  204. package/ios/Tests/ModuleRegistrySpec.swift +2 -3
  205. package/ios/Tests/RecordSpec.swift +9 -20
  206. package/package.json +3 -3
  207. package/src/NativeModulesProxy.native.ts +2 -22
  208. package/src/NativeModulesProxy.types.ts +0 -8
  209. package/src/NativeViewManagerAdapter.native.tsx +12 -28
  210. package/src/index.ts +4 -0
  211. package/src/requireNativeModule.ts +29 -0
  212. package/src/sweet/NativeErrorManager.ts +2 -0
  213. package/src/sweet/setUpErrorManager.fx.ts +12 -0
  214. package/android/src/main/java/expo/modules/kotlin/events/KEventEmitterWrapper.kt +0 -26
  215. package/android/src/main/java/expo/modules/kotlin/methods/Method.kt +0 -14
  216. package/android/src/main/java/expo/modules/kotlin/methods/PromiseMethod.kt +0 -15
  217. package/ios/JSI/ExpoModulesProxySpec.h +0 -24
  218. package/ios/JSI/ExpoModulesProxySpec.mm +0 -135
  219. package/ios/JSI/JSIInstaller.mm +0 -22
@@ -1,17 +1,9 @@
1
-
2
1
  import Foundation
3
2
 
4
3
  /**
5
- An empty protocol that Objective-C can refer to as a type of the modules provider.
6
- In Objective-C code we do not do anything with it except passing it back to the Swift light side.
7
- */
8
- @objc
9
- public protocol ModulesProviderObjCProtocol {}
10
-
11
- /**
12
- Swift protocol defining the requirements for modules providers. Extends its Objective-C counterpart `ModulesProviderObjCProtocol`.
4
+ Swift protocol defining the requirements for modules providers.
13
5
  */
14
- public protocol ModulesProviderProtocol: ModulesProviderObjCProtocol {
6
+ public protocol ModulesProviderProtocol {
15
7
  func getModuleClasses() -> [AnyModule.Type]
16
8
 
17
9
  /**
@@ -31,7 +23,7 @@ public protocol ModulesProviderProtocol: ModulesProviderObjCProtocol {
31
23
  The proper implementation is generated by autolinking as part of `pod install` command.
32
24
  */
33
25
  @objc
34
- open class ModulesProvider: NSObject, ModulesProviderProtocol, ModulesProviderObjCProtocol {
26
+ open class ModulesProvider: NSObject, ModulesProviderProtocol {
35
27
  open func getModuleClasses() -> [AnyModule.Type] {
36
28
  return []
37
29
  }
@@ -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,257 @@
1
+ /// This file implements definition components that are allowed in any object-based definition — `ObjectDefinition`.
2
+ /// So far only constants and functions belong to plain object.
3
+
4
+ // MARK: - Constants
5
+
6
+ /**
7
+ Definition function setting the module's constants to export.
8
+ */
9
+ @available(*, deprecated, renamed: "Constants")
10
+ public func constants(_ body: @escaping () -> [String: Any?]) -> AnyDefinition {
11
+ return ConstantsDefinition(body: body)
12
+ }
13
+
14
+ /**
15
+ Definition function setting the module's constants to export.
16
+ */
17
+ public func Constants(_ body: @escaping () -> [String: Any?]) -> AnyDefinition {
18
+ return ConstantsDefinition(body: body)
19
+ }
20
+
21
+ /**
22
+ Definition function setting the module's constants to export.
23
+ */
24
+ @available(*, deprecated, renamed: "Constants")
25
+ public func constants(_ body: @autoclosure @escaping () -> [String: Any?]) -> AnyDefinition {
26
+ return ConstantsDefinition(body: body)
27
+ }
28
+
29
+ /**
30
+ Definition function setting the module's constants to export.
31
+ */
32
+ public func Constants(_ body: @autoclosure @escaping () -> [String: Any?]) -> AnyDefinition {
33
+ return ConstantsDefinition(body: body)
34
+ }
35
+
36
+ // MARK: - Functions
37
+
38
+ /**
39
+ Function without arguments.
40
+ */
41
+ @available(*, deprecated, renamed: "AsyncFunction")
42
+ public func function<R>(
43
+ _ name: String,
44
+ _ closure: @escaping () throws -> R
45
+ ) -> AnyFunction {
46
+ return ConcreteFunction(
47
+ name,
48
+ argTypes: [],
49
+ closure
50
+ )
51
+ }
52
+
53
+ /**
54
+ Function with one argument.
55
+ */
56
+ @available(*, deprecated, renamed: "AsyncFunction")
57
+ public func function<R, A0: AnyArgument>(
58
+ _ name: String,
59
+ _ closure: @escaping (A0) throws -> R
60
+ ) -> AnyFunction {
61
+ return ConcreteFunction(
62
+ name,
63
+ argTypes: [ArgumentType(A0.self)],
64
+ closure
65
+ )
66
+ }
67
+
68
+ /**
69
+ Function with two arguments.
70
+ */
71
+ @available(*, deprecated, renamed: "AsyncFunction")
72
+ public func function<R, A0: AnyArgument, A1: AnyArgument>(
73
+ _ name: String,
74
+ _ closure: @escaping (A0, A1) throws -> R
75
+ ) -> AnyFunction {
76
+ return ConcreteFunction(
77
+ name,
78
+ argTypes: [ArgumentType(A0.self), ArgumentType(A1.self)],
79
+ closure
80
+ )
81
+ }
82
+
83
+ /**
84
+ Function with three arguments.
85
+ */
86
+ @available(*, deprecated, renamed: "AsyncFunction")
87
+ public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument>(
88
+ _ name: String,
89
+ _ closure: @escaping (A0, A1, A2) throws -> R
90
+ ) -> AnyFunction {
91
+ return ConcreteFunction(
92
+ name,
93
+ argTypes: [
94
+ ArgumentType(A0.self),
95
+ ArgumentType(A1.self),
96
+ ArgumentType(A2.self)
97
+ ],
98
+ closure
99
+ )
100
+ }
101
+
102
+ /**
103
+ Function with four arguments.
104
+ */
105
+ @available(*, deprecated, renamed: "AsyncFunction")
106
+ public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument>(
107
+ _ name: String,
108
+ _ closure: @escaping (A0, A1, A2, A3) throws -> R
109
+ ) -> AnyFunction {
110
+ return ConcreteFunction(
111
+ name,
112
+ argTypes: [
113
+ ArgumentType(A0.self),
114
+ ArgumentType(A1.self),
115
+ ArgumentType(A2.self),
116
+ ArgumentType(A3.self)
117
+ ],
118
+ closure
119
+ )
120
+ }
121
+
122
+ /**
123
+ Function with five arguments.
124
+ */
125
+ @available(*, deprecated, renamed: "AsyncFunction")
126
+ public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument>(
127
+ _ name: String,
128
+ _ closure: @escaping (A0, A1, A2, A3, A4) throws -> R
129
+ ) -> AnyFunction {
130
+ return ConcreteFunction(
131
+ name,
132
+ argTypes: [
133
+ ArgumentType(A0.self),
134
+ ArgumentType(A1.self),
135
+ ArgumentType(A2.self),
136
+ ArgumentType(A3.self),
137
+ ArgumentType(A4.self)
138
+ ],
139
+ closure
140
+ )
141
+ }
142
+
143
+ /**
144
+ Function with six arguments.
145
+ */
146
+ @available(*, deprecated, renamed: "AsyncFunction")
147
+ public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument, A5: AnyArgument>(
148
+ _ name: String,
149
+ _ closure: @escaping (A0, A1, A2, A3, A4, A5) throws -> R
150
+ ) -> AnyFunction {
151
+ return ConcreteFunction(
152
+ name,
153
+ argTypes: [
154
+ ArgumentType(A0.self),
155
+ ArgumentType(A1.self),
156
+ ArgumentType(A2.self),
157
+ ArgumentType(A3.self),
158
+ ArgumentType(A4.self),
159
+ ArgumentType(A5.self)
160
+ ],
161
+ closure
162
+ )
163
+ }
164
+
165
+ /**
166
+ Function with seven arguments.
167
+ */
168
+ @available(*, deprecated, renamed: "AsyncFunction")
169
+ public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument, A5: AnyArgument, A6: AnyArgument>(
170
+ _ name: String,
171
+ _ closure: @escaping (A0, A1, A2, A3, A4, A5, A6) throws -> R
172
+ ) -> AnyFunction {
173
+ return ConcreteFunction(
174
+ name,
175
+ argTypes: [
176
+ ArgumentType(A0.self),
177
+ ArgumentType(A1.self),
178
+ ArgumentType(A2.self),
179
+ ArgumentType(A3.self),
180
+ ArgumentType(A4.self),
181
+ ArgumentType(A5.self),
182
+ ArgumentType(A6.self)
183
+ ],
184
+ closure
185
+ )
186
+ }
187
+
188
+ /**
189
+ Function with eight arguments.
190
+ */
191
+ @available(*, deprecated, renamed: "AsyncFunction")
192
+ public func function<R, A0: AnyArgument, A1: AnyArgument, A2: AnyArgument, A3: AnyArgument, A4: AnyArgument, A5: AnyArgument, A6: AnyArgument, A7: AnyArgument>(
193
+ _ name: String,
194
+ _ closure: @escaping (A0, A1, A2, A3, A4, A5, A6, A7) throws -> R
195
+ ) -> AnyFunction {
196
+ return ConcreteFunction(
197
+ name,
198
+ argTypes: [
199
+ ArgumentType(A0.self),
200
+ ArgumentType(A1.self),
201
+ ArgumentType(A2.self),
202
+ ArgumentType(A3.self),
203
+ ArgumentType(A4.self),
204
+ ArgumentType(A5.self),
205
+ ArgumentType(A6.self),
206
+ ArgumentType(A7.self)
207
+ ],
208
+ closure
209
+ )
210
+ }
211
+
212
+ // MARK: - Events
213
+
214
+ /**
215
+ Defines event names that the object can send to JavaScript.
216
+ */
217
+ @available(*, deprecated, renamed: "Events")
218
+ public func events(_ names: String...) -> AnyDefinition {
219
+ return EventsDefinition(names: names)
220
+ }
221
+
222
+ /**
223
+ Defines event names that the object can send to JavaScript.
224
+ */
225
+ public func Events(_ names: String...) -> AnyDefinition {
226
+ return EventsDefinition(names: names)
227
+ }
228
+
229
+ /**
230
+ Function that is invoked when the first event listener is added.
231
+ */
232
+ @available(*, deprecated, renamed: "OnStartObserving")
233
+ public func onStartObserving(_ body: @escaping () -> Void) -> AnyFunction {
234
+ return ConcreteFunction("startObserving", argTypes: [], body)
235
+ }
236
+
237
+ /**
238
+ Function that is invoked when the first event listener is added.
239
+ */
240
+ public func OnStartObserving(_ body: @escaping () -> Void) -> AnyFunction {
241
+ return ConcreteFunction("startObserving", argTypes: [], body)
242
+ }
243
+
244
+ /**
245
+ Function that is invoked when all event listeners are removed.
246
+ */
247
+ @available(*, deprecated, renamed: "OnStopObserving")
248
+ public func onStopObserving(_ body: @escaping () -> Void) -> AnyFunction {
249
+ return ConcreteFunction("stopObserving", argTypes: [], body)
250
+ }
251
+
252
+ /**
253
+ Function that is invoked when all event listeners are removed.
254
+ */
255
+ public func OnStopObserving(_ body: @escaping () -> Void) -> AnyFunction {
256
+ return ConcreteFunction("stopObserving", argTypes: [], body)
257
+ }
@@ -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
  */