expo-modules-core 0.4.8 → 0.5.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 (129) hide show
  1. package/CHANGELOG.md +14 -1
  2. package/android/build.gradle +30 -2
  3. package/android/src/main/java/expo/modules/adapters/react/ModuleRegistryAdapter.java +27 -5
  4. package/android/src/main/java/expo/modules/adapters/react/NativeModulesProxy.java +49 -5
  5. package/android/src/main/java/expo/modules/core/BasePackage.java +6 -0
  6. package/android/src/main/java/expo/modules/core/ModulePriorities.kt +25 -0
  7. package/android/src/main/java/expo/modules/core/interfaces/ActivityEventListener.java +3 -1
  8. package/android/src/main/java/expo/modules/core/interfaces/Package.java +4 -0
  9. package/android/src/main/java/expo/modules/core/interfaces/ReactActivityHandler.kt +18 -0
  10. package/android/src/main/java/expo/modules/core/interfaces/ReactNativeHostHandler.kt +14 -0
  11. package/android/src/main/java/expo/modules/core/utilities/KotlinUtilities.kt +23 -0
  12. package/android/src/main/java/expo/modules/kotlin/AppContext.kt +166 -0
  13. package/android/src/main/java/expo/modules/kotlin/DynamicExtenstions.kt +9 -0
  14. package/android/src/main/java/expo/modules/kotlin/ExpoModulesHelper.kt +18 -0
  15. package/android/src/main/java/expo/modules/kotlin/KPromiseWrapper.kt +23 -0
  16. package/android/src/main/java/expo/modules/kotlin/KotlinInteropModuleRegistry.kt +98 -0
  17. package/android/src/main/java/expo/modules/kotlin/ModuleHolder.kt +41 -0
  18. package/android/src/main/java/expo/modules/kotlin/ModuleRegistry.kt +56 -0
  19. package/android/src/main/java/expo/modules/kotlin/ModulesProvider.kt +7 -0
  20. package/android/src/main/java/expo/modules/kotlin/Promise.kt +13 -0
  21. package/android/src/main/java/expo/modules/kotlin/ReactLifecycleDelegate.kt +39 -0
  22. package/android/src/main/java/expo/modules/kotlin/ReadableArrayIterator.kt +14 -0
  23. package/android/src/main/java/expo/modules/kotlin/ReadableTypeExtensions.kt +18 -0
  24. package/android/src/main/java/expo/modules/kotlin/allocators/ObjectConstructor.kt +5 -0
  25. package/android/src/main/java/expo/modules/kotlin/allocators/ObjectConstructorFactory.kt +31 -0
  26. package/android/src/main/java/expo/modules/kotlin/allocators/UnsafeAllocator.kt +49 -0
  27. package/android/src/main/java/expo/modules/kotlin/events/EventListener.kt +39 -0
  28. package/android/src/main/java/expo/modules/kotlin/events/EventName.kt +31 -0
  29. package/android/src/main/java/expo/modules/kotlin/events/EventsDefinition.kt +3 -0
  30. package/android/src/main/java/expo/modules/kotlin/events/KEventEmitterWrapper.kt +26 -0
  31. package/android/src/main/java/expo/modules/kotlin/events/OnActivityResultPayload.kt +8 -0
  32. package/android/src/main/java/expo/modules/kotlin/exception/CodedException.kt +70 -0
  33. package/android/src/main/java/expo/modules/kotlin/methods/AnyMethod.kt +50 -0
  34. package/android/src/main/java/expo/modules/kotlin/methods/Method.kt +14 -0
  35. package/android/src/main/java/expo/modules/kotlin/methods/PromiseMethod.kt +15 -0
  36. package/android/src/main/java/expo/modules/kotlin/modules/Module.kt +24 -0
  37. package/android/src/main/java/expo/modules/kotlin/modules/ModuleDefinitionBuilder.kt +227 -0
  38. package/android/src/main/java/expo/modules/kotlin/modules/ModuleDefinitionData.kt +16 -0
  39. package/android/src/main/java/expo/modules/kotlin/records/Field.kt +5 -0
  40. package/android/src/main/java/expo/modules/kotlin/records/Record.kt +3 -0
  41. package/android/src/main/java/expo/modules/kotlin/records/RecordTypeConverter.kt +55 -0
  42. package/android/src/main/java/expo/modules/kotlin/types/AnyType.kt +14 -0
  43. package/android/src/main/java/expo/modules/kotlin/types/ArrayTypeConverter.kt +44 -0
  44. package/android/src/main/java/expo/modules/kotlin/types/BasicTypeConverters.kt +60 -0
  45. package/android/src/main/java/expo/modules/kotlin/types/EnumTypeConverter.kt +84 -0
  46. package/android/src/main/java/expo/modules/kotlin/types/ListTypeConverter.kt +25 -0
  47. package/android/src/main/java/expo/modules/kotlin/types/MapTypeConverter.kt +39 -0
  48. package/android/src/main/java/expo/modules/kotlin/types/PairTypeConverter.kt +28 -0
  49. package/android/src/main/java/expo/modules/kotlin/types/TypeConverter.kt +19 -0
  50. package/android/src/main/java/expo/modules/kotlin/types/TypeConverterProvider.kt +107 -0
  51. package/android/src/main/java/expo/modules/kotlin/views/AnyViewProp.kt +10 -0
  52. package/android/src/main/java/expo/modules/kotlin/views/ConcreteViewProp.kt +17 -0
  53. package/android/src/main/java/expo/modules/kotlin/views/GroupViewManagerWrapper.kt +22 -0
  54. package/android/src/main/java/expo/modules/kotlin/views/SimpleViewManagerWrapper.kt +21 -0
  55. package/android/src/main/java/expo/modules/kotlin/views/ViewManagerDefinition.kt +36 -0
  56. package/android/src/main/java/expo/modules/kotlin/views/ViewManagerDefinitionBuilder.kt +40 -0
  57. package/android/src/main/java/expo/modules/kotlin/views/ViewManagerWrapperDelegate.kt +21 -0
  58. package/android/src/main/java/expo/modules/kotlin/views/ViewWrapperDelegateHolder.kt +5 -0
  59. package/build/NativeModulesProxy.native.d.ts +4 -0
  60. package/build/NativeModulesProxy.native.js +14 -1
  61. package/build/NativeModulesProxy.native.js.map +1 -1
  62. package/build/NativeModulesProxy.types.d.ts +3 -0
  63. package/build/NativeModulesProxy.types.js.map +1 -1
  64. package/ios/AppDelegates/EXAppDelegateWrapper.h +16 -0
  65. package/ios/AppDelegates/EXAppDelegateWrapper.m +42 -0
  66. package/ios/AppDelegates/EXAppDelegatesLoader.h +15 -0
  67. package/ios/AppDelegates/EXAppDelegatesLoader.m +29 -0
  68. package/ios/AppDelegates/EXLegacyAppDelegateWrapper.h +16 -0
  69. package/ios/{EXAppDelegateWrapper.m → AppDelegates/EXLegacyAppDelegateWrapper.m} +2 -2
  70. package/ios/AppDelegates/ExpoAppDelegate.swift +264 -0
  71. package/ios/AppDelegates/ExpoAppDelegateSubscriber.swift +24 -0
  72. package/ios/ExpoModulesCore.podspec +7 -2
  73. package/ios/JSI/ExpoModulesProxySpec.h +24 -0
  74. package/ios/JSI/ExpoModulesProxySpec.mm +135 -0
  75. package/ios/JSI/JSIConversions.h +42 -0
  76. package/ios/JSI/JSIConversions.mm +164 -0
  77. package/ios/JSI/JSIInstaller.h +19 -0
  78. package/ios/JSI/JSIInstaller.mm +22 -0
  79. package/ios/ModuleRegistryAdapter/EXModuleRegistryAdapter.m +1 -6
  80. package/ios/NativeModulesProxy/EXNativeModulesProxy.h +6 -0
  81. package/ios/NativeModulesProxy/{EXNativeModulesProxy.m → EXNativeModulesProxy.mm} +45 -12
  82. package/ios/Services/EXReactNativeEventEmitter.h +6 -0
  83. package/ios/Services/EXReactNativeEventEmitter.m +15 -0
  84. package/ios/Swift/AppContext.swift +14 -1
  85. package/ios/Swift/Arguments/AnyArgument.swift +14 -0
  86. package/ios/Swift/Arguments/AnyArgumentType.swift +13 -0
  87. package/ios/Swift/Arguments/ArgumentType.swift +24 -0
  88. package/ios/Swift/Arguments/ConvertibleArgument.swift +15 -0
  89. package/ios/Swift/Arguments/Convertibles.swift +93 -0
  90. package/ios/Swift/Arguments/Types/ArrayArgumentType.swift +42 -0
  91. package/ios/Swift/Arguments/Types/ConvertibleArgumentType.swift +16 -0
  92. package/ios/Swift/Arguments/Types/EnumArgumentType.swift +105 -0
  93. package/ios/Swift/Arguments/Types/OptionalArgumentType.swift +49 -0
  94. package/ios/Swift/Arguments/Types/PromiseArgumentType.swift +15 -0
  95. package/ios/Swift/Arguments/Types/RawArgumentType.swift +25 -0
  96. package/ios/Swift/Conversions.swift +199 -7
  97. package/ios/Swift/EventListener.swift +37 -5
  98. package/ios/Swift/Functions/AnyFunction.swift +42 -0
  99. package/ios/Swift/{Methods/ConcreteMethod.swift → Functions/ConcreteFunction.swift} +32 -34
  100. package/ios/Swift/ModuleHolder.swift +75 -20
  101. package/ios/Swift/ModuleRegistry.swift +19 -8
  102. package/ios/Swift/Modules/AnyModule.swift +8 -8
  103. package/ios/Swift/Modules/Module.swift +7 -0
  104. package/ios/Swift/Modules/ModuleDefinition.swift +52 -8
  105. package/ios/Swift/Modules/ModuleDefinitionBuilder.swift +1 -1
  106. package/ios/Swift/Modules/ModuleDefinitionComponents.swift +140 -52
  107. package/ios/Swift/ModulesProvider.swift +9 -0
  108. package/ios/Swift/Promise.swift +1 -1
  109. package/ios/Swift/Records/Field.swift +1 -1
  110. package/ios/Swift/Records/Record.swift +8 -1
  111. package/ios/Swift/SwiftInteropBridge.swift +45 -16
  112. package/ios/Swift/Views/AnyViewProp.swift +2 -2
  113. package/ios/Swift/Views/ConcreteViewProp.swift +37 -10
  114. package/ios/Swift/Views/ViewModuleWrapper.swift +9 -4
  115. package/ios/Swift.h +9 -0
  116. package/ios/Tests/ArgumentTypeSpec.swift +145 -0
  117. package/ios/Tests/ConvertiblesSpec.swift +231 -0
  118. package/ios/Tests/{MethodSpec.swift → FunctionSpec.swift} +69 -54
  119. package/ios/Tests/FunctionWithConvertiblesSpec.swift +66 -0
  120. package/ios/Tests/Mocks/ModuleMocks.swift +21 -7
  121. package/ios/Tests/ModuleEventListenersSpec.swift +17 -16
  122. package/ios/Tests/ModuleRegistrySpec.swift +4 -7
  123. package/package.json +3 -3
  124. package/src/NativeModulesProxy.native.ts +22 -2
  125. package/src/NativeModulesProxy.types.ts +8 -0
  126. package/ios/EXAppDelegateWrapper.h +0 -13
  127. package/ios/Swift/Methods/AnyArgumentType.swift +0 -48
  128. package/ios/Swift/Methods/AnyMethod.swift +0 -31
  129. package/ios/Swift/Methods/AnyMethodArgument.swift +0 -13
