expo-modules-core 0.6.4 → 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 (159) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/README.md +1 -1
  3. package/android/build.gradle +5 -5
  4. package/android/src/main/java/expo/modules/kotlin/AppContext.kt +11 -1
  5. package/android/src/main/java/expo/modules/kotlin/DynamicExtenstions.kt +5 -3
  6. package/android/src/main/java/expo/modules/kotlin/KPromiseWrapper.kt +8 -2
  7. package/android/src/main/java/expo/modules/kotlin/KotlinInteropModuleRegistry.kt +13 -4
  8. package/android/src/main/java/expo/modules/kotlin/ModuleHolder.kt +7 -6
  9. package/android/src/main/java/expo/modules/kotlin/ModuleRegistry.kt +6 -1
  10. package/android/src/main/java/expo/modules/kotlin/Promise.kt +1 -1
  11. package/android/src/main/java/expo/modules/kotlin/callbacks/Callback.kt +5 -0
  12. package/android/src/main/java/expo/modules/kotlin/callbacks/ViewCallback.kt +28 -0
  13. package/android/src/main/java/expo/modules/kotlin/callbacks/ViewCallbackDelegate.kt +27 -0
  14. package/android/src/main/java/expo/modules/kotlin/defaultmodules/ErrorManagerModule.kt +25 -0
  15. package/android/src/main/java/expo/modules/kotlin/exception/CodedException.kt +68 -8
  16. package/android/src/main/java/expo/modules/kotlin/exception/ExceptionDecorator.kt +11 -0
  17. package/android/src/main/java/expo/modules/kotlin/methods/AnyMethod.kt +13 -14
  18. package/android/src/main/java/expo/modules/kotlin/modules/DefinitionMarker.kt +4 -0
  19. package/android/src/main/java/expo/modules/kotlin/modules/Module.kt +3 -2
  20. package/android/src/main/java/expo/modules/kotlin/modules/ModuleDefinitionBuilder.kt +44 -4
  21. package/android/src/main/java/expo/modules/kotlin/records/Record.kt +39 -0
  22. package/android/src/main/java/expo/modules/kotlin/records/RecordTypeConverter.kt +14 -7
  23. package/android/src/main/java/expo/modules/kotlin/types/ArrayTypeConverter.kt +11 -5
  24. package/android/src/main/java/expo/modules/kotlin/types/ListTypeConverter.kt +10 -4
  25. package/android/src/main/java/expo/modules/kotlin/types/MapTypeConverter.kt +12 -6
  26. package/android/src/main/java/expo/modules/kotlin/types/PairTypeConverter.kt +29 -13
  27. package/android/src/main/java/expo/modules/kotlin/types/TypeConverter.kt +2 -1
  28. package/android/src/main/java/expo/modules/kotlin/views/CallbacksDefinition.kt +3 -0
  29. package/android/src/main/java/expo/modules/kotlin/views/GroupViewManagerWrapper.kt +22 -0
  30. package/android/src/main/java/expo/modules/kotlin/views/SimpleViewManagerWrapper.kt +22 -0
  31. package/android/src/main/java/expo/modules/kotlin/views/ViewManagerDefinition.kt +27 -2
  32. package/android/src/main/java/expo/modules/kotlin/views/ViewManagerDefinitionBuilder.kt +29 -1
  33. package/android/src/main/java/expo/modules/kotlin/views/ViewManagerWrapperDelegate.kt +59 -2
  34. package/build/EventEmitter.d.ts +1 -0
  35. package/build/EventEmitter.d.ts.map +1 -0
  36. package/build/NativeModulesProxy.d.ts +1 -0
  37. package/build/NativeModulesProxy.d.ts.map +1 -0
  38. package/build/NativeModulesProxy.native.d.ts +1 -0
  39. package/build/NativeModulesProxy.native.d.ts.map +1 -0
  40. package/build/NativeModulesProxy.types.d.ts +1 -0
  41. package/build/NativeModulesProxy.types.d.ts.map +1 -0
  42. package/build/NativeViewManagerAdapter.d.ts +1 -0
  43. package/build/NativeViewManagerAdapter.d.ts.map +1 -0
  44. package/build/NativeViewManagerAdapter.native.d.ts +1 -0
  45. package/build/NativeViewManagerAdapter.native.d.ts.map +1 -0
  46. package/build/PermissionsHook.d.ts +1 -0
  47. package/build/PermissionsHook.d.ts.map +1 -0
  48. package/build/PermissionsInterface.d.ts +1 -0
  49. package/build/PermissionsInterface.d.ts.map +1 -0
  50. package/build/Platform.d.ts +1 -0
  51. package/build/Platform.d.ts.map +1 -0
  52. package/build/SyntheticPlatformEmitter.d.ts +1 -0
  53. package/build/SyntheticPlatformEmitter.d.ts.map +1 -0
  54. package/build/SyntheticPlatformEmitter.web.d.ts +1 -0
  55. package/build/SyntheticPlatformEmitter.web.d.ts.map +1 -0
  56. package/build/deprecate.d.ts +1 -0
  57. package/build/deprecate.d.ts.map +1 -0
  58. package/build/environment/browser.d.ts +1 -0
  59. package/build/environment/browser.d.ts.map +1 -0
  60. package/build/environment/browser.web.d.ts +1 -0
  61. package/build/environment/browser.web.d.ts.map +1 -0
  62. package/build/errors/CodedError.d.ts +1 -0
  63. package/build/errors/CodedError.d.ts.map +1 -0
  64. package/build/errors/UnavailabilityError.d.ts +1 -0
  65. package/build/errors/UnavailabilityError.d.ts.map +1 -0
  66. package/build/index.d.ts +3 -0
  67. package/build/index.d.ts.map +1 -0
  68. package/build/index.js +2 -0
  69. package/build/index.js.map +1 -1
  70. package/build/requireNativeModule.d.ts +16 -0
  71. package/build/requireNativeModule.d.ts.map +1 -0
  72. package/build/requireNativeModule.js +18 -0
  73. package/build/requireNativeModule.js.map +1 -0
  74. package/build/sweet/NativeErrorManager.d.ts +3 -0
  75. package/build/sweet/NativeErrorManager.d.ts.map +1 -0
  76. package/build/sweet/NativeErrorManager.js +3 -0
  77. package/build/sweet/NativeErrorManager.js.map +1 -0
  78. package/build/sweet/setUpErrorManager.fx.d.ts +2 -0
  79. package/build/sweet/setUpErrorManager.fx.d.ts.map +1 -0
  80. package/build/sweet/setUpErrorManager.fx.js +11 -0
  81. package/build/sweet/setUpErrorManager.fx.js.map +1 -0
  82. package/ios/AppDelegates/ExpoAppDelegate.swift +18 -10
  83. package/ios/JSI/ExpoModulesHostObject.h +33 -0
  84. package/ios/JSI/ExpoModulesHostObject.mm +40 -0
  85. package/ios/JSI/ExpoModulesProxySpec.h +4 -0
  86. package/ios/JSI/ExpoModulesProxySpec.mm +1 -3
  87. package/ios/JSI/JSIConversions.h +2 -0
  88. package/ios/JSI/JSIConversions.mm +9 -0
  89. package/ios/JSI/JSIInstaller.h +10 -0
  90. package/ios/JSI/JSIInstaller.mm +14 -2
  91. package/ios/JSI/JavaScriptObject.h +60 -0
  92. package/ios/JSI/JavaScriptObject.mm +93 -0
  93. package/ios/JSI/JavaScriptRuntime.h +54 -0
  94. package/ios/JSI/JavaScriptRuntime.mm +102 -0
  95. package/ios/ModuleRegistryAdapter/EXModuleRegistryAdapter.m +2 -12
  96. package/ios/NativeModulesProxy/EXComponentDataCompatibleWrapper.h +16 -0
  97. package/ios/NativeModulesProxy/EXComponentDataCompatibleWrapper.m +28 -0
  98. package/ios/NativeModulesProxy/EXNativeModulesProxy.mm +90 -66
  99. package/ios/RCTComponentData+Privates.h +12 -0
  100. package/ios/ReactDelegates/ExpoReactDelegate.swift +2 -2
  101. package/ios/ReactDelegates/ExpoReactDelegateHandler.swift +3 -3
  102. package/ios/ReactDelegates/ModulePriorities.swift +1 -1
  103. package/ios/Swift/AppContext.swift +38 -4
  104. package/ios/Swift/Arguments/ArgumentType.swift +4 -0
  105. package/ios/Swift/Arguments/Convertibles.swift +13 -13
  106. package/ios/Swift/Arguments/Types/EnumArgumentType.swift +11 -17
  107. package/ios/Swift/Arguments/Types/PromiseArgumentType.swift +1 -1
  108. package/ios/Swift/Arguments/Types/RawArgumentType.swift +2 -2
  109. package/ios/Swift/Conversions.swift +51 -56
  110. package/ios/Swift/EventListener.swift +8 -10
  111. package/ios/Swift/Events/Callback.swift +66 -0
  112. package/ios/Swift/Events/Event.swift +43 -0
  113. package/ios/Swift/Exceptions/ChainableException.swift +51 -0
  114. package/ios/Swift/{CodedError.swift → Exceptions/CodedError.swift} +1 -12
  115. package/ios/Swift/Exceptions/Exception.swift +62 -0
  116. package/ios/Swift/Exceptions/ExceptionOrigin.swift +28 -0
  117. package/ios/Swift/Exceptions/GenericException.swift +20 -0
  118. package/ios/Swift/Exceptions/UnexpectedException.swift +16 -0
  119. package/ios/Swift/Functions/AnyFunction.swift +11 -1
  120. package/ios/Swift/Functions/ConcreteFunction.swift +37 -16
  121. package/ios/Swift/JavaScriptUtils.swift +43 -0
  122. package/ios/Swift/ModuleHolder.swift +53 -14
  123. package/ios/Swift/ModuleRegistry.swift +4 -1
  124. package/ios/Swift/Modules/AnyModule.swift +0 -1
  125. package/ios/Swift/Modules/ModuleDefinition.swift +4 -13
  126. package/ios/Swift/Modules/ModuleDefinitionBuilder.swift +0 -1
  127. package/ios/Swift/Modules/ModuleDefinitionComponents.swift +0 -188
  128. package/ios/Swift/ModulesProvider.swift +0 -1
  129. package/ios/Swift/Objects/ObjectDefinition.swift +30 -0
  130. package/ios/Swift/Objects/ObjectDefinitionComponents.swift +208 -0
  131. package/ios/Swift/Promise.swift +8 -3
  132. package/ios/Swift/Records/AnyField.swift +7 -0
  133. package/ios/Swift/Records/Field.swift +24 -19
  134. package/ios/Swift/Records/FieldOption.swift +1 -1
  135. package/ios/Swift/Records/Record.swift +12 -4
  136. package/ios/Swift/SwiftInteropBridge.swift +39 -10
  137. package/ios/Swift/Views/AnyViewProp.swift +1 -1
  138. package/ios/Swift/Views/ComponentData.swift +95 -0
  139. package/ios/Swift/Views/ConcreteViewProp.swift +6 -8
  140. package/ios/Swift/Views/ViewFactory.swift +1 -1
  141. package/ios/Swift/Views/ViewManagerDefinition.swift +23 -2
  142. package/ios/Swift/Views/ViewManagerDefinitionBuilder.swift +0 -1
  143. package/ios/Swift/Views/ViewManagerDefinitionComponents.swift +26 -0
  144. package/ios/Swift/Views/ViewModuleWrapper.swift +5 -2
  145. package/ios/Tests/ArgumentTypeSpec.swift +3 -4
  146. package/ios/Tests/ConstantsSpec.swift +4 -4
  147. package/ios/Tests/ConvertiblesSpec.swift +33 -33
  148. package/ios/Tests/ExceptionsSpec.swift +112 -0
  149. package/ios/Tests/FunctionSpec.swift +20 -22
  150. package/ios/Tests/FunctionWithConvertiblesSpec.swift +2 -2
  151. package/ios/Tests/Mocks/ModuleMocks.swift +1 -1
  152. package/ios/Tests/Mocks/ModulesProviderMock.swift +0 -1
  153. package/ios/Tests/ModuleEventListenersSpec.swift +1 -1
  154. package/ios/Tests/RecordSpec.swift +7 -17
  155. package/package.json +3 -3
  156. package/src/index.ts +4 -0
  157. package/src/requireNativeModule.ts +29 -0
  158. package/src/sweet/NativeErrorManager.ts +2 -0
  159. package/src/sweet/setUpErrorManager.fx.ts +12 -0
