@umituz/react-native-subscription 2.37.14 → 2.37.16
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 +1 -1
- package/src/domains/revenuecat/infrastructure/services/ConfigurationStateManager.ts +7 -1
- package/src/domains/subscription/application/initializer/BackgroundInitializer.ts +46 -16
- package/src/domains/subscription/infrastructure/hooks/useSubscriptionPackages.ts +13 -4
- package/src/domains/subscription/infrastructure/managers/SubscriptionManager.ts +11 -0
- package/src/domains/subscription/infrastructure/services/OfferingsFetcher.ts +49 -21
- package/src/domains/subscription/infrastructure/state/initializationState.ts +66 -0
- package/src/domains/subscription/presentation/useSubscriptionStatus.ts +14 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/react-native-subscription",
|
|
3
|
-
"version": "2.37.
|
|
3
|
+
"version": "2.37.16",
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
|
@@ -65,6 +91,7 @@ export async function startBackgroundInitialization(config: SubscriptionInitConf
|
|
|
65
91
|
// This prevents a Firestore permission-denied error from querying with an anonymous ID.
|
|
66
92
|
if (initialRevenueCatUserId) {
|
|
67
93
|
await initializeInBackground(initialRevenueCatUserId);
|
|
94
|
+
lastInitSucceeded = true;
|
|
68
95
|
} else if (typeof __DEV__ !== 'undefined' && __DEV__) {
|
|
69
96
|
console.log('[BackgroundInitializer] Skipping anonymous init, waiting for auth state');
|
|
70
97
|
}
|
|
@@ -75,6 +102,9 @@ export async function startBackgroundInitialization(config: SubscriptionInitConf
|
|
|
75
102
|
if (debounceTimer) {
|
|
76
103
|
clearTimeout(debounceTimer);
|
|
77
104
|
}
|
|
105
|
+
if (retryTimer) {
|
|
106
|
+
clearTimeout(retryTimer);
|
|
107
|
+
}
|
|
78
108
|
if (unsubscribe) {
|
|
79
109
|
unsubscribe();
|
|
80
110
|
}
|
|
@@ -5,12 +5,13 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import { useQuery, useQueryClient } from "@umituz/react-native-design-system";
|
|
8
|
-
import { useEffect, useRef } from "react";
|
|
8
|
+
import { useEffect, useRef, useSyncExternalStore } from "react";
|
|
9
9
|
import {
|
|
10
10
|
useAuthStore,
|
|
11
11
|
selectUserId,
|
|
12
12
|
} from "@umituz/react-native-auth";
|
|
13
13
|
import { SubscriptionManager } from '../../infrastructure/managers/SubscriptionManager';
|
|
14
|
+
import { initializationState } from "../../infrastructure/state/initializationState";
|
|
14
15
|
import {
|
|
15
16
|
SUBSCRIPTION_QUERY_KEYS,
|
|
16
17
|
} from "./subscriptionQueryKeys";
|
|
@@ -26,10 +27,18 @@ export const useSubscriptionPackages = () => {
|
|
|
26
27
|
const queryClient = useQueryClient();
|
|
27
28
|
const prevUserIdRef = useRef(userId);
|
|
28
29
|
|
|
29
|
-
//
|
|
30
|
+
// Reactive initialization state - triggers re-render when BackgroundInitializer completes
|
|
31
|
+
const initState = useSyncExternalStore(
|
|
32
|
+
initializationState.subscribe,
|
|
33
|
+
initializationState.getSnapshot,
|
|
34
|
+
initializationState.getSnapshot,
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
// For authenticated users: check reactive state matches current userId
|
|
38
|
+
// For anonymous users: check if initialized at all (reactive state or manager state)
|
|
30
39
|
const isInitialized = userId
|
|
31
|
-
?
|
|
32
|
-
: SubscriptionManager.isInitialized();
|
|
40
|
+
? initState.initialized && initState.userId === userId
|
|
41
|
+
: initState.initialized || SubscriptionManager.isInitialized();
|
|
33
42
|
|
|
34
43
|
const query = useQuery({
|
|
35
44
|
queryKey: [...SUBSCRIPTION_QUERY_KEYS.packages, userId ?? "anonymous"] as const,
|
|
@@ -8,6 +8,7 @@ import { createPackageHandler } from "./packageHandlerFactory";
|
|
|
8
8
|
import { checkPremiumStatusFromService } from "./premiumStatusChecker";
|
|
9
9
|
import { getPackagesOperation, purchasePackageOperation, restoreOperation } from "./managerOperations";
|
|
10
10
|
import { performServiceInitialization } from "./initializationHandler";
|
|
11
|
+
import { initializationState } from "../state/initializationState";
|
|
11
12
|
|
|
12
13
|
class SubscriptionManagerImpl {
|
|
13
14
|
private managerConfig: SubscriptionManagerConfig | null = null;
|
|
@@ -52,6 +53,9 @@ class SubscriptionManagerImpl {
|
|
|
52
53
|
return existingPromise;
|
|
53
54
|
}
|
|
54
55
|
|
|
56
|
+
// Mark pending so React components know to wait
|
|
57
|
+
initializationState.markPending();
|
|
58
|
+
|
|
55
59
|
const promise = this.performInitialization(actualUserId);
|
|
56
60
|
this.state.initCache.setPromise(promise, cacheKey);
|
|
57
61
|
return promise;
|
|
@@ -70,6 +74,12 @@ class SubscriptionManagerImpl {
|
|
|
70
74
|
this.serviceInstance = service ?? null;
|
|
71
75
|
this.ensurePackageHandlerInitialized();
|
|
72
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;
|
|
80
|
+
initializationState.markInitialized(notifyUserId);
|
|
81
|
+
}
|
|
82
|
+
|
|
73
83
|
if (typeof __DEV__ !== 'undefined' && __DEV__) {
|
|
74
84
|
console.log('[SubscriptionManager] Initialization completed:', { success });
|
|
75
85
|
}
|
|
@@ -111,6 +121,7 @@ class SubscriptionManagerImpl {
|
|
|
111
121
|
this.state.reset();
|
|
112
122
|
this.serviceInstance = null;
|
|
113
123
|
this.packageHandler = null;
|
|
124
|
+
initializationState.reset();
|
|
114
125
|
}
|
|
115
126
|
|
|
116
127
|
isConfigured = (): boolean => this.managerConfig !== null;
|
|
@@ -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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
13
|
+
for (let attempt = 0; attempt <= MAX_FETCH_RETRIES; attempt++) {
|
|
14
|
+
try {
|
|
15
|
+
const offerings = await Purchases.getOfferings();
|
|
24
16
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reactive Initialization State
|
|
3
|
+
* Uses useSyncExternalStore pattern to make SubscriptionManager
|
|
4
|
+
* initialization state reactive for React components.
|
|
5
|
+
*
|
|
6
|
+
* Problem: SubscriptionManager.isInitializedForUser() is a plain method call.
|
|
7
|
+
* When BackgroundInitializer completes (500ms+ after auth), React components
|
|
8
|
+
* don't re-render because there's no reactive state change.
|
|
9
|
+
*
|
|
10
|
+
* Solution: This module provides a subscribe/getSnapshot interface that
|
|
11
|
+
* React's useSyncExternalStore can use to trigger re-renders when
|
|
12
|
+
* initialization completes.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
type Listener = () => void;
|
|
16
|
+
|
|
17
|
+
interface InitState {
|
|
18
|
+
initialized: boolean;
|
|
19
|
+
userId: string | null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let state: InitState = { initialized: false, userId: null };
|
|
23
|
+
const listeners = new Set<Listener>();
|
|
24
|
+
|
|
25
|
+
const notifyListeners = (): void => {
|
|
26
|
+
listeners.forEach((listener) => listener());
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const initializationState = {
|
|
30
|
+
subscribe: (listener: Listener): (() => void) => {
|
|
31
|
+
listeners.add(listener);
|
|
32
|
+
return () => listeners.delete(listener);
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
getSnapshot: (): InitState => state,
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Called by SubscriptionManager after successful initialization.
|
|
39
|
+
* Triggers re-render in all subscribed React components.
|
|
40
|
+
*/
|
|
41
|
+
markInitialized: (userId: string | null): void => {
|
|
42
|
+
state = { initialized: true, userId };
|
|
43
|
+
notifyListeners();
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Called when initialization starts for a new user (e.g., user switch).
|
|
48
|
+
* Resets the state so queries know they need to wait.
|
|
49
|
+
*/
|
|
50
|
+
markPending: (): void => {
|
|
51
|
+
state = { initialized: false, userId: null };
|
|
52
|
+
notifyListeners();
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Check if initialized for a specific user.
|
|
57
|
+
*/
|
|
58
|
+
isInitializedForUser: (userId: string | null): boolean => {
|
|
59
|
+
return state.initialized && state.userId === userId;
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
reset: (): void => {
|
|
63
|
+
state = { initialized: false, userId: null };
|
|
64
|
+
notifyListeners();
|
|
65
|
+
},
|
|
66
|
+
};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { useQuery, useQueryClient } from "@umituz/react-native-design-system";
|
|
2
|
-
import { useEffect } from "react";
|
|
2
|
+
import { useEffect, useSyncExternalStore } from "react";
|
|
3
3
|
import { useAuthStore, selectUserId } from "@umituz/react-native-auth";
|
|
4
4
|
import { SubscriptionManager } from "../infrastructure/managers/SubscriptionManager";
|
|
5
|
+
import { initializationState } from "../infrastructure/state/initializationState";
|
|
5
6
|
import { subscriptionEventBus, SUBSCRIPTION_EVENTS } from "../../../shared/infrastructure/SubscriptionEventBus";
|
|
6
7
|
import { SubscriptionStatusResult } from "./useSubscriptionStatus.types";
|
|
7
8
|
import { isAuthenticated } from "../utils/authGuards";
|
|
@@ -19,8 +20,18 @@ export const useSubscriptionStatus = (): SubscriptionStatusResult => {
|
|
|
19
20
|
const queryClient = useQueryClient();
|
|
20
21
|
const isConfigured = SubscriptionManager.isConfigured();
|
|
21
22
|
|
|
22
|
-
//
|
|
23
|
-
const
|
|
23
|
+
// Reactive initialization state - triggers re-render when BackgroundInitializer completes
|
|
24
|
+
const initState = useSyncExternalStore(
|
|
25
|
+
initializationState.subscribe,
|
|
26
|
+
initializationState.getSnapshot,
|
|
27
|
+
initializationState.getSnapshot,
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
// Check if initialized for this specific user (reactive)
|
|
31
|
+
const isInitialized = userId
|
|
32
|
+
? initState.initialized && initState.userId === userId
|
|
33
|
+
: false;
|
|
34
|
+
|
|
24
35
|
const queryEnabled = isAuthenticated(userId) && isConfigured && isInitialized;
|
|
25
36
|
|
|
26
37
|
const { data, status, error, refetch } = useQuery({
|
|
@@ -84,7 +95,3 @@ export const useSubscriptionStatus = (): SubscriptionStatusResult => {
|
|
|
84
95
|
refetch,
|
|
85
96
|
};
|
|
86
97
|
};
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|