@@ -0,0 +1,107 @@
1
+ @file:OptIn(ExperimentalStdlibApi::class)
2
+
3
+ package expo.modules.kotlin.types
4
+
5
+ import com.facebook.react.bridge.Dynamic
6
+ import com.facebook.react.bridge.ReadableArray
7
+ import com.facebook.react.bridge.ReadableMap
8
+ import expo.modules.kotlin.exception.MissingTypeConverter
9
+ import expo.modules.kotlin.records.Record
10
+ import expo.modules.kotlin.records.RecordTypeConverter
11
+ import kotlin.reflect.KClass
12
+ import kotlin.reflect.KType
13
+ import kotlin.reflect.full.createType
14
+ import kotlin.reflect.full.isSubclassOf
15
+ import kotlin.reflect.typeOf
16
+
17
+ interface TypeConverterProvider {
18
+ fun obtainTypeConverter(type: KType): TypeConverter<*>
19
+ }
20
+
21
+ inline fun <reified T : Any> obtainTypeConverter(): TypeConverter<T> {
22
+ @Suppress("UNCHECKED_CAST")
23
+ return TypeConverterProviderImpl.obtainTypeConverter(typeOf<T>()) as TypeConverter<T>
24
+ }
25
+
26
+ inline fun <reified T> convert(value: Dynamic): T {
27
+ val converter = TypeConverterProviderImpl.obtainTypeConverter(typeOf<T>())
28
+ return converter.convert(value) as T
29
+ }
30
+
31
+ fun convert(value: Dynamic, type: KType): Any? {
32
+ val converter = TypeConverterProviderImpl.obtainTypeConverter(type)
33
+ return converter.convert(value)
34
+ }
35
+
36
+ object TypeConverterProviderImpl : TypeConverterProvider {
37
+ private val cachedConverters = createCashedConverters(false) + createCashedConverters(true)
38
+
39
+ override fun obtainTypeConverter(type: KType): TypeConverter<*> {
40
+ cachedConverters[type]?.let {
41
+ return it
42
+ }
43
+
44
+ val kClass = type.classifier as? KClass<*> ?: throw MissingTypeConverter(type)
45
+
46
+ if (kClass.java.isArray) {
47
+ return ArrayTypeConverter(this, type)
48
+ }
49
+
50
+ if (kClass.isSubclassOf(List::class)) {
51
+ return ListTypeConverter(this, type)
52
+ }
53
+
54
+ if (kClass.isSubclassOf(Map::class)) {
55
+ return MapTypeConverter(this, type)
56
+ }
57
+
58
+ if (kClass.isSubclassOf(Pair::class)) {
59
+ return PairTypeConverter(this, type)
60
+ }
61
+
62
+ if (kClass.isSubclassOf(Array::class)) {
63
+ return ArrayTypeConverter(this, type)
64
+ }
65
+
66
+ if (kClass.java.isEnum) {
67
+ @Suppress("UNCHECKED_CAST")
68
+ return EnumTypeConverter(kClass as KClass<Enum<*>>, type.isMarkedNullable)
69
+ }
70
+
71
+ if (kClass.isSubclassOf(Record::class)) {
72
+ return RecordTypeConverter<Record>(this, type)
73
+ }
74
+
75
+ throw MissingTypeConverter(type)
76
+ }
77
+
78
+ private fun createCashedConverters(isOptional: Boolean): Map<KType, TypeConverter<*>> {
79
+ val intTypeConverter = IntTypeConverter(isOptional)
80
+ val doubleTypeConverter = DoubleTypeConverter(isOptional)
81
+ val floatTypeConverter = FloatTypeConverter(isOptional)
82
+ val boolTypeConverter = BoolTypeConverter(isOptional)
83
+
84
+ return mapOf(
85
+ Int::class.createType(nullable = isOptional) to intTypeConverter,
86
+ java.lang.Integer::class.createType(nullable = isOptional) to intTypeConverter,
87
+
88
+ Double::class.createType(nullable = isOptional) to doubleTypeConverter,
89
+ java.lang.Double::class.createType(nullable = isOptional) to doubleTypeConverter,
90
+
91
+ Float::class.createType(nullable = isOptional) to floatTypeConverter,
92
+ java.lang.Float::class.createType(nullable = isOptional) to floatTypeConverter,
93
+
94
+ Boolean::class.createType(nullable = isOptional) to boolTypeConverter,
95
+ java.lang.Boolean::class.createType(nullable = isOptional) to boolTypeConverter,
96
+
97
+ String::class.createType(nullable = isOptional) to StringTypeConverter(isOptional),
98
+
99
+ ReadableArray::class.createType(nullable = isOptional) to ReadableArrayTypeConverter(isOptional),
100
+ ReadableMap::class.createType(nullable = isOptional) to ReadableMapTypeConverter(isOptional),
101
+
102
+ IntArray::class.createType(nullable = isOptional) to PrimitiveIntArrayTypeConverter(isOptional),
103
+ DoubleArray::class.createType(nullable = isOptional) to PrimitiveDoubleArrayTypeConverter(isOptional),
104
+ FloatArray::class.createType(nullable = isOptional) to PrimitiveFloatArrayTypeConverter(isOptional),
105
+ )
106
+ }
107
+ }
@@ -0,0 +1,10 @@
1
+ package expo.modules.kotlin.views
2
+
3
+ import android.view.View
4
+ import com.facebook.react.bridge.Dynamic
5
+
6
+ abstract class AnyViewProp(
7
+ val name: String
8
+ ) {
9
+ abstract fun set(prop: Dynamic, onView: View)
10
+ }
@@ -0,0 +1,17 @@
1
+ package expo.modules.kotlin.views
2
+
3
+ import android.view.View
4
+ import com.facebook.react.bridge.Dynamic
5
+ import expo.modules.kotlin.types.AnyType
6
+
7
+ class ConcreteViewProp<ViewType : View, PropType>(
8
+ name: String,
9
+ private val propType: AnyType,
10
+ private val setter: (view: ViewType, prop: PropType) -> Unit,
11
+ ) : AnyViewProp(name) {
12
+
13
+ @Suppress("UNCHECKED_CAST")
14
+ override fun set(prop: Dynamic, onView: View) {
15
+ setter(onView as ViewType, propType.convert(prop) as PropType)
16
+ }
17
+ }
@@ -0,0 +1,22 @@
1
+ package expo.modules.kotlin.views
2
+
3
+ import android.view.View
4
+ import android.view.ViewGroup
5
+ import com.facebook.react.bridge.ReadableMap
6
+ import com.facebook.react.uimanager.ThemedReactContext
7
+ import com.facebook.react.uimanager.ViewGroupManager
8
+ import com.facebook.react.uimanager.annotations.ReactProp
9
+
10
+ class GroupViewManagerWrapper(
11
+ override val viewWrapperDelegate: ViewManagerWrapperDelegate
12
+ ) : ViewGroupManager<ViewGroup>(), ViewWrapperDelegateHolder {
13
+ override fun getName(): String = "ViewManagerAdapter_${viewWrapperDelegate.name}"
14
+
15
+ override fun createViewInstance(reactContext: ThemedReactContext): ViewGroup =
16
+ viewWrapperDelegate.createView(reactContext) as ViewGroup
17
+
18
+ @ReactProp(name = "proxiedProperties")
19
+ fun setProxiedProperties(view: View, proxiedProperties: ReadableMap) {
20
+ viewWrapperDelegate.setProxiedProperties(view, proxiedProperties)
21
+ }
22
+ }
@@ -0,0 +1,21 @@
1
+ package expo.modules.kotlin.views
2
+
3
+ import android.view.View
4
+ import com.facebook.react.bridge.ReadableMap
5
+ import com.facebook.react.uimanager.SimpleViewManager
6
+ import com.facebook.react.uimanager.ThemedReactContext
7
+ import com.facebook.react.uimanager.annotations.ReactProp
8
+
9
+ class SimpleViewManagerWrapper(
10
+ override val viewWrapperDelegate: ViewManagerWrapperDelegate
11
+ ) : SimpleViewManager<View>(), ViewWrapperDelegateHolder {
12
+ override fun getName(): String = "ViewManagerAdapter_${viewWrapperDelegate.name}"
13
+
14
+ override fun createViewInstance(reactContext: ThemedReactContext): View =
15
+ viewWrapperDelegate.createView(reactContext)
16
+
17
+ @ReactProp(name = "proxiedProperties")
18
+ fun setProxiedProperties(view: View, proxiedProperties: ReadableMap) {
19
+ viewWrapperDelegate.setProxiedProperties(view, proxiedProperties)
20
+ }
21
+ }
@@ -0,0 +1,36 @@
1
+ package expo.modules.kotlin.views
2
+
3
+ import android.content.Context
4
+ import android.view.View
5
+ import android.view.ViewGroup
6
+ import com.facebook.react.bridge.ReadableMap
7
+ import expo.modules.core.ViewManager
8
+ import expo.modules.kotlin.recycle
9
+
10
+ class ViewManagerDefinition(
11
+ private val viewFactory: (Context) -> View,
12
+ private val viewType: Class<out View>,
13
+ private val props: Map<String, AnyViewProp>
14
+ ) {
15
+
16
+ fun createView(context: Context): View = viewFactory(context)
17
+
18
+ fun getViewManagerType(): ViewManager.ViewManagerType {
19
+ return if (ViewGroup::class.java.isAssignableFrom(viewType)) {
20
+ ViewManager.ViewManagerType.GROUP
21
+ } else {
22
+ ViewManager.ViewManagerType.SIMPLE
23
+ }
24
+ }
25
+
26
+ fun setProps(propsToSet: ReadableMap, onView: View) {
27
+ val iterator = propsToSet.keySetIterator()
28
+ while (iterator.hasNextKey()) {
29
+ val key = iterator.nextKey()
30
+ val propDelegate = props[key] ?: continue
31
+ propsToSet.getDynamic(key).recycle {
32
+ propDelegate.set(this, onView)
33
+ }
34
+ }
35
+ }
36
+ }
@@ -0,0 +1,40 @@
1
+ @file:OptIn(ExperimentalStdlibApi::class)
2
+
3
+ package expo.modules.kotlin.views
4
+
5
+ import android.content.Context
6
+ import android.view.View
7
+ import expo.modules.kotlin.types.toAnyType
8
+ import kotlin.reflect.typeOf
9
+
10
+ class ViewManagerDefinitionBuilder {
11
+ @PublishedApi
12
+ internal var viewFactory: ((Context) -> View)? = null
13
+ @PublishedApi
14
+ internal var viewType: Class<out View>? = null
15
+ @PublishedApi
16
+ internal var props = mutableMapOf<String, AnyViewProp>()
17
+
18
+ fun build(): ViewManagerDefinition =
19
+ ViewManagerDefinition(
20
+ requireNotNull(viewFactory),
21
+ requireNotNull(viewType),
22
+ props
23
+ )
24
+
25
+ inline fun <reified ViewType : View> view(noinline body: (Context) -> ViewType) {
26
+ viewType = ViewType::class.java
27
+ viewFactory = body
28
+ }
29
+
30
+ inline fun <reified ViewType : View, reified PropType> prop(
31
+ name: String,
32
+ noinline body: (view: ViewType, prop: PropType) -> Unit
33
+ ) {
34
+ props[name] = ConcreteViewProp(
35
+ name,
36
+ typeOf<PropType>().toAnyType(),
37
+ body
38
+ )
39
+ }
40
+ }
@@ -0,0 +1,21 @@
1
+ package expo.modules.kotlin.views
2
+
3
+ import android.content.Context
4
+ import android.view.View
5
+ import com.facebook.react.bridge.ReadableMap
6
+ import expo.modules.kotlin.ModuleHolder
7
+
8
+ class ViewManagerWrapperDelegate(internal var moduleHolder: ModuleHolder) {
9
+ private val definition: ViewManagerDefinition
10
+ get() = requireNotNull(moduleHolder.definition.viewManagerDefinition)
11
+
12
+ val name: String
13
+ get() = moduleHolder.name
14
+
15
+ fun createView(context: Context): View =
16
+ definition.createView(context)
17
+
18
+ fun setProxiedProperties(view: View, proxiedProperties: ReadableMap) {
19
+ definition.setProps(proxiedProperties, view)
20
+ }
21
+ }
@@ -0,0 +1,5 @@
1
+ package expo.modules.kotlin.views
2
+
3
+ interface ViewWrapperDelegateHolder {
4
+ val viewWrapperDelegate: ViewManagerWrapperDelegate
5
+ }
@@ -2,4 +2,8 @@ import { ProxyNativeModule } from './NativeModulesProxy.types';
2
2
  declare const NativeModulesProxy: {
3
3
  [moduleName: string]: ProxyNativeModule;
4
4
  };
