-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
[expo-network] Add network state change listeners #28808
base: main
Are you sure you want to change the base?
Conversation
e3b0e33
to
f44f4a0
Compare
f44f4a0
to
930efa8
Compare
packages/expo-network/android/src/main/java/expo/modules/network/NetworkModule.kt
Outdated
Show resolved
Hide resolved
packages/expo-network/android/src/main/java/expo/modules/network/NetworkModule.kt
Outdated
Show resolved
Hide resolved
packages/expo-network/src/Network.ts
Outdated
export function removeNetworkStateListener(subscription: Subscription) { | ||
emitter.removeSubscription(subscription); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this? addNetworkStateListener
will return a subscription. We have the hook to take care of this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I implemented it because some other modules implement it (e.g. expo-clipboard
). But the question is whether removeSubscription
is needed at all, because addListener
returns the remove
function.
packages/expo-network/android/src/main/java/expo/modules/network/NetworkModule.kt
Outdated
Show resolved
Hide resolved
packages/expo-network/android/src/main/java/expo/modules/network/NetworkModule.kt
Outdated
Show resolved
Hide resolved
packages/expo-network/src/Network.ts
Outdated
export { NetworkState, NetworkStateType }; | ||
export { NetworkState, NetworkStateEvent, NetworkStateType }; | ||
|
||
const emitter = new EventEmitter(ExpoNetwork); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As of SDK 51 we no longer need to use additional EventEmitter
to send events. The native module object (ExpoNetwork
here) already inherits from a new emitter class that is written in C++, see:
- type declaration: https://github.com/expo/expo/blob/main/packages/expo-modules-core/src/ts-declarations/EventEmitter.ts
- new implementation: https://github.com/expo/expo/blob/main/packages/expo-modules-core/common/cpp/EventEmitter.cpp
This also solves a long-standing problem with RN's event emitters which are actually global events – event names from different modules can conflict with each other, thus we used to prefix them with something expo-specific.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the hint, I have adjusted it. However, it would be good if this change was also reflected in the documentation and in the template (npx create-expo-module my-module
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we're planning to do this, however:
- We want
create-expo-module
to be compatible with at least one SDK version older than the latest (actually we're going to version it per-SDK but that's another topic). - Our docs generation is not yet adjusted to present class inheritance and supported events. We'll also need a new page in the docs that includes all the public APIs exported by the
expo
package (public and stable APIs fromexpo-modules-core
should be re-exported byexpo
).
All of those should be solved before SDK 52 though.
a8ea357
to
9e02e37
Compare
9e02e37
to
126d74f
Compare
126d74f
to
c07888b
Compare
@alanjhughes @tsapeta Are we ready to merge this? 🙂 |
Why
This PR introduces a approach to handling network state changes across iOS, Android, and Web platforms in the
expo-network
module. It adds new methods to manage listeners that respond to changes in network connectivity and network state, improving the module's utility in reactive network management scenarios.How
To implement this feature, I extended the existing
expo-network
module to include new methods for adding and removing event listeners.Test Plan
Tested it for Android in the emulator, and for iOS on my physical device (iPhone 11 Pro).
Checklist
npx expo prebuild
& EAS Build (eg: updated a module plugin).