@umituz/react-native-subscription 2.37.14 → 2.37.15

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.14",
3
+ "version": "2.37.15",
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",
@@ -19,7 +19,13 @@ class ConfigurationStateManager {
19
19
 
20
20
  startConfiguration(): (value: InitializeResult) => void {
21
21
  if (this._configurationPromise) {
22
- throw new Error('Configuration already in progress');
22
+ // Resolve previous pending configuration as failed to prevent dangling promises
23
+ if (this._resolveConfiguration) {
24
+ const prevResolve = this._resolveConfiguration;
25
+ this._resolveConfiguration = null;
26
+ prevResolve({ success: false, offering: null, isPremium: false });
27
+ }
28
+ this._configurationPromise = null;
23
29
  }
24
30
 
25
31
  // Create promise and store resolve function atomically
@@ -3,10 +3,14 @@ import { getCurrentUserId, setupAuthStateListener } from "../SubscriptionAuthLis
3
3
  import type { SubscriptionInitConfig } from "../SubscriptionInitializerTypes";
4
4
 
5
5
  const AUTH_STATE_DEBOUNCE_MS = 500; // Wait 500ms before processing auth state changes
6
+ const MAX_RETRY_ATTEMPTS = 3;
7
+ const RETRY_DELAY_MS = 2000;
6
8
 
7
9
  export async function startBackgroundInitialization(config: SubscriptionInitConfig): Promise<() => void> {
8
10
  let debounceTimer: ReturnType<typeof setTimeout> | null = null;
11
+ let retryTimer: ReturnType<typeof setTimeout> | null = null;
9
12
  let lastUserId: string | undefined = undefined;
13
+ let lastInitSucceeded = false;
10
14
 
11
15
  const initializeInBackground = async (revenueCatUserId?: string): Promise<void> => {
12
16
  if (typeof __DEV__ !== 'undefined' && __DEV__) {
@@ -15,16 +19,49 @@ export async function startBackgroundInitialization(config: SubscriptionInitConf
15
19
  await SubscriptionManager.initialize(revenueCatUserId);
16
20
  };
17
21
 
22
+ const attemptInitWithRetry = async (revenueCatUserId?: string, attempt = 0): Promise<void> => {
23
+ try {
24
+ await initializeInBackground(revenueCatUserId);
25
+ lastUserId = revenueCatUserId;
26
+ lastInitSucceeded = true;
27
+ } catch (error) {
28
+ lastInitSucceeded = false;
29
+ console.error('[BackgroundInitializer] Initialization failed:', {
30
+ userId: revenueCatUserId,
31
+ attempt: attempt + 1,
32
+ maxAttempts: MAX_RETRY_ATTEMPTS,
33
+ error: error instanceof Error ? error.message : String(error)
34
+ });
35
+
36
+ if (attempt < MAX_RETRY_ATTEMPTS - 1) {
37
+ if (typeof __DEV__ !== 'undefined' && __DEV__) {
38
+ console.log('[BackgroundInitializer] Scheduling retry', { attempt: attempt + 2 });
39
+ }
40
+ retryTimer = setTimeout(() => {
41
+ void attemptInitWithRetry(revenueCatUserId, attempt + 1);
42
+ }, RETRY_DELAY_MS * (attempt + 1));
43
+ } else {
44
+ // After all retries failed, set lastUserId so we don't block
45
+ // but mark as failed so next auth change can retry
46
+ lastUserId = revenueCatUserId;
47
+ }
48
+ }
49
+ };
50
+
18
51
  const debouncedInitialize = (revenueCatUserId?: string): void => {
19
- // Clear any pending initialization
52
+ // Clear any pending initialization or retry
20
53
  if (debounceTimer) {
21
54
  clearTimeout(debounceTimer);
22
55
  }
56
+ if (retryTimer) {
57
+ clearTimeout(retryTimer);
58
+ retryTimer = null;
59
+ }
23
60
 
24
- // If userId hasn't changed, skip
25
- if (lastUserId === revenueCatUserId) {
61
+ // If userId hasn't changed AND last init succeeded, skip
62
+ if (lastUserId === revenueCatUserId && lastInitSucceeded) {
26
63
  if (typeof __DEV__ !== 'undefined' && __DEV__) {
27
- console.log('[BackgroundInitializer] UserId unchanged, skipping reinitialization');
64
+ console.log('[BackgroundInitializer] UserId unchanged and init succeeded, skipping');
28
65
  }
29
66
  return;
30
67
  }
@@ -33,18 +70,7 @@ export async function startBackgroundInitialization(config: SubscriptionInitConf
33
70
  if (typeof __DEV__ !== 'undefined' && __DEV__) {
34
71
  console.log('[BackgroundInitializer] Auth state listener triggered, reinitializing with userId:', revenueCatUserId || '(undefined - anonymous)');
35
72
  }
36
- try {
37
- await initializeInBackground(revenueCatUserId);
38
- lastUserId = revenueCatUserId;
39
- } catch (error) {
40
- // Don't update lastUserId on failure — allow retry on next auth state change
41
- // with the same userId (e.g., network blip recovers)
42
- lastUserId = undefined;
43
- console.error('[BackgroundInitializer] Reinitialization failed:', {
44
- userId: revenueCatUserId,
45
- error: error instanceof Error ? error.message : String(error)
46
- });
47
- }
73
+ void attemptInitWithRetry(revenueCatUserId);
48
74
  }, AUTH_STATE_DEBOUNCE_MS);
49
75
  };
50
76
 
@@ -75,6 +101,9 @@ export async function startBackgroundInitialization(config: SubscriptionInitConf
75
101
  if (debounceTimer) {
76
102
  clearTimeout(debounceTimer);
77
103
  }
104
+ if (retryTimer) {
105
+ clearTimeout(retryTimer);
106
+ }
78
107
  if (unsubscribe) {
79
108
  unsubscribe();
80
109
  }
@@ -4,31 +4,59 @@ interface OfferingsFetcherDeps {
4
4
  isInitialized: () => boolean;
5
5
  }
6
6
 
7
+ const MAX_FETCH_RETRIES = 2;
8
+ const FETCH_RETRY_DELAY_MS = 1500;
9
+
7
10
  export async function fetchOfferings(deps: OfferingsFetcherDeps): Promise<PurchasesOffering | null> {
8
11
  if (!deps.isInitialized()) return null;
9
- try {
10
- const offerings = await Purchases.getOfferings();
11
-
12
- if (__DEV__) {
13
- console.log('[OfferingsFetcher] Offerings received:', {
14
- hasCurrent: !!offerings.current,
15
- currentId: offerings.current?.identifier,
16
- allOfferingsCount: Object.keys(offerings.all).length,
17
- allOfferingIds: Object.keys(offerings.all),
18
- });
19
- }
20
12
 
21
- if (offerings.current) {
22
- return offerings.current;
23
- }
13
+ for (let attempt = 0; attempt <= MAX_FETCH_RETRIES; attempt++) {
14
+ try {
15
+ const offerings = await Purchases.getOfferings();
24
16
 
25
- const allOfferings = Object.values(offerings.all);
26
- if (allOfferings.length > 0) {
27
- return allOfferings[0];
28
- }
17
+ if (typeof __DEV__ !== "undefined" && __DEV__) {
18
+ console.log('[OfferingsFetcher] Offerings received:', {
19
+ attempt,
20
+ hasCurrent: !!offerings.current,
21
+ currentId: offerings.current?.identifier,
22
+ allOfferingsCount: Object.keys(offerings.all).length,
23
+ allOfferingIds: Object.keys(offerings.all),
24
+ });
25
+ }
26
+
27
+ if (offerings.current) {
28
+ return offerings.current;
29
+ }
29
30
 
30
- return null;
31
- } catch (error) {
32
- throw new Error(`Failed to fetch offerings: ${error instanceof Error ? error.message : "Unknown error"}`);
31
+ const allOfferings = Object.values(offerings.all);
32
+ if (allOfferings.length > 0) {
33
+ return allOfferings[0];
34
+ }
35
+
36
+ // No offerings found - retry after delay (RevenueCat may still be syncing)
37
+ if (attempt < MAX_FETCH_RETRIES) {
38
+ if (typeof __DEV__ !== "undefined" && __DEV__) {
39
+ console.log('[OfferingsFetcher] No offerings found, retrying...', { attempt: attempt + 1 });
40
+ }
41
+ await new Promise<void>(resolve => setTimeout(resolve, FETCH_RETRY_DELAY_MS));
42
+ continue;
43
+ }
44
+
45
+ if (typeof __DEV__ !== "undefined" && __DEV__) {
46
+ console.warn('[OfferingsFetcher] No offerings found after all retries');
47
+ }
48
+ return null;
49
+ } catch (error) {
50
+ if (attempt < MAX_FETCH_RETRIES) {
51
+ if (typeof __DEV__ !== "undefined" && __DEV__) {
52
+ console.warn('[OfferingsFetcher] Fetch failed, retrying...', { attempt: attempt + 1, error });
53
+ }
54
+ await new Promise<void>(resolve => setTimeout(resolve, FETCH_RETRY_DELAY_MS));
55
+ continue;
56
+ }
57
+ throw new Error(`Failed to fetch offerings: ${error instanceof Error ? error.message : "Unknown error"}`);
58
+ }
33
59
  }
60
+
61
+ return null;
34
62
  }