hms-react-native-plugin icon indicating copy to clipboard operation
hms-react-native-plugin copied to clipboard

Invariant Violation: `new NativeEventEmitter()` requires a non-null argument., js engine: hermes

Open jeremyhon opened this issue 1 year ago • 1 comments

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch @hmscore/[email protected] for the project I'm working on.

The code was throwing an error on iOS due to initializing NativeEventEmitter without a native module. This code exists in ActivityIdentification.js, Geofence.js as well as FusedLocation.js. Example below.

const HMSActivityIdentificationEmitter = new NativeEventEmitter();

This error can be safely ignored because this package isn't expected to run on iOS (and provides no iOS code). But we still need a way to safely ignore it.

I chose to change the code inside index.js such that the code would not be imported or run on iOS. There's probably a better way to do it. Here is the diff that solved my problem:

diff --git a/node_modules/@hmscore/react-native-hms-location/src/index.js b/node_modules/@hmscore/react-native-hms-location/src/index.js
index 3d5fb4e..92f427c 100644
--- a/node_modules/@hmscore/react-native-hms-location/src/index.js
+++ b/node_modules/@hmscore/react-native-hms-location/src/index.js
@@ -14,73 +14,74 @@
     limitations under the License.
 */
 
+import { Platform } from 'react-native';
 import HMSLocationKit from './modules/LocationKit';
-
-import HMSActivityIdentification, {
-    registerActivityIdentificationHeadlessTask,
-    addActivityIdentificationEventListener,
-    removeActivityIdentificationEventListener,
-    registerActivityConversionHeadlessTask,
-    addActivityConversionEventListener,
-    removeActivityConversionEventListener
-} from './modules/ActivityIdentification';
-
-import HMSFusedLocation, {
-    registerFusedLocationHeadlessTask,
-    addFusedLocationEventListener,
-    removeFusedLocationEventListener,
-} from './modules/FusedLocation';
-
-import HMSGeofence, {
-    registerGeofenceHeadlessTask,
-    addGeofenceEventListener,
-    removeGeofenceEventListener
-} from './modules/Geofence';
-
 import HMSGeocoder from './modules/Geocoder';
 
 const LocationKit = {
     Native: HMSLocationKit
 };
 
-const Geofence = {
-    Native: HMSGeofence,
-    Events: {
-        registerGeofenceHeadlessTask,
-        addGeofenceEventListener,
-        removeGeofenceEventListener
-    }
-};
-
-const FusedLocation = {
-    Native: HMSFusedLocation,
-    Events: {
-        registerFusedLocationHeadlessTask,
-        addFusedLocationEventListener,
-        removeFusedLocationEventListener,
-    },
-};
-
-const ActivityIdentification = {
-    Native: HMSActivityIdentification,
-    Events: {
-        registerActivityIdentificationHeadlessTask,
-        addActivityIdentificationEventListener,
-        removeActivityIdentificationEventListener,
-        registerActivityConversionHeadlessTask,
-        addActivityConversionEventListener,
-        removeActivityConversionEventListener
-    }
-};
-
 const Geocoder = {
     Native: HMSGeocoder
 };
 
-export default {
+let ActivityIdentification = {};
+let Geofence = {};
+let FusedLocation = {};
+
+const exported = {
     LocationKit,
     Geofence,
     FusedLocation,
     ActivityIdentification,
     Geocoder
-};
+}
+
+// Guard clause for non-Android platforms
+if (Platform.OS !== 'android') {
+    module.exports = {
+        LocationKit,
+        Geofence: {},
+        FusedLocation: {},
+        ActivityIdentification: {},
+        Geocoder
+    };
+} else {
+    // Android-specific imports and setup
+    const HMSActivityIdentification = require('./modules/ActivityIdentification');
+    const HMSGeofence = require('./modules/Geofence');
+    const HMSFusedLocation = require('./modules/FusedLocation');
+
+    module.exports = {
+        LocationKit,
+        Geofence: {
+            Native: HMSGeofence.default,
+            Events: {
+                registerGeofenceHeadlessTask: HMSGeofence.registerGeofenceHeadlessTask,
+                addGeofenceEventListener: HMSGeofence.addGeofenceEventListener,
+                removeGeofenceEventListener: HMSGeofence.removeGeofenceEventListener
+            }
+        },
+        FusedLocation: {
+            Native: HMSFusedLocation.default,
+            Events: {
+                registerFusedLocationHeadlessTask: HMSFusedLocation.registerFusedLocationHeadlessTask,
+                addFusedLocationEventListener: HMSFusedLocation.addFusedLocationEventListener,
+                removeFusedLocationEventListener: HMSFusedLocation.removeFusedLocationEventListener,
+            },
+        },
+        ActivityIdentification: {
+            Native: HMSActivityIdentification.default,
+            Events: {
+                registerActivityIdentificationHeadlessTask: HMSActivityIdentification.registerActivityIdentificationHeadlessTask,
+                addActivityIdentificationEventListener: HMSActivityIdentification.addActivityIdentificationEventListener,
+                removeActivityIdentificationEventListener: HMSActivityIdentification.removeActivityIdentificationEventListener,
+                registerActivityConversionHeadlessTask: HMSActivityIdentification.registerActivityConversionHeadlessTask,
+                addActivityConversionEventListener: HMSActivityIdentification.addActivityConversionEventListener,
+                removeActivityConversionEventListener: HMSActivityIdentification.removeActivityConversionEventListener
+            }
+        },
+        Geocoder
+    };
+}
\ No newline at end of file

Hope the team can fix this soon! Thanks.

This issue body was partially generated by patch-package.

jeremyhon avatar Aug 16 '24 06:08 jeremyhon