-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add TurboModule support (#910)
Co-authored-by: Shawn Dempsey <shawndempsey@fb.com> Co-authored-by: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Co-authored-by: Shawn Dempsey <shawnton@gmail.com>
- Loading branch information
1 parent
f4e5d92
commit 86a7e90
Showing
19 changed files
with
375 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
android/src/javaPackage/java/com/reactnativecommunity/asyncstorage/AsyncStoragePackage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.reactnativecommunity.asyncstorage; | ||
|
||
import com.facebook.react.TurboReactPackage; | ||
import com.facebook.react.ViewManagerOnDemandReactPackage; | ||
import com.facebook.react.bridge.ModuleSpec; | ||
import com.facebook.react.bridge.JavaScriptModule; | ||
import com.facebook.react.bridge.NativeModule; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.module.annotations.ReactModule; | ||
import com.facebook.react.module.annotations.ReactModuleList; | ||
import com.facebook.react.module.model.ReactModuleInfo; | ||
import com.facebook.react.module.model.ReactModuleInfoProvider; | ||
import com.facebook.react.turbomodule.core.interfaces.TurboModule; | ||
import com.facebook.react.uimanager.ViewManager; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
@ReactModuleList( | ||
nativeModules = { | ||
AsyncStorageModule.class, | ||
} | ||
) | ||
public class AsyncStoragePackage extends TurboReactPackage { | ||
|
||
@Override | ||
protected List<ModuleSpec> getViewManagers(ReactApplicationContext reactContext) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public NativeModule getModule(String name, @Nonnull ReactApplicationContext reactContext) { | ||
switch (name) { | ||
case AsyncStorageModule.NAME: | ||
return new AsyncStorageModule(reactContext); | ||
default: | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public ReactModuleInfoProvider getReactModuleInfoProvider() { | ||
try { | ||
Class<?> reactModuleInfoProviderClass = | ||
Class.forName("com.reactnativecommunity.asyncstorage.AsyncStoragePackage$$ReactModuleInfoProvider"); | ||
return (ReactModuleInfoProvider) reactModuleInfoProviderClass.newInstance(); | ||
} catch (ClassNotFoundException e) { | ||
// ReactModuleSpecProcessor does not run at build-time. Create this ReactModuleInfoProvider by | ||
// hand. | ||
return new ReactModuleInfoProvider() { | ||
@Override | ||
public Map<String, ReactModuleInfo> getReactModuleInfos() { | ||
final Map<String, ReactModuleInfo> reactModuleInfoMap = new HashMap<>(); | ||
|
||
Class<? extends NativeModule>[] moduleList = | ||
new Class[] { | ||
AsyncStorageModule.class, | ||
}; | ||
|
||
for (Class<? extends NativeModule> moduleClass : moduleList) { | ||
ReactModule reactModule = moduleClass.getAnnotation(ReactModule.class); | ||
|
||
reactModuleInfoMap.put( | ||
reactModule.name(), | ||
new ReactModuleInfo( | ||
reactModule.name(), | ||
moduleClass.getName(), | ||
reactModule.canOverrideExistingModule(), | ||
reactModule.needsEagerInit(), | ||
reactModule.hasConstants(), | ||
reactModule.isCxxModule(), | ||
TurboModule.class.isAssignableFrom(moduleClass))); | ||
} | ||
|
||
return reactModuleInfoMap; | ||
} | ||
}; | ||
} catch (InstantiationException | IllegalAccessException e) { | ||
throw new RuntimeException( | ||
"No ReactModuleInfoProvider for com.reactnativecommunity.asyncstorage.AsyncStoragePackage$$ReactModuleInfoProvider", e); | ||
} | ||
} | ||
|
||
// Deprecated in RN 0.47 | ||
public List<Class<? extends JavaScriptModule>> createJSModules() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("rawtypes") | ||
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) { | ||
return Collections.emptyList(); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
android/src/kotlinPackage/java/com/reactnativecommunity/asyncstorage/AsyncStoragePackage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.reactnativecommunity.asyncstorage | ||
|
||
import com.facebook.react.TurboReactPackage | ||
import com.facebook.react.ViewManagerOnDemandReactPackage | ||
import com.facebook.react.bridge.ModuleSpec | ||
import com.facebook.react.bridge.NativeModule | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.module.annotations.ReactModule | ||
import com.facebook.react.module.annotations.ReactModuleList | ||
import com.facebook.react.module.model.ReactModuleInfo | ||
import com.facebook.react.module.model.ReactModuleInfoProvider | ||
import com.facebook.react.turbomodule.core.interfaces.TurboModule | ||
import com.facebook.react.uimanager.ReactShadowNode | ||
import com.facebook.react.uimanager.ViewManager | ||
import com.reactnativecommunity.asyncstorage.next.StorageModule | ||
|
||
@ReactModuleList( | ||
nativeModules = [ | ||
StorageModule::class | ||
] | ||
) | ||
class AsyncStoragePackage : TurboReactPackage() { | ||
override fun getModule(name: String, context: ReactApplicationContext): NativeModule? = when (name) { | ||
StorageModule.NAME -> StorageModule(context) | ||
else -> null | ||
} | ||
|
||
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider { | ||
try { | ||
val reactModuleInfoProviderClass = | ||
Class.forName("com.reactnativecommunity.asyncstorage.AsyncStoragePackage$\$ReactModuleInfoProvider") | ||
return reactModuleInfoProviderClass.newInstance() as ReactModuleInfoProvider | ||
} catch (e: ClassNotFoundException) { | ||
return ReactModuleInfoProvider { | ||
val reactModule: ReactModule = StorageModule::class.java.getAnnotation( | ||
ReactModule::class.java)!! | ||
|
||
mutableMapOf( | ||
StorageModule.NAME to ReactModuleInfo( | ||
reactModule.name, | ||
StorageModule::class.java.name, | ||
reactModule.canOverrideExistingModule, | ||
reactModule.needsEagerInit, | ||
reactModule.hasConstants, | ||
reactModule.isCxxModule, | ||
TurboModule::class.java.isAssignableFrom(StorageModule::class.java) | ||
) | ||
) | ||
} | ||
} catch (e: InstantiationException) { | ||
throw RuntimeException("No ReactModuleInfoProvider for AsyncStoragePackage$\$ReactModuleInfoProvider", e) | ||
} catch (e: IllegalAccessException) { | ||
throw RuntimeException("No ReactModuleInfoProvider for AsyncStoragePackage$\$ReactModuleInfoProvider", e) | ||
} | ||
} | ||
|
||
override fun getViewManagers(reactContext: ReactApplicationContext?): MutableList<ModuleSpec>? = null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.