@umituz/react-native-subscription 2.37.29 → 2.37.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umituz/react-native-subscription",
3
- "version": "2.37.29",
3
+ "version": "2.37.31",
4
4
  "description": "Complete subscription management with RevenueCat, paywall UI, and credits system for React Native apps",
5
5
  "main": "./src/index.ts",
6
6
  "types": "./src/index.ts",
@@ -9,7 +9,23 @@ declare const __DEV__: boolean;
9
9
 
10
10
  function buildSuccessResult(deps: InitializerDeps, customerInfo: CustomerInfo, offerings: any): InitializeResult {
11
11
  const isPremium = !!customerInfo.entitlements.active[deps.config.entitlementIdentifier];
12
- return { success: true, offering: offerings.current, isPremium };
12
+ return { success: true, offering: offerings?.current ?? null, isPremium };
13
+ }
14
+
15
+ /**
16
+ * Fetch offerings separately - non-fatal if it fails.
17
+ * Empty offerings (no products configured in RevenueCat dashboard) should NOT
18
+ * block SDK initialization. The SDK is still usable for premium checks, purchases, etc.
19
+ */
20
+ async function fetchOfferingsSafe(): Promise<any> {
21
+ try {
22
+ return await Purchases.getOfferings();
23
+ } catch (error) {
24
+ if (typeof __DEV__ !== 'undefined' && __DEV__) {
25
+ console.warn('[UserSwitchHandler] Offerings fetch failed (non-fatal):', error);
26
+ }
27
+ return { current: null, all: {} };
28
+ }
13
29
  }
14
30
 
15
31
  function normalizeUserId(userId: string): string | null {
@@ -87,7 +103,7 @@ async function performUserSwitch(
87
103
 
88
104
  deps.setInitialized(true);
89
105
  deps.setCurrentUserId(normalizedUserId || undefined);
90
- const offerings = await Purchases.getOfferings();
106
+ const offerings = await fetchOfferingsSafe();
91
107
 
92
108
  if (typeof __DEV__ !== 'undefined' && __DEV__) {
93
109
  console.log('[UserSwitchHandler] ✅ User switch completed successfully');
@@ -136,9 +152,11 @@ export async function handleInitialConfiguration(
136
152
  console.log('[UserSwitchHandler] ✅ Purchases.configure() successful');
137
153
  }
138
154
 
155
+ // Fetch customer info (critical) and offerings (non-fatal) separately.
156
+ // Empty offerings should NOT block initialization - SDK is still usable.
139
157
  const [customerInfo, offerings] = await Promise.all([
140
158
  Purchases.getCustomerInfo(),
141
- Purchases.getOfferings(),
159
+ fetchOfferingsSafe(),
142
160
  ]);
143
161
 
144
162
  const currentUserId = await Purchases.getAppUserID();
@@ -198,11 +216,11 @@ export async function fetchCurrentUserData(deps: InitializerDeps): Promise<Initi
198
216
  try {
199
217
  const [customerInfo, offerings] = await Promise.all([
200
218
  Purchases.getCustomerInfo(),
201
- Purchases.getOfferings(),
219
+ fetchOfferingsSafe(),
202
220
  ]);
203
221
  return buildSuccessResult(deps, customerInfo, offerings);
204
222
  } catch (error) {
205
- console.error('[UserSwitchHandler] Failed to fetch customer info/offerings for initialized user', {
223
+ console.error('[UserSwitchHandler] Failed to fetch customer info for initialized user', {
206
224
  error
207
225
  });
208
226
  return FAILED_INITIALIZATION_RESULT;
@@ -74,9 +74,13 @@ class SubscriptionManagerImpl {
74
74
  this.serviceInstance = service ?? null;
75
75
  this.ensurePackageHandlerInitialized();
76
76
 
77
- if (success) {
78
- // Notify reactive state so React components re-render and enable their queries
79
- const notifyUserId = (userId && userId.length > 0) ? userId : null;
77
+ // Always notify reactive state when service is available, even if offerings fetch failed.
78
+ // This ensures React components (useSubscriptionPackages, useSubscriptionStatus) are unblocked.
79
+ // The service IS configured and usable - empty offerings is not a fatal error.
80
+ const notifyUserId = (userId && userId.length > 0) ? userId : null;
81
+ if (service) {
82
+ initializationState.markInitialized(notifyUserId);
83
+ } else if (success) {
80
84
  initializationState.markInitialized(notifyUserId);
81
85
  }
82
86
 
@@ -54,7 +54,10 @@ export async function fetchOfferings(deps: OfferingsFetcherDeps): Promise<Purcha
54
54
  await new Promise<void>(resolve => setTimeout(resolve, FETCH_RETRY_DELAY_MS));
55
55
  continue;
56
56
  }
57
- throw new Error(`Failed to fetch offerings: ${error instanceof Error ? error.message : "Unknown error"}`);
57
+ if (typeof __DEV__ !== "undefined" && __DEV__) {
58
+ console.warn('[OfferingsFetcher] Failed to fetch offerings after all retries:', error);
59
+ }
60
+ return null;
58
61
  }
59
62
  }
60
63
 
@@ -17,6 +17,11 @@ export function useFeatureGate(params: UseFeatureGateParams): UseFeatureGateResu
17
17
 
18
18
  const pendingActionRef = useRef<(() => void | Promise<void>) | null>(null);
19
19
  const prevCreditBalanceRef = useRef(creditBalance);
20
+ // Separate ref to track previous subscription state for canExecutePurchaseAction.
21
+ // NOTE: Must NOT use hasSubscriptionRef from useSyncedRefs here because useSyncedRefs
22
+ // effects run BEFORE this effect (React runs effects in definition order), so
23
+ // hasSubscriptionRef.current would already be the NEW value when we check it.
24
+ const prevHasSubscriptionRef = useRef(hasSubscription);
20
25
  const isWaitingForPurchaseRef = useRef(false);
21
26
  const isWaitingForAuthCreditsRef = useRef(false);
22
27
 
@@ -50,13 +55,14 @@ export function useFeatureGate(params: UseFeatureGateParams): UseFeatureGateResu
50
55
  }, [isCreditsLoaded, creditBalance, hasSubscription, requiredCredits, onShowPaywallRef, requiredCreditsRef]);
51
56
 
52
57
  useEffect(() => {
53
-
58
+ // Use prevHasSubscriptionRef (updated AFTER check) not hasSubscriptionRef from useSyncedRefs
59
+ // (which is already updated to new value before this effect runs - race condition fix)
54
60
  const shouldExecute = canExecutePurchaseAction(
55
61
  isWaitingForPurchaseRef.current,
56
62
  creditBalance,
57
63
  prevCreditBalanceRef.current ?? 0,
58
64
  hasSubscription,
59
- hasSubscriptionRef.current,
65
+ prevHasSubscriptionRef.current,
60
66
  !!pendingActionRef.current
61
67
  );
62
68
 
@@ -67,9 +73,10 @@ export function useFeatureGate(params: UseFeatureGateParams): UseFeatureGateResu
67
73
  action();
68
74
  }
69
75
 
76
+ // Update AFTER check so next render has correct "prev" values
70
77
  prevCreditBalanceRef.current = creditBalance;
71
- // hasSubscriptionRef is already synced by useSyncedRefs, no need to update manually
72
- // eslint-disable-next-line react-hooks/exhaustive-deps
78
+ prevHasSubscriptionRef.current = hasSubscription;
79
+
73
80
  }, [creditBalance, hasSubscription]);
74
81
 
75
82
  const requireFeature = useCallback(