5
+ /**
6
+ * Sets whether to use a TurboModule version of the proxy.
7
+ */
8
+ export declare function useExpoTurboModules(state?: boolean): void;
5
9
  export default NativeModulesProxy;
@@ -3,6 +3,14 @@ const NativeProxy = NativeModules.NativeUnimoduleProxy;
3
3
  const modulesConstantsKey = 'modulesConstants';
4
4
  const exportedMethodsKey = 'exportedMethods';
5
5
  const NativeModulesProxy = {};
6
+ // Keep it opt-in for now. It's too risky without proper and thorough testing.
7
+ let canUseExpoTurboModules = false;
8
+ /**
9
+ * Sets whether to use a TurboModule version of the proxy.
10
+ */
11
+ export function useExpoTurboModules(state = true) {
12
+ canUseExpoTurboModules = state;
13
+ }
6
14
  if (NativeProxy) {
7
15
  Object.keys(NativeProxy[exportedMethodsKey]).forEach((moduleName) => {
8
16
  NativeModulesProxy[moduleName] = NativeProxy[modulesConstantsKey][moduleName] || {};
@@ -12,7 +20,12 @@ if (NativeProxy) {
12
20
  if (argumentsCount !== args.length) {
13
21
  return Promise.reject(new Error(`Native method ${moduleName}.${methodInfo.name} expects ${argumentsCount} ${argumentsCount === 1 ? 'argument' : 'arguments'} but received ${args.length}`));
14
22
  }
15
- return NativeProxy.callMethod(moduleName, key, args);
23
+ if (canUseExpoTurboModules && global.ExpoModulesProxy) {
24
+ return global.ExpoModulesProxy.callMethodAsync(moduleName, methodInfo.name, args);
25
+ }
26
+ else {
27
+ return NativeProxy.callMethod(moduleName, key, args);
28
+ }
16
29
  };
17
30
  });
18
31
  // These are called by EventEmitter (which is a wrapper for NativeEventEmitter)
@@ -1 +1 @@
1
- {"version":3,"file":"NativeModulesProxy.native.js","sourceRoot":"","sources":["../src/NativeModulesProxy.native.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAI7C,MAAM,WAAW,GAAG,aAAa,CAAC,oBAAoB,CAAC;AACvD,MAAM,mBAAmB,GAAG,kBAAkB,CAAC;AAC/C,MAAM,kBAAkB,GAAG,iBAAiB,CAAC;AAE7C,MAAM,kBAAkB,GAAgD,EAAE,CAAC;AAE3E,IAAI,WAAW,EAAE;IACf,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,kBAAkB,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE;QAClE,kBAAkB,CAAC,UAAU,CAAC,GAAG,WAAW,CAAC,mBAAmB,CAAC,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC;QACpF,WAAW,CAAC,kBAAkB,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,WAAW,GAAG,GAAG,EAAE,GAAE,CAAC,CAAC;YACtD,kBAAkB,CAAC,UAAU,CAAC,CAAC,eAAe,GAAG,GAAG,EAAE,GAAE,CAAC,CAAC;SAC3D;IACH,CAAC,CAAC,CAAC;CACJ;KAAM;IACL,OAAO,CAAC,IAAI,CACV,gJAAgJ,CACjJ,CAAC;CACH;AAED,eAAe,kBAAkB,CAAC","sourcesContent":["import { NativeModules } from 'react-native';\n\nimport { ProxyNativeModule } from './NativeModulesProxy.types';\n\nconst NativeProxy = NativeModules.NativeUnimoduleProxy;\nconst modulesConstantsKey = 'modulesConstants';\nconst exportedMethodsKey = 'exportedMethods';\n\nconst NativeModulesProxy: { [moduleName: string]: ProxyNativeModule } = {};\n\nif (NativeProxy) {\n Object.keys(NativeProxy[exportedMethodsKey]).forEach((moduleName) => {\n NativeModulesProxy[moduleName] = NativeProxy[modulesConstantsKey][moduleName] || {};\n NativeProxy[exportedMethodsKey][moduleName].forEach((methodInfo) => {\n NativeModulesProxy[moduleName][methodInfo.name] = (...args: unknown[]): Promise<any> => {\n const { key, argumentsCount } = methodInfo;\n if (argumentsCount !== args.length) {\n return Promise.reject(\n new Error(\n `Native method ${moduleName}.${methodInfo.name} expects ${argumentsCount} ${\n argumentsCount === 1 ? 'argument' : 'arguments'\n } but received ${args.length}`\n )\n );\n }\n return NativeProxy.callMethod(moduleName, key, args);\n };\n });\n\n // These are called by EventEmitter (which is a wrapper for NativeEventEmitter)\n // only on iOS and they use iOS-specific native module, EXReactNativeEventEmitter.\n //\n // On Android only {start,stop}Observing are called on the native module\n // and these should be exported as Expo methods.\n //\n // Before the RN 65, addListener/removeListeners weren't called on Android. However, it no longer stays true.\n // See https://github.com/facebook/react-native/commit/f5502fbda9fe271ff6e1d0da773a3a8ee206a453.\n // That's why, we check if the `EXReactNativeEventEmitter` exists and only if yes, we use it in the listener implementation.\n // Otherwise, those methods are NOOP.\n if (NativeModules.EXReactNativeEventEmitter) {\n NativeModulesProxy[moduleName].addListener = (...args) =>\n NativeModules.EXReactNativeEventEmitter.addProxiedListener(moduleName, ...args);\n NativeModulesProxy[moduleName].removeListeners = (...args) =>\n NativeModules.EXReactNativeEventEmitter.removeProxiedListeners(moduleName, ...args);\n } else {\n // Fixes on Android:\n // WARN `new NativeEventEmitter()` was called with a non-null argument without the required `addListener` method.\n // WARN `new NativeEventEmitter()` was called with a non-null argument without the required `removeListeners` method.\n NativeModulesProxy[moduleName].addListener = () => {};\n NativeModulesProxy[moduleName].removeListeners = () => {};\n }\n });\n} else {\n console.warn(\n `The \"EXNativeModulesProxy\" native module is not exported through NativeModules; verify that expo-modules-core's native code is linked properly`\n );\n}\n\nexport default NativeModulesProxy;\n"]}
1
+ {"version":3,"file":"NativeModulesProxy.native.js","sourceRoot":"","sources":["../src/NativeModulesProxy.native.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAS7C,MAAM,WAAW,GAAG,aAAa,CAAC,oBAAoB,CAAC;AACvD,MAAM,mBAAmB,GAAG,kBAAkB,CAAC;AAC/C,MAAM,kBAAkB,GAAG,iBAAiB,CAAC;AAE7C,MAAM,kBAAkB,GAAgD,EAAE,CAAC;AAE3E,8EAA8E;AAC9E,IAAI,sBAAsB,GAAG,KAAK,CAAC;AAEnC;;GAEG;AACH,MAAM,UAAU,mBAAmB,CAAC,QAAiB,IAAI;IACvD,sBAAsB,GAAG,KAAK,CAAC;AACjC,CAAC;AAED,IAAI,WAAW,EAAE;IACf,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,kBAAkB,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE;QAClE,kBAAkB,CAAC,UAAU,CAAC,GAAG,WAAW,CAAC,mBAAmB,CAAC,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC;QACpF,WAAW,CAAC,kBAAkB,CAAC,CAAC,UAAU,CAAC,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE;YACjE,kBAA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AAC,CAAC;SACvF;aAAM;YACL,oBAAoB;YACpB,kHAAkH;YAClH,sHAAsH;YACtH,kBAAkB,CAAC,UAAU,CAAC,CAAC,WAAW,GAAG,GAAG,EAAE,GAAE,CAAC,CAAC;YACtD,kBAAkB,CAAC,UAAU,CAAC,CAAC,eAAe,GAAG,GAAG,EAAE,GAAE,CAAC,CAAC;SAC3D;IACH,CAAC,CAAC,CAAC;CACJ;KAAM;IACL,OAAO,CAAC,IAAI,CACV,gJAAgJ,CACjJ,CAAC;CACH;AAED,eAAe,kBAAkB,CAAC","sourcesContent":["import { NativeModules } from 'react-native';\n\nimport { ProxyNativeModule, TurboNativeModuleProxy } from './NativeModulesProxy.types';\n\n// `ExpoModulesProxy` is not declared in TypeScript yet. It's installed via JSI.\ndeclare namespace global {\n const ExpoModulesProxy: TurboNativeModuleProxy;\n}\n\nconst NativeProxy = NativeModules.NativeUnimoduleProxy;\nconst modulesConstantsKey = 'modulesConstants';\nconst exportedMethodsKey = 'exportedMethods';\n\nconst NativeModulesProxy: { [moduleName: string]: ProxyNativeModule } = {};\n\n// Keep it opt-in for now. It's too risky without proper and thorough testing.\nlet canUseExpoTurboModules = false;\n\n/**\n * Sets whether to use a TurboModule version of the proxy.\n */\nexport function useExpoTurboModules(state: boolean = true) {\n canUseExpoTurboModules = state;\n}\n\nif (NativeProxy) {\n Object.keys(NativeProxy[exportedMethodsKey]).forEach((moduleName) => {\n NativeModulesProxy[moduleName] = NativeProxy[modulesConstantsKey][moduleName] || {};\n NativeProxy[exportedMethodsKey][moduleName].forEach((methodInfo) => {\n NativeModulesProxy[moduleName][methodInfo.name] = (...args: unknown[]): Promise<any> => {\n const { key, argumentsCount } = methodInfo;\n if (argumentsCount !== args.length) {\n return Promise.reject(\n new Error(\n `Native method ${moduleName}.${methodInfo.name} expects ${argumentsCount} ${\n argumentsCount === 1 ? 'argument' : 'arguments'\n } but received ${args.length}`\n )\n );\n }\n\n if (canUseExpoTurboModules && global.ExpoModulesProxy) {\n return global.ExpoModulesProxy.callMethodAsync(moduleName, methodInfo.name, args);\n } else {\n return NativeProxy.callMethod(moduleName, key, args);\n }\n };\n });\n\n // These are called by EventEmitter (which is a wrapper for NativeEventEmitter)\n // only on iOS and they use iOS-specific native module, EXReactNativeEventEmitter.\n //\n // On Android only {start,stop}Observing are called on the native module\n // and these should be exported as Expo methods.\n //\n // Before the RN 65, addListener/removeListeners weren't called on Android. However, it no longer stays true.\n // See https://github.com/facebook/react-native/commit/f5502fbda9fe271ff6e1d0da773a3a8ee206a453.\n // That's why, we check if the `EXReactNativeEventEmitter` exists and only if yes, we use it in the listener implementation.\n // Otherwise, those methods are NOOP.\n if (NativeModules.EXReactNativeEventEmitter) {\n NativeModulesProxy[moduleName].addListener = (...args) =>\n NativeModules.EXReactNativeEventEmitter.addProxiedListener(moduleName, ...args);\n NativeModulesProxy[moduleName].removeListeners = (...args) =>\n NativeModules.EXReactNativeEventEmitter.removeProxiedListeners(moduleName, ...args);\n } else {\n // Fixes on Android:\n // WARN `new NativeEventEmitter()` was called with a non-null argument without the required `addListener` method.\n // WARN `new NativeEventEmitter()` was called with a non-null argument without the required `removeListeners` method.\n NativeModulesProxy[moduleName].addListener = () => {};\n NativeModulesProxy[moduleName].removeListeners = () => {};\n }\n });\n} else {\n console.warn(\n `The \"EXNativeModulesProxy\" native module is not exported through NativeModules; verify that expo-modules-core's native code is linked properly`\n );\n}\n\nexport default NativeModulesProxy;\n"]}
@@ -3,3 +3,6 @@ export declare type ProxyNativeModule = {
3
3
  addListener: (eventName: string) => void;
4
4
  removeListeners: (count: number) => void;
5
5
  };
6
+ export declare type TurboNativeModuleProxy = {
7
+ callMethodAsync: <ReturnType>(moduleName: string, methodName: string, args: any[]) => Promise<ReturnType>;
8
+ };
@@ -1 +1 @@
1
- {"version":3,"file":"NativeModulesProxy.types.js","sourceRoot":"","sources":["../src/NativeModulesProxy.types.ts"],"names":[],"mappings":"","sourcesContent":["export type ProxyNativeModule = {\n [propertyName: string]: any;\n addListener: (eventName: string) => void;\n removeListeners: (count: number) => void;\n};\n"]}
1
+ {"version":3,"file":"NativeModulesProxy.types.js","sourceRoot":"","sources":["../src/NativeModulesProxy.types.ts"],"names":[],"mappings":"","sourcesContent":["export type ProxyNativeModule = {\n [propertyName: string]: any;\n addListener: (eventName: string) => void;\n removeListeners: (count: number) => void;\n};\n\nexport type TurboNativeModuleProxy = {\n callMethodAsync: <ReturnType>(\n moduleName: string,\n methodName: string,\n args: any[]\n ) => Promise<ReturnType>;\n};\n"]}
@@ -0,0 +1,16 @@
1
+ // Copyright 2018-present 650 Industries. All rights reserved.
2
+
3
+ #import <UIKit/UIKit.h>
4
+
5
+ NS_ASSUME_NONNULL_BEGIN
6
+
7
+ /**
8
+ Provides backwards compatibility for existing projects with `AppDelegate`
9
+ written in Objective-C and that forwards all messages to the new `ExpoAppDelegate`.
10
+ If your `AppDelegate` is in Swift, it should inherit from `ExpoAppDelegate` class instead.
11
+ */
12
+ @interface EXAppDelegateWrapper : UIResponder <UIApplicationDelegate>
13
+
14
+ @end
15
+
16
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,42 @@
1
+ // Copyright 2018-present 650 Industries. All rights reserved.
2
+
3
+ #import <ExpoModulesCore/EXAppDelegateWrapper.h>
4
+
5
+ #if __has_include(<ExpoModulesCore/ExpoModulesCore-Swift.h>)
6
+ // When `use_frameworks!` is used, the generated Swift header is inside ExpoModulesCore module.
7
+ #import <ExpoModulesCore/ExpoModulesCore-Swift.h>
8
+ #else
9
+ #import "ExpoModulesCore-Swift.h"
10
+ #endif
11
+
12
+ @implementation EXAppDelegateWrapper {
13
+ EXExpoAppDelegate *_expoAppDelegate;
14
+ }
15
+
16
+ // Synthesize window, so the AppDelegate can synthesize it too.
17
+ @synthesize window = _window;
18
+
19
+ - (instancetype)init
20
+ {
21
+ if (self = [super init]) {
22
+ _expoAppDelegate = [[EXExpoAppDelegate alloc] init];
23
+ }
24
+ return self;
25
+ }
26
+
27
+ // This needs to be implemented, otherwise forwarding won't be called.
28
+ // When the app starts, `UIApplication` uses it to check beforehand
29
+ // which `UIApplicationDelegate` selectors are implemented.
30
+ - (BOOL)respondsToSelector:(SEL)selector
31
+ {
32
+ return [super respondsToSelector:selector]
33
+ || [_expoAppDelegate respondsToSelector:selector];
34
+ }
35
+
36
+ // Forwards all invocations to `ExpoAppDelegate` object.
37
+ - (id)forwardingTargetForSelector:(SEL)selector
38
+ {
39
+ return _expoAppDelegate;
40
+ }
41
+
42
+ @end
@@ -0,0 +1,15 @@
1
+ // Copyright 2018-present 650 Industries. All rights reserved.
2
+
3
+ #import <Foundation/Foundation.h>
4
+
5
+ NS_ASSUME_NONNULL_BEGIN
6
+
7
+ /**
8
+ Loads `ExpoAppDelegate` subscribers based on
9
+ the list from the generated `ExpoModulesProvider`.
10
+ */
11
+ @interface EXAppDelegatesLoader : NSObject
12
+
13
+ @end
14
+
15
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,29 @@
1
+ // Copyright 2018-present 650 Industries. All rights reserved.
2
+
3
+ #import <ExpoModulesCore/EXAppDelegatesLoader.h>
4
+ #import <ExpoModulesCore/EXNativeModulesProxy.h>
5
+
6
+ #if __has_include(<ExpoModulesCore/ExpoModulesCore-Swift.h>)
7
+ // When `use_frameworks!` is used, the generated Swift header is inside ExpoModulesCore module.
8
+ #import <ExpoModulesCore/ExpoModulesCore-Swift.h>
9
+ #else
10
+ #import "ExpoModulesCore-Swift.h"
11
+ #endif
12
+
13
+ // Make the legacy wrapper conform to the protocol for subscribers.
14
+ @interface EXLegacyAppDelegateWrapper () <EXAppDelegateSubscriberProtocol>
15
+ @end
16
+
17
+ @implementation EXAppDelegatesLoader
18
+
19
+ // App delegate providers must be registered before any `AppDelegate` life-cycle event is called.
20
+ // Unfortunately it's not possible in Swift to run code right after the binary is loaded
21
+ // and before any code is executed, so we switch back to Objective-C just to do this one thing.
22
+ + (void)load
23
+ {
24
+ id<ModulesProviderObjCProtocol> modulesProvider = [EXNativeModulesProxy getExpoModulesProvider];
25
+ [EXExpoAppDelegate registerSubscriber:[[EXLegacyAppDelegateWrapper alloc] init]];
26
+ [EXExpoAppDelegate registerSubscribersFromModulesProvider:modulesProvider];
27
+ }
28
+
29
+ @end
@@ -0,0 +1,16 @@
1
+ // Copyright © 2018 650 Industries. All rights reserved.
2
+
3
+ #import <Foundation/Foundation.h>
4
+ #import <UIKit/UIKit.h>
5
+
6
+ NS_ASSUME_NONNULL_BEGIN
7
+
8
+ /**
9
+ The legacy wrapper is still used to forward app delegate calls to singleton modules.
10
+ See `EXAppDelegatesLoader.m` which registers this class as a subscriber of `ExpoAppDelegate`.
11
+ */
12
+ @interface EXLegacyAppDelegateWrapper : UIResponder <UIApplicationDelegate>
13
+
14
+ @end
15
+
16
+ NS_ASSUME_NONNULL_END
@@ -3,13 +3,13 @@
3
3
  #import <Foundation/FoundationErrors.h>
4
4
 
5
5
  #import <ExpoModulesCore/EXModuleRegistryProvider.h>
6
- #import <ExpoModulesCore/EXAppDelegateWrapper.h>
6
+ #import <ExpoModulesCore/EXLegacyAppDelegateWrapper.h>
7
7
 
8
8
  static NSMutableArray<id<UIApplicationDelegate>> *subcontractors;
9
9
  static NSMutableDictionary<NSString *,NSArray<id<UIApplicationDelegate>> *> *subcontractorsForSelector;
10
10
  static dispatch_once_t onceToken;
11
11
 
12
- @implementation EXAppDelegateWrapper
12
+ @implementation EXLegacyAppDelegateWrapper
13
13
 
14
14
  @synthesize window = _window;
15
15