Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): avoid send event when has no listener #548

Merged
merged 2 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public abstract class ConnectivityReceiver {
private final WifiManager mWifiManager;
private final TelephonyManager mTelephonyManager;
private final ReactApplicationContext mReactContext;
public boolean hasListener = false;

@Nonnull
private ConnectionType mConnectionType = ConnectionType.UNKNOWN;
Expand Down Expand Up @@ -96,7 +97,9 @@ void updateConnectivity(
mConnectionType = connectionType;
mCellularGeneration = cellularGeneration;
mIsInternetReachable = isInternetReachable;
sendConnectivityChangedEvent();
if (hasListener) {
sendConnectivityChangedEvent();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void initialize() {
public void onCatalystInstanceDestroy() {
mAmazonConnectivityChecker.unregister();
mConnectivityReceiver.unregister();
mConnectivityReceiver.hasListener = false;
}

@Override
Expand All @@ -63,10 +64,14 @@ public void onAmazonFireDeviceConnectivityChanged(boolean isConnected) {
@ReactMethod
public void addListener(String eventName) {
// Keep: Required for RN built in Event Emitter Calls.
mConnectivityReceiver.hasListener = true;
}

@ReactMethod
public void removeListeners(Integer count) {
// Keep: Required for RN built in Event Emitter Calls.
if (count == 0) {
mConnectivityReceiver.hasListener = false;
}
Comment on lines 71 to +75
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think count is the number of listeners to remove, so we need to track the number of listener and if the number of listeners is 0 then hasListener should be false

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch! I peeled it to a new issue for tracking

}
}