Skip to content

Commit

Permalink
Merge branch 'main' into @tomekzaw/react-native-0.77
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekzaw committed Jan 12, 2025
2 parents e30d134 + a2eb043 commit e4d8ad3
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public void registerFabricEventListener(NodesManager nodesManager) {
}
}

public void unregisterFabricEventListener(NodesManager nodesManager) {
if (fabricUIManager != null) {
fabricUIManager.getEventDispatcher().removeListener(nodesManager);
}
}

public void synchronouslyUpdateUIProps(int viewTag, ReadableMap uiProps) {
fabricUIManager.synchronouslyUpdateViewOnUIThread(viewTag, uiProps);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.facebook.react.uimanager.UIManagerReanimatedHelper;
import com.facebook.react.uimanager.common.UIManagerType;
import com.facebook.react.uimanager.events.Event;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.react.uimanager.events.EventDispatcherListener;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.swmansion.reanimated.layoutReanimation.AnimationsManager;
Expand Down Expand Up @@ -111,6 +112,7 @@ public interface OnAnimationFrame {
public Set<String> uiProps = Collections.emptySet();
public Set<String> nativeProps = Collections.emptySet();
private ReaCompatibility compatibility;
private @Nullable Runnable mUnsubscribe = null;

public NativeProxy getNativeProxy() {
return mNativeProxy;
Expand All @@ -131,6 +133,15 @@ public void invalidate() {
mNativeProxy.invalidate();
mNativeProxy = null;
}

if (compatibility != null) {
compatibility.unregisterFabricEventListener(this);
}

if (mUnsubscribe != null) {
mUnsubscribe.run();
mUnsubscribe = null;
}
}

public void initWithContext(ReactApplicationContext reactApplicationContext) {
Expand Down Expand Up @@ -184,8 +195,10 @@ protected void doFrameGuarded(long frameTimeNanos) {
// Events are handled in the native modules thread in the `onEventDispatch()` method.
// This method indirectly uses `mChoreographerCallback` which was created after event
// registration, creating race condition
Objects.requireNonNull(UIManagerHelper.getEventDispatcher(context, uiManagerType))
.addListener(this);
EventDispatcher eventDispatcher =
Objects.requireNonNull(UIManagerHelper.getEventDispatcher(context, uiManagerType));
eventDispatcher.addListener(this);
mUnsubscribe = () -> eventDispatcher.removeListener(this);

mAnimationManager = new AnimationsManager(mContext, mUIManager);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,14 @@ public static float convertToFloat(Object value) {
}
return 0;
}

public static Runnable combineRunnables(final Runnable... runnables) {
return new Runnable() {
public void run() {
for (Runnable r : runnables) {
r.run();
}
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ public ReaCompatibility(ReactApplicationContext reactApplicationContext) {}

public void registerFabricEventListener(NodesManager nodesManager) {}

public void unregisterFabricEventListener(NodesManager nodesManager) {}

public void synchronouslyUpdateUIProps(int viewTag, ReadableMap uiProps) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ private interface UIThreadOperation {
private ArrayList<UIThreadOperation> mOperations = new ArrayList<>();
private @Nullable NodesManager mNodesManager;
private final WorkletsModule mWorkletsModule;
private Runnable mUnsubscribe = () -> {};

public ReanimatedModule(ReactApplicationContext reactContext) {
super(reactContext);
Expand All @@ -81,14 +82,22 @@ public void initialize() {
UIManager uiManager = reactCtx.getFabricUIManager();
if (uiManager instanceof FabricUIManager) {
((FabricUIManager) uiManager).addUIManagerEventListener(this);
mUnsubscribe =
Utils.combineRunnables(
mUnsubscribe,
() -> ((FabricUIManager) uiManager).removeUIManagerEventListener(this));
} else {
throw new RuntimeException("[Reanimated] Failed to obtain instance of FabricUIManager.");
}
} else {
UIManagerModule uiManager = reactCtx.getNativeModule(UIManagerModule.class);
uiManager.addUIManagerListener(this);
mUnsubscribe =
Utils.combineRunnables(mUnsubscribe, () -> uiManager.removeUIManagerListener(this));
}
reactCtx.addLifecycleEventListener(this);
mUnsubscribe =
Utils.combineRunnables(mUnsubscribe, () -> reactCtx.removeLifecycleEventListener(this));
}

@Override
Expand Down Expand Up @@ -176,5 +185,7 @@ public void invalidate() {
if (mNodesManager != null) {
mNodesManager.invalidate();
}

mUnsubscribe.run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ private interface UIThreadOperation {
private ArrayList<UIThreadOperation> mOperations = new ArrayList<>();
private @Nullable NodesManager mNodesManager;
private final WorkletsModule mWorkletsModule;
private Runnable mUnsubscribe = () -> {};

public ReanimatedModule(ReactApplicationContext reactContext) {
super(reactContext);
Expand All @@ -81,15 +82,23 @@ public void initialize() {
UIManager uiManager = reactCtx.getFabricUIManager();
if (uiManager instanceof FabricUIManager) {
((FabricUIManager) uiManager).addUIManagerEventListener(this);
mUnsubscribe =
Utils.combineRunnables(
mUnsubscribe,
() -> ((FabricUIManager) uiManager).removeUIManagerEventListener(this));
} else {
throw new RuntimeException("[Reanimated] Failed to obtain instance of FabricUIManager.");
}
} else {
UIManagerModule uiManager =
Objects.requireNonNull(reactCtx.getNativeModule(UIManagerModule.class));
uiManager.addUIManagerListener(this);
mUnsubscribe =
Utils.combineRunnables(mUnsubscribe, () -> uiManager.removeUIManagerListener(this));
}
reactCtx.addLifecycleEventListener(this);
mUnsubscribe =
Utils.combineRunnables(mUnsubscribe, () -> reactCtx.removeLifecycleEventListener(this));
}

@Override
Expand Down Expand Up @@ -174,5 +183,7 @@ public void invalidate() {
if (mNodesManager != null) {
mNodesManager.invalidate();
}

mUnsubscribe.run();
}
}

0 comments on commit e4d8ad3

Please sign in to comment.