adaptyteam / AdaptySDK-React-Native

React Native SDK for growing in-app subscriptions
https://docs.adapty.io/docs/quickstart
MIT License
125 stars 12 forks source link

Best practice for adapty.activate #83

Closed WrathChaos closed 1 year ago

WrathChaos commented 1 year ago

Description

CleanShot 2023-08-17 at 18 53 14 https://docs.adapty.io/docs/react-native-configuring#delaying-sdk-activation

As it says on this documentation, Preferably, place activation before React component however, if I am doing this there is a problem for async-await warning.

CleanShot 2023-08-17 at 18 54 29

How should we do it properly with the new 2.6.0?

Thank you 🍻

System info

No response

divanc commented 1 year ago

Merhaba, Ogün! Documentation displays a simple workable example, but your analysis tool is right (kind of)! The thing is activate can actually throw, and that's what your linter is concerned about. You see, some errors might kill your app, if unhandled. In RN debug it looks like unclosable black&red box, in prod — app just quits.

P.S. I'm not intimately familiar with how RN runtime works, but if we consider it works the same way as Node, then uncaught promise error like activate call SHOULDN'T terminate your app, they should only result into a console.error.

While we provide several possible errors in activation, the only activation error I've seen in years is when you provide invalid public API key. Hence, nothing in Adapty SDK would work.

You can choose to just wrap it into a catching function and work with it how you like, but you will probably still need to suppress this rule for one line, depending on how your tool decides what to warn. (it might be due to JSDoc @throws directive in activate)

async function activateSdks() {
  try {
    await adapty.activate(...)
    ...
  } catch (error) {
    // msg to Slack?
  }
}

// before core component
// LINTER SUPRESS UNHANDLED EXCEPTION
activateSdks();

You also can just imitate a sync function, so that your linter does not know it can reject, but you should be very cautious about how this code is interpreted by computer and other programmers:

function promiseToActivateSdks() {
  adapty.activate(...);
  // try-catch wouldn't work here
}

// before core component
promiseToActivateSdks();

@WrathChaos

divanc commented 1 year ago

Please, re-open this issue, if this question is still unanswered to you!

WrathChaos commented 1 year ago

Hey @divanc Sorry, I was on vacation. I will take a look at the answer today!