Purchasely / Purchasely-ReactNative

Other
13 stars 2 forks source link

RCTCallableJSModules is not set. This is probably because you've explicitly synthesized the RCTCallableJSModules in PurchaselyRN, even though it's inherited from RCTEventEmitter. #88

Closed filipboev closed 8 months ago

filipboev commented 8 months ago

Noticed here that you're subclassing RCTEventEmitter https://github.com/Purchasely/Purchasely-ReactNative/blob/be5075c9ab123bff0cd080168a78b8f81647de5c/purchasely/ios/PurchaselyRN.h#L11

But in PurchaselyRN.m there isn't any way to stop events from being emitted if there are no active listeners yet. https://github.com/Purchasely/Purchasely-ReactNative/blob/be5075c9ab123bff0cd080168a78b8f81647de5c/purchasely/ios/PurchaselyRN.m#L16

Which tends to lead to exceptions like these:

*** Assertion failure in -[RCTEventEmitter sendEventWithName:body:](), [REDACTED]/node_modules/react-native/React/Modules/RCTEventEmitter.m:53
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error when sending event: PURCHASELY_EVENTS with body: {
    name = "APP_CONFIGURED";
    properties =     {
        "app_installed_at" = "2024-01-10T15:41:11.539Z";
        "app_installed_at_ms" = 1704901271539;
        device = "[REDACTED]";
        "event_created_at" = "2024-01-15T14:18:59.689Z";
        "event_created_at_ms" = 1705328339689;
        "event_name" = "APP_CONFIGURED";
        "os_version" = "iOS 17.2.1";
        "sdk_version" = "4.2.1";
        "session_count" = 14;
        "session_duration" = 18;
        type = PHONE;
        "user_id" = "[REDACTED]";
    };
}. RCTCallableJSModules is not set. This is probably because you've explicitly synthesized the RCTCallableJSModules in PurchaselyRN, even though it's inherited from RCTEventEmitter.'
*** First throw call stack:
(0x1a2fa669c 0x19b25bc80 0x1a2504f14 0x101a67150 0x1022e7a20 0x107289558 0x1072d4ce0 0x1aaeab6a8 0x1aaead300 0x1aaebb998 0x1aaebb5b0 0x1a2ef101c 0x1a2eedd28 0x1a2eed478 0x1e64464f8 0x1a531162c 0x1a5310c68 0x100d1332c 0x1c5c12dcc)
libc++abi: terminating due to uncaught exception of type NSException

I noticed this happening often on a debug build, but my guess is it might also happen on a release build since I've seen other native modules have the same problem in the past.

React Native has these two methods from RCTEventEmitter that you can override to handle the cases where you have no listeners yet.

- (void)startObserving;
- (void)stopObserving;

So something like this should do the trick and won't expend resources unnecessarily.

- (void)startObserving
{
  _shouldEmit = YES;
}

- (void)stopObserving
{
  _shouldEmit = NO;
}

- (void)eventTriggered:(enum PLYEvent)event properties:(NSDictionary<NSString *, id> * _Nullable)properties {
    if(_shouldEmit) {
        if (properties != nil) {
            NSDictionary<NSString *, id> *body = @{@"name": [NSString fromPLYEvent:event], @"properties": properties};
            [self sendEventWithName: @"PURCHASELY_EVENTS" body: body];
        } else {
            NSDictionary<NSString *, id> *body = @{@"name": [NSString fromPLYEvent:event]};
            [self sendEventWithName:@"PURCHASELY_EVENTS" body:body];
        }
    }
}

Environment

System:
  OS: macOS 14.2
  CPU: (10) arm64 Apple M1 Pro
  Memory: 114.70 MB / 32.00 GB
Binaries:
  Node:
    version: 21.5.0
    path: /usr/local/bin/node
  Yarn:
    version: 1.22.19
    path: /opt/homebrew/bin/yarn
  npm:
    version: 10.2.4
    path: /opt/homebrew/bin/npm
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.2
      - iOS 17.2
      - macOS 14.2
      - tvOS 17.2
      - visionOS 1.0
      - watchOS 10.2
IDEs:
  Xcode:
    version: 15.2/15C500b
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 11.0.21
    path: /usr/bin/javac
  Ruby:
    version: 3.1.4
npmPackages:
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.72.7
    wanted: 0.72.7
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: false
  newArchEnabled: false
EPIKorial commented 8 months ago

Hello @filipboev,

Thank you for bringing this issue to our attention. It looks like you've provided a viable solution to the problem, which is commendable!

We'll conduct thorough testing on our end and incorporate it into the upcoming release. We'll keep you informed once the release is live.

Best regards.