-
Notifications
You must be signed in to change notification settings - Fork 109
feat: use react-native NativeEventEmitter #189
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
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
this is already achieved
Wow, just saw this! Dependency free! Would love to see if we could avoid the breaking change, but obviously can if needed. |
android/src/main/java/com/reactlibrary/RNSpotifyRemoteAppModule.java
Outdated
Show resolved
Hide resolved
Since it will be transpiled again by Metro anyway
src/TypedEventEmitter.ts
Outdated
*/ | ||
export default interface TypedEventEmitter<T> { | ||
on<K extends keyof T>(name: K, listener: (v: T[K]) => void): this; | ||
addListener<K extends keyof T>(event: K, listener: (v: T[K]) => void): 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 think where appropriate we can use the new interface. So here, we could probably return the EmitterSubscription
. It is a breaking change, but aligns with the NativeEventEmitter implementation better
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.
Done b6abe5b
src/SpotifyRemote.ts
Outdated
}, | ||
removeListener(eventType, listener) { | ||
eventListeners[eventType].forEach((eventListener) => { | ||
if (!listener || eventListener.listener === listener) { |
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 think we only need to pass the listener here. And if no listener gets passed in, we just do nothing rather than unsubscribing all existing listeners.
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.
Unsubscribe everything in case no listener is a pattern employed in many event emitter libs. It also helps our other method unsubscribeAllListeners.
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.
b6abe5b should also solve this. we no longer allow passing in no function
src/SpotifyRemote.ts
Outdated
nativeModule.eventStartObserving(eventType); | ||
} | ||
eventListeners[eventType].add(sub); | ||
return 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.
If we return the sub
here. Then this method looks exactly like the nativeEventEmitter and we can add deprecations to removeListener
below similar to here
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.
Done via b6abe5b
const eventListeners: Record< | ||
keyof SpotifyRemoteEvents, | ||
Set<EmitterSubscription> | ||
> = { | ||
playerContextChanged: new Set(), | ||
playerStateChanged: new Set(), | ||
remoteConnected: new Set(), | ||
remoteDisconnected: new Set(), | ||
}; |
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.
👍
@hoangvvo Thanks again for this PR. Looks like a recent update I made introduced a few conflicts with the readme and the docs. Feel free to take the incoming changes and I'll update the contributors stuff after this PR is finished. Also if you could add an entry to the |
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.
This looks great, thanks for putting in those changes.
I think you may need to rebase as there was one small PR around the build.gradle version.
Thanks for all your work on this.
I'll have to double check that everything builds on iOS, but might take care of that once this get's merged as sometimes I find it awkward to participate in external PR's.
@hoangvvo if you're interested in contributing more in the future, I can add you as a contributor that would allow you to create branches / PR's directly in this Repo instead of from your fork.
That would be great. I am actively using this library in some of my side projects and would not mind maintaining it as well. |
@hoangvvo this has been released in |
* upstream/master: gradle flatDir to sourceSets (cjam#205) chore: release 1.0.0-1 Fix iOS Build (cjam#202) feat: use react-native NativeEventEmitter (cjam#189) Update gradle to direct include aar files (cjam#199) chore: release 0.3.11-6 Update contributors and changelog Add contribute script and Pull request template Add a null check when mapping tracks (cjam#192)
* Use NativeEventEmitter * Adopt react-native event emitter signature * Update docs
This makes use directly of react-native NativeEventEmitter (https://reactnative.dev/docs/native-modules-android#sending-events-to-javascript) and remove
react-native-events
. Thus, the library is now dependency-free and also avoids the trouble of autolinking involvingreact-native-events
🎉Implementation status
Android: I have tested this on Android, and it works alright!
IOS: I do not know how to program
c
/ios
nor have aMac
to test out the changes, but I tried my best to write thec
code. It would be great if you can assist me on that. See https://reactnative.dev/docs/native-modules-ios#sending-events-to-javascript for references.Breaking changes
However, sinceNativeEventEmitter
signature is not the same as our previous event emitter, I have decided to make the breaking change to move the event emitter to a property calledevents
. This is because our native module contains the native methodaddListener
, which would break if I try to attach the methods directly on the object.The latest commit succeeded in maintaining the event emitter API. I have not added back some of the APIs that are not commonly used (See https://github.com/cjam/react-native-spotify-remote/pull/189/files#diff-2cdc799d0278758d580b911374652f261c50cb134076f329d0ab0b406bcd2623), but we can do it if we want to.