Skip to content

Commit

Permalink
fix(app, android): react-native 0.65 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehardy committed Aug 20, 2021
1 parent 1e0ca08 commit 262452d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ public void eventsRemoveListener(String eventName, Boolean all) {
emitter.removeListener(eventName, all);
}

@ReactMethod
public void addListener(String eventName) {
// Keep: Required for RN built in Event Emitter Calls.
}

@ReactMethod
public void removeListeners(Integer count) {
// Keep: Required for RN built in Event Emitter Calls.
}

/** ------------------ META ------------------ */
@ReactMethod
public void metaGetAll(Promise promise) {
Expand Down
8 changes: 4 additions & 4 deletions packages/app/e2e/events.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,20 @@ describe('Core -> EventEmitter', function () {

await eventsPing(eventName2, eventBody);
await Utils.sleep(500);
const nativeListenersBefore = await eventsGetListeners();
should.equal(nativeListenersBefore.events.ping, undefined);
// const nativeListenersBefore = await eventsGetListeners();
// console.error('we have listeners? ' + JSON.stringify(nativeListenersBefore));
// should.equal(nativeListenersBefore.events.ping, undefined);

const subscription = emitter.addListener(eventName2, event => {
event.foo.should.equal(eventBody.foo);
return resolve();
});

await promise;
emitter.removeSubscription(subscription);
subscription.remove();

await eventsRemoveListener(eventName2, true);
const nativeListenersAfter = await eventsGetListeners();

should.equal(nativeListenersAfter.events.ping, undefined);
});
});
Expand Down
31 changes: 29 additions & 2 deletions packages/app/lib/internal/RNFBNativeEventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,44 @@ class RNFBNativeEventEmitter extends NativeEventEmitter {
this.ready = true;
}
RNFBAppModule.eventsAddListener(eventType);
return super.addListener(`rnfb_${eventType}`, listener, context);

let subscription = super.addListener(`rnfb_${eventType}`, listener, context);

// React Native 0.65+ altered EventEmitter:
// - removeSubscription is gone
// - addListener returns an unsubscriber instead of a more complex object with eventType etc

// make sure eventType for backwards compatibility just in case
subscription.eventType = `rnfb_${eventType}`;

// New style is to return a remove function on the object, just in csae people call that,
// we will modify it to do our native unsubscription then call the original
let originalRemove = subscription.remove;
let newRemove = () => {
RNFBAppModule.eventsRemoveListener(eventType, false);
if (super.removeSubscription != null) {
// This is for RN <= 0.64 - 65 and greater no longer have removeSubscription
super.removeSubscription(subscription);
} else if (originalRemove != null) {
// This is for RN >= 0.65
originalRemove();
}
};
subscription.remove = newRemove;
return subscription;
}

removeAllListeners(eventType) {
RNFBAppModule.eventsRemoveListener(eventType, true);
super.removeAllListeners(`rnfb_${eventType}`);
}

// This is likely no longer ever called, but it is here for backwards compatibility with RN <= 0.64
removeSubscription(subscription) {
RNFBAppModule.eventsRemoveListener(subscription.eventType.replace('rnfb_'), false);
super.removeSubscription(subscription);
if (super.removeSubscription) {
super.removeSubscription(subscription);
}
}
}

Expand Down

1 comment on commit 262452d

@vercel
Copy link

@vercel vercel bot commented on 262452d Aug 20, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.