@@ -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
  /**
@@ -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
  */
@@ -0,0 +1,95 @@
1
+ // Copyright 2021-present 650 Industries. All rights reserved.
2
+
3
+
4
+ /**
5
+ Custom component data extending `RCTComponentData`. Its main purpose is to handle event-based props (callbacks),
6
+ but it also simplifies capturing the view config so we can omit some reflections that React Native executes.
7
+ */
8
+ @objc(EXComponentData)
9
+ public final class ComponentData: EXComponentDataCompatibleWrapper {
10
+ /**
11
+ Weak pointer to the holder of a module that the component data was created for.
12
+ */
13
+ weak var moduleHolder: ModuleHolder?
14
+
15
+ /**
16
+ Initializer that additionally takes the original view module to have access to its definition.
17
+ */
18
+ @objc
19
+ public init(viewModule: ViewModuleWrapper, managerClass: ViewModuleWrapper.Type, bridge: RCTBridge) {
20
+ self.moduleHolder = viewModule.wrappedModuleHolder
21
+ super.init(managerClass: managerClass, bridge: bridge, eventDispatcher: bridge.eventDispatcher())
22
+ }
23
+
24
+ // MARK: RCTComponentData
25
+
26
+ /**
27
+ Creates a setter for the specific prop. For non-event props we just let React Native do its job.
28
+ Events are handled differently to conveniently use them in Swift.
29
+ */
30
+ public override func createPropBlock(_ propName: String, isShadowView: Bool) -> RCTPropBlockAlias {
31
+ // Expo Modules Core doesn't support shadow views yet, so fall back to the default implementation.
32
+ if isShadowView {
33
+ return super.createPropBlock(propName, isShadowView: isShadowView)
34
+ }
35
+
36
+ // If the prop is defined as an event, create our own event setter.
37
+ if moduleHolder?.viewManager?.eventNames.contains(propName) == true {
38
+ return createEventSetter(eventName: propName, bridge: self.manager?.bridge)
39
+ }
40
+
41
+ // Otherwise also fall back to the default implementation.
42
+ return super.createPropBlock(propName, isShadowView: isShadowView)
43
+ }
44
+
45
+ /**
46
+ The base `RCTComponentData` class does some Objective-C dynamic calls in this function, but we don't
47
+ need to do these slow operations since the Sweet API gives us necessary details without reflections.
48
+ */
49
+ public override func viewConfig() -> [String: Any] {
50
+ var propTypes: [String: Any] = [:]
51
+ var directEvents: [String] = []
52
+ let superClass: AnyClass? = managerClass.superclass()
53
+
54
+ if let eventNames = moduleHolder?.viewManager?.eventNames {
55
+ for eventName in eventNames {
56
+ directEvents.append(RCTNormalizeInputEventName(eventName))
57
+ propTypes[eventName] = "BOOL"
58
+ }
59
+ }
60
+
61
+ return [
62
+ "propTypes": propTypes,
63
+ "directEvents": directEvents,
64
+ "bubblingEvents": [String](),
65
+ "baseModuleName": superClass?.moduleName() as Any
66
+ ]
67
+ }
68
+ }
69
+
70
+ /**
71
+ Creates a setter for the event prop.
72
+ */
73
+ private func createEventSetter(eventName: String, bridge: RCTBridge?) -> RCTPropBlockAlias {
74
+ return { [weak bridge] (target: RCTComponent, value: Any) in
75
+ // Find view's property that is named as the prop and is wrapped by `Event`.
76
+ let child = Mirror(reflecting: target).children.first {
77
+ $0.label == "_\(eventName)"
78
+ }
79
+ guard let event = child?.value as? AnyEventInternal else {
80
+ return
81
+ }
82
+
83
+ // For callbacks React Native passes a bool value whether the prop is specified or not.
84
+ if value as? Bool == true {
85
+ event.settle { [weak target] (body: Any) in
86
+ if let target = target {
87
+ let componentEvent = RCTComponentEvent(name: eventName, viewTag: target.reactTag, body: ["payload": body])
88
+ bridge?.eventDispatcher().send(componentEvent)
89
+ }
90
+ }
91
+ } else {
92
+ event.invalidate()
93
+ }
94
+ }
95
+ }
@@ -36,22 +36,20 @@ public final class ConcreteViewProp<ViewType: UIView, PropType: AnyArgument>: An
36
36
  // Method's signature must be type-erased to conform to `AnyViewProp` protocol.
37
37
  // Given view must be castable to the generic `ViewType` type.
38
38
  guard let view = view as? ViewType else {
39
- throw IncompatibleViewError(propName: name, viewType: ViewType.self)
39
+ throw IncompatibleViewException((propName: name, viewType: ViewType.self))
40
40
  }
41
41
  guard let value = try propType.cast(value) as? PropType else {
42
- throw Conversions.CastingError<PropType>(value: value)
42
+ throw Conversions.CastingException<PropType>(value)
43
43
  }
44
44
  setter(view, value)
45
45
  }
46
46
  }
47
47
 
48
48
  /**
49
- An error that is thrown when the view passed to prop's setter doesn't match the type in setter's definition.
49
+ An exception that is thrown when the view passed to prop's setter doesn't match the type in setter's definition.
50
50
  */
51
- internal struct IncompatibleViewError: CodedError {
52
- let propName: String
53
- let viewType: UIView.Type
54
- var description: String {
55
- "Tried to set prop `\(propName)` on the view that isn't `\(viewType)`"
51
+ internal class IncompatibleViewException: GenericException<(propName: String, viewType: UIView.Type)> {
52
+ override var reason: String {
53
+ "Tried to set prop '\(param.propName)' on the view that isn't \(param.viewType)"
56
54
  }
57
55
  }
@@ -3,7 +3,7 @@ import UIKit
3
3
  /**
4
4
  A definition of the view factory that creates views.
5
5
  */
6
- internal struct ViewFactory: AnyDefinition {
6
+ internal struct ViewFactory: ViewManagerDefinitionComponent {
7
7
  typealias FactoryClosureType = () -> UIView
8
8
 
9
9
  let factory: FactoryClosureType
@@ -3,7 +3,7 @@ import UIKit
3
3
  /**
4
4
  The definition of the view manager. It's part of the module definition to scope only view-related definitions.
5
5
  */
6
- public struct ViewManagerDefinition: AnyDefinition {
6
+ public final class ViewManagerDefinition: ObjectDefinition {
7
7
  /**
8
8
  The view factory that lets us create views.
9
9
  */
@@ -14,13 +14,29 @@ public struct ViewManagerDefinition: AnyDefinition {
14
14
  */
15
15
  let props: [AnyViewProp]
16
16
 
17
- init(definitions: [AnyDefinition]) {
17
+ /**
18
+ Names of the events that the view can send to JavaScript.
19
+ */
20
+ let eventNames: [String]
21
+
22
+ /**
23
+ Default initializer receiving children definitions from the result builder.
24
+ */
25
+ override init(definitions: [AnyDefinition]) {
18
26
  self.factory = definitions
19
27
  .compactMap { $0 as? ViewFactory }
20
28
  .last
21
29
 
22
30
  self.props = definitions
23
31
  .compactMap { $0 as? AnyViewProp }
32
+
33
+ self.eventNames = Array(
34
+ definitions
35
+ .compactMap { ($0 as? EventsDefinition)?.names }
36
+ .joined()
37
+ )
38
+
39
+ super.init(definitions: definitions)
24
40
  }
25
41
 
26
42
  /**
@@ -39,3 +55,8 @@ public struct ViewManagerDefinition: AnyDefinition {
39
55
  }
40
56
  }
41
57
  }
58
+
59
+ /**
60
+ The protocol for definition components that can only be handled by the view manager builder.
61
+ */
62
+ public protocol ViewManagerDefinitionComponent: AnyDefinition {}
@@ -1,4 +1,3 @@
1
-
2
1
  #if swift(>=5.4)
3
2
  /**
4
3
  A result builder that captures scoped definitions specific for view managers, such as `ViewFactory` and `ConcreteViewProp`.