-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Always return an EventDispatcher in bridgeless mode
Summary: This changes how we access the EventDispatcher from the FabricUIManager in bridgeless mode. Currently, we have implemented a similar API to what we use for Fabric (used in UIManagerHelper): `BridgelessReactContext.getJSIModule(UIManager).getEventDispatcher()`. However, `getJSIModule` does not have a nullable return type, which means that we have to throw an exception if the UIManager can't be found - if, for example, the instance is not initialized yet (or has been destroyed). This is causing crashes when a view tries to access the EventDispatcher before the instance is initialized, which takes longer for Venice because we include JS bundle loading as part of initialization (we may need to revisit that). Ideally, we'd like to create a first-class API for `getEventDispatcher()`, and make sure that it never crashes if the instance is destroyed, because we don't care if JS events aren't delivered at that point. However, there are some obstacles to making this change for the bridge - mostly related to avoiding circular dependencies between the bridge module and the uimanager module. (Also, this might be a behavior change for the bridge, because I think we currently start queueing events before it's initialized? and product code might be relying on that). Reviewed By: mdvacca Differential Revision: D21672949 fbshipit-source-id: a38e96cd40c6f70124b7ca2a5c9722988fe7fcf4
- Loading branch information
1 parent
fd97e95
commit 0a12f3e
Showing
4 changed files
with
112 additions
and
33 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
57 changes: 57 additions & 0 deletions
57
ReactAndroid/src/main/java/com/facebook/react/uimanager/events/BlackHoleEventDispatcher.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,57 @@ | ||
/* | ||
* 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.facebook.react.uimanager.events; | ||
|
||
import com.facebook.common.logging.FLog; | ||
|
||
/** | ||
* A singleton class that overrides {@link EventDispatcher} with no-op methods, to be used by | ||
* callers that expect an EventDispatcher when the instance doesn't exist. | ||
*/ | ||
public class BlackHoleEventDispatcher implements EventDispatcher { | ||
|
||
private static final EventDispatcher sEventDispatcher = new BlackHoleEventDispatcher(); | ||
|
||
public static EventDispatcher get() { | ||
return sEventDispatcher; | ||
} | ||
|
||
private BlackHoleEventDispatcher() {} | ||
|
||
@Override | ||
public void dispatchEvent(Event event) { | ||
FLog.d( | ||
getClass().getSimpleName(), | ||
"Trying to emit event to JS, but the React instance isn't ready. Event: " | ||
+ event.getEventName()); | ||
} | ||
|
||
@Override | ||
public void dispatchAllEvents() {} | ||
|
||
@Override | ||
public void addListener(EventDispatcherListener listener) {} | ||
|
||
@Override | ||
public void removeListener(EventDispatcherListener listener) {} | ||
|
||
@Override | ||
public void addBatchEventDispatchedListener(BatchEventDispatchedListener listener) {} | ||
|
||
@Override | ||
public void removeBatchEventDispatchedListener(BatchEventDispatchedListener listener) {} | ||
|
||
@Override | ||
public void registerEventEmitter(int uiManagerType, RCTEventEmitter eventEmitter) {} | ||
|
||
@Override | ||
public void unregisterEventEmitter(int uiManagerType) {} | ||
|
||
@Override | ||
public void onCatalystInstanceDestroyed() {} | ||
} |
24 changes: 24 additions & 0 deletions
24
ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcherProvider.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,24 @@ | ||
/* | ||
* 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.facebook.react.uimanager.events; | ||
|
||
/** | ||
* An interface that can be implemented by a {@link com.facebook.react.bridge.ReactContext} to | ||
* provide a first-class API for accessing the {@link EventDispatcher} from the {@link | ||
* com.facebook.react.bridge.UIManager}. | ||
*/ | ||
public interface EventDispatcherProvider { | ||
|
||
/** | ||
* This method should always return an EventDispatcher, even if the instance doesn't exist; in | ||
* that case it should return the empty {@link BlackHoleEventDispatcher}. | ||
* | ||
* @return An {@link EventDispatcher} to emit events to JS. | ||
*/ | ||
EventDispatcher getEventDispatcher(); | ||
} |