@umituz/react-native-subscription 2.37.15 → 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/subscription/application/initializer/BackgroundInitializer.ts +1 -0
- 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/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",
|
|
@@ -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
|
}
|
|
@@ -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;
|
|
@@ -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
|
-
|