@umituz/react-native-subscription 2.37.15 → 2.37.17
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/subscription/application/initializer/BackgroundInitializer.ts +1 -0
- package/src/domains/subscription/infrastructure/hooks/useSubscriptionPackages.ts +18 -8
- package/src/domains/subscription/infrastructure/managers/SubscriptionManager.ts +11 -0
- package/src/domains/subscription/infrastructure/state/initializationState.ts +66 -0
- package/src/domains/subscription/infrastructure/utils/renewal/RenewalDetector.ts +4 -1
- 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.17",
|
|
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",
|
|
@@ -91,6 +91,7 @@ export async function startBackgroundInitialization(config: SubscriptionInitConf
|
|
|
91
91
|
// This prevents a Firestore permission-denied error from querying with an anonymous ID.
|
|
92
92
|
if (initialRevenueCatUserId) {
|
|
93
93
|
await initializeInBackground(initialRevenueCatUserId);
|
|
94
|
+
lastInitSucceeded = true;
|
|
94
95
|
} else if (typeof __DEV__ !== 'undefined' && __DEV__) {
|
|
95
96
|
console.log('[BackgroundInitializer] Skipping anonymous init, waiting for auth state');
|
|
96
97
|
}
|
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Subscription Packages Hook
|
|
3
|
-
* TanStack query for fetching available packages
|
|
3
|
+
* TanStack query for fetching available packages (offerings)
|
|
4
4
|
* Auth info automatically read from @umituz/react-native-auth
|
|
5
|
+
*
|
|
6
|
+
* IMPORTANT: Packages (offerings) are NOT user-specific - they're the same
|
|
7
|
+
* for all users. We only need RevenueCat to be initialized, not necessarily
|
|
8
|
+
* for a specific user. User-specific checks belong in useSubscriptionStatus.
|
|
5
9
|
*/
|
|
6
10
|
|
|
7
11
|
import { useQuery, useQueryClient } from "@umituz/react-native-design-system";
|
|
8
|
-
import { useEffect, useRef } from "react";
|
|
12
|
+
import { useEffect, useRef, useSyncExternalStore } from "react";
|
|
9
13
|
import {
|
|
10
14
|
useAuthStore,
|
|
11
15
|
selectUserId,
|
|
12
16
|
} from "@umituz/react-native-auth";
|
|
13
17
|
import { SubscriptionManager } from '../../infrastructure/managers/SubscriptionManager';
|
|
18
|
+
import { initializationState } from "../../infrastructure/state/initializationState";
|
|
14
19
|
import {
|
|
15
20
|
SUBSCRIPTION_QUERY_KEYS,
|
|
16
21
|
} from "./subscriptionQueryKeys";
|
|
@@ -26,16 +31,21 @@ export const useSubscriptionPackages = () => {
|
|
|
26
31
|
const queryClient = useQueryClient();
|
|
27
32
|
const prevUserIdRef = useRef(userId);
|
|
28
33
|
|
|
29
|
-
//
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
34
|
+
// Reactive initialization state - triggers re-render when BackgroundInitializer completes
|
|
35
|
+
const initState = useSyncExternalStore(
|
|
36
|
+
initializationState.subscribe,
|
|
37
|
+
initializationState.getSnapshot,
|
|
38
|
+
initializationState.getSnapshot,
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
// Packages (offerings) are NOT user-specific - same for all users.
|
|
42
|
+
// We only need RevenueCat to be initialized at all.
|
|
43
|
+
// Use reactive state OR direct manager check for backwards compatibility.
|
|
44
|
+
const isInitialized = initState.initialized || SubscriptionManager.isInitialized();
|
|
33
45
|
|
|
34
46
|
const query = useQuery({
|
|
35
47
|
queryKey: [...SUBSCRIPTION_QUERY_KEYS.packages, userId ?? "anonymous"] as const,
|
|
36
48
|
queryFn: async () => {
|
|
37
|
-
// No side effects - just fetch packages
|
|
38
|
-
// Initialization is handled by BackgroundInitializer
|
|
39
49
|
return SubscriptionManager.getPackages();
|
|
40
50
|
},
|
|
41
51
|
enabled: isConfigured && isInitialized,
|
|
@@ -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;
|
|
@@ -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
|
+
};
|
|
@@ -45,7 +45,10 @@ export function detectRenewal(
|
|
|
45
45
|
const newExpiration = new Date(newExpirationDate).getTime();
|
|
46
46
|
const previousExpiration = new Date(state.previousExpirationDate).getTime();
|
|
47
47
|
const productChanged = productId !== state.previousProductId;
|
|
48
|
-
|
|
48
|
+
|
|
49
|
+
// Guard against NaN from invalid date strings - treat as no extension
|
|
50
|
+
const expirationExtended =
|
|
51
|
+
!isNaN(newExpiration) && !isNaN(previousExpiration) && newExpiration > previousExpiration;
|
|
49
52
|
|
|
50
53
|
if (productChanged) {
|
|
51
54
|
const oldTier = getPackageTier(state.previousProductId);
|
|
@@ -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
|
-
|