Skip to content

Conversation

hoangvvo
Copy link
Collaborator

@hoangvvo hoangvvo commented Apr 21, 2022

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 involving react-native-events 🎉

Implementation status

Android: I have tested this on Android, and it works alright!

image

IOS: I do not know how to program c/ios nor have a Mac to test out the changes, but I tried my best to write the c 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, since NativeEventEmitter 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 called events. This is because our native module contains the native method addListener, 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.

@hoangvvo hoangvvo changed the title Use react-native NativeEventEmitter feat: use react-native NativeEventEmitter Apr 21, 2022
@hoangvvo

This comment was marked as outdated.

this is already achieved
@cjam
Copy link
Owner

cjam commented Apr 23, 2022

Wow, just saw this!
Thanks for contribution! I'll review this PR as soon as I can.

Dependency free! Would love to see if we could avoid the breaking change, but obviously can if needed.

@hoangvvo hoangvvo requested a review from cjam May 24, 2022 08:21
*/
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;
Copy link
Owner

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done b6abe5b

},
removeListener(eventType, listener) {
eventListeners[eventType].forEach((eventListener) => {
if (!listener || eventListener.listener === listener) {
Copy link
Owner

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.

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

@hoangvvo hoangvvo Jun 11, 2022

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

nativeModule.eventStartObserving(eventType);
}
eventListeners[eventType].add(sub);
return this;
Copy link
Owner

@cjam cjam May 24, 2022

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

https://github.com/facebook/react-native/blob/2596b2f6954362d2cd34a1be870810ab90cbb916/Libraries/vendor/emitter/_EventEmitter.js#L81-L92

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done via b6abe5b

Comment on lines +258 to +266
const eventListeners: Record<
keyof SpotifyRemoteEvents,
Set<EmitterSubscription>
> = {
playerContextChanged: new Set(),
playerStateChanged: new Set(),
remoteConnected: new Set(),
remoteDisconnected: new Set(),
};
Copy link
Owner

Choose a reason for hiding this comment

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

👍

@cjam
Copy link
Owner

cjam commented Jun 4, 2022

@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 Unreleased section of the CHANGELOG.md as part of this PR that would be great.

@hoangvvo hoangvvo requested a review from cjam June 11, 2022 11:35
Copy link
Owner

@cjam cjam left a 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.

@hoangvvo
Copy link
Collaborator Author

@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.

@cjam cjam merged commit 1f99933 into cjam:master Jun 13, 2022
@cjam
Copy link
Owner

cjam commented Jun 17, 2022

@hoangvvo this has been released in @next (1.0.0-1)

ahbou added a commit to TTFM-Labs/react-native-spotify-remote that referenced this pull request Dec 23, 2022
* 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)
theshadowagent pushed a commit to theshadowagent/react-native-spotify-remote that referenced this pull request Jan 24, 2023
* Use NativeEventEmitter
* Adopt react-native event emitter signature
* Update docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants