-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: duplicate worklets code the smart way (#6827)
## Summary In the transitional period of `react-native-worklets` coming as a separate package, Reanimated should work with or without `react-native-worklets` installed. This requires the code of Worklets to be present both in `react-native-reanimated` and `react-native-worklets`. This PR moves Worklets' files that were in Reanimated to `react-native-worklets` package and adds a script that makes hard links to these files. The linking happens during the `yarn build` step of `react-native-reanimated` - this guarantees that `react-native-reanimated` would still work without `react-native-worklets` and that its' npm package would still contain all the necessary files. For Android some files had to be duplicated. This is because of codegen conflicts and namespace generation. However, that code is relatively small and put into respective source sets `externalWorklets/with` and `externalWorklets/without`. Also: - I moved `ReanimatedVersion.cpp/h` files back to Reanimated. We'll implement version checking for Worklets when we actually need it. - I adjusted the CI to always do `yarn build` in Reanimated when necessary. - Enabled detection of `react-native-worklets` in `react-native-reanimated`. - I improved the detection mechanism of `react-native-worklets` for iOS. - Renamed `NativeWorkletsModuleSpec.ts` to `NativeReaWorkletsModuleSpec.ts` to avoid codegen conflicts. - Removed `DummyWorkletsModule` from `react-native-worklets` since now it has `WorkletsModule`. ## Test plan Run an Example with these changes. Then do `yarn add react-native-worklets` in `common-app` (it will use workspace version by default) in the example directory and run it again.
- Loading branch information
Showing
93 changed files
with
361 additions
and
180 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
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
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
2 changes: 1 addition & 1 deletion
2
...es/react-native-reanimated/Common/cpp/reanimated/RuntimeDecorators/RNRuntimeDecorator.cpp
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
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
97 changes: 97 additions & 0 deletions
97
...nimated/android/src/externalWorklets/with/com/swmansion/reanimated/ReanimatedPackage.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,97 @@ | ||
package com.swmansion.reanimated; | ||
|
||
import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_UI_MANAGER_MODULE_END; | ||
import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_UI_MANAGER_MODULE_START; | ||
|
||
import androidx.annotation.NonNull; | ||
import com.facebook.react.BaseReactPackage; | ||
import com.facebook.react.ReactApplication; | ||
import com.facebook.react.ReactInstanceManager; | ||
import com.facebook.react.ReactPackage; | ||
import com.facebook.react.bridge.NativeModule; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReactMarker; | ||
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.uimanager.ReanimatedUIManager; | ||
import com.facebook.react.uimanager.UIManagerModule; | ||
import com.facebook.react.uimanager.ViewManager; | ||
import com.facebook.systrace.Systrace; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
@ReactModuleList( | ||
nativeModules = { | ||
ReanimatedModule.class, | ||
ReanimatedUIManager.class, | ||
}) | ||
public class ReanimatedPackage extends BaseReactPackage implements ReactPackage { | ||
@Override | ||
public NativeModule getModule( | ||
@NonNull String name, @NonNull ReactApplicationContext reactContext) { | ||
return switch (name) { | ||
case ReanimatedModule.NAME -> new ReanimatedModule(reactContext); | ||
case ReanimatedUIManager.NAME -> createUIManager(reactContext); | ||
default -> null; | ||
}; | ||
} | ||
|
||
@Override | ||
public ReactModuleInfoProvider getReactModuleInfoProvider() { | ||
Class<? extends NativeModule>[] moduleList = | ||
new Class[] { | ||
ReanimatedModule.class, ReanimatedUIManager.class, | ||
}; | ||
|
||
final Map<String, ReactModuleInfo> reactModuleInfoMap = new HashMap<>(); | ||
for (Class<? extends NativeModule> moduleClass : moduleList) { | ||
ReactModule reactModule = | ||
Objects.requireNonNull(moduleClass.getAnnotation(ReactModule.class)); | ||
|
||
reactModuleInfoMap.put( | ||
reactModule.name(), | ||
new ReactModuleInfo( | ||
reactModule.name(), | ||
moduleClass.getName(), | ||
true, // override UIManagerModule | ||
reactModule.needsEagerInit(), | ||
reactModule.isCxxModule(), | ||
BuildConfig.IS_NEW_ARCHITECTURE_ENABLED)); | ||
} | ||
|
||
return () -> reactModuleInfoMap; | ||
} | ||
|
||
private UIManagerModule createUIManager(final ReactApplicationContext reactContext) { | ||
ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_START); | ||
Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "createUIManagerModule"); | ||
final ReactInstanceManager reactInstanceManager = getReactInstanceManager(reactContext); | ||
List<ViewManager> viewManagers = reactInstanceManager.getOrCreateViewManagers(reactContext); | ||
int minTimeLeftInFrameForNonBatchedOperationMs = -1; | ||
try { | ||
return ReanimatedUIManagerFactory.create( | ||
reactContext, viewManagers, minTimeLeftInFrameForNonBatchedOperationMs); | ||
} finally { | ||
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); | ||
ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_END); | ||
} | ||
} | ||
|
||
/** | ||
* Get the {@link ReactInstanceManager} used by this app. By default, assumes {@link | ||
* ReactApplicationContext#getApplicationContext()} is an instance of {@link ReactApplication} and | ||
* calls {@link ReactApplication#getReactNativeHost().getReactInstanceManager()}. Override this | ||
* method if your application class does not implement {@code ReactApplication} or you simply have | ||
* a different mechanism for storing a {@code ReactInstanceManager}, e.g. as a static field | ||
* somewhere. | ||
*/ | ||
public ReactInstanceManager getReactInstanceManager(ReactApplicationContext reactContext) { | ||
return ((ReactApplication) reactContext.getApplicationContext()) | ||
.getReactNativeHost() | ||
.getReactInstanceManager(); | ||
} | ||
} |
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.