Skip to content
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

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

Open
jeremyhon opened this issue Aug 16, 2024 · 0 comments

Comments

@jeremyhon
Copy link

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.

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

No branches or pull requests

1 participant