@umituz/react-native-subscription 2.27.30 → 2.27.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.27.30",
3
+ "version": "2.27.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",
@@ -64,9 +64,11 @@ class SubscriptionManagerImpl {
64
64
 
65
65
  const effectiveUserId = userId || (await this.userIdProvider.getOrCreateAnonymousUserId());
66
66
 
67
- if (!this.initCache.shouldReinitialize(effectiveUserId)) {
68
- const existingPromise = this.initCache.getExistingPromise();
69
- if (existingPromise) return existingPromise;
67
+ // Atomic check-and-acquire to prevent race conditions
68
+ const { shouldInit, existingPromise } = this.initCache.tryAcquireInitialization(effectiveUserId);
69
+
70
+ if (!shouldInit && existingPromise) {
71
+ return existingPromise;
70
72
  }
71
73
 
72
74
  const promise = this.performInitialization(effectiveUserId);
@@ -20,6 +20,8 @@ export interface InitializerDeps {
20
20
 
21
21
  let isPurchasesConfigured = false;
22
22
  let isLogHandlerConfigured = false;
23
+ // Mutex to prevent concurrent configuration
24
+ let configurationInProgress = false;
23
25
 
24
26
  function configureLogHandler(): void {
25
27
  if (isLogHandlerConfigured) return;
@@ -97,12 +99,26 @@ export async function initializeSDK(
97
99
  }
98
100
 
99
101
  // Case 3: First time configuration
102
+ // Check mutex to prevent double configuration
103
+ if (configurationInProgress) {
104
+ // Wait a bit and retry - another thread is configuring
105
+ await new Promise(resolve => setTimeout(resolve, 100));
106
+ // After waiting, isPurchasesConfigured should be true
107
+ if (isPurchasesConfigured) {
108
+ return initializeSDK(deps, userId, apiKey);
109
+ }
110
+ return { success: false, offering: null, hasPremium: false };
111
+ }
112
+
100
113
  const key = apiKey || resolveApiKey(deps.config);
101
114
 
102
115
  if (!key) {
103
116
  return { success: false, offering: null, hasPremium: false };
104
117
  }
105
118
 
119
+ // Acquire mutex
120
+ configurationInProgress = true;
121
+
106
122
  try {
107
123
  configureLogHandler();
108
124
 
@@ -123,5 +139,8 @@ export async function initializeSDK(
123
139
  } catch (error) {
124
140
  getErrorMessage(error, "RevenueCat init failed");
125
141
  return { success: false, offering: null, hasPremium: false };
142
+ } finally {
143
+ // Release mutex
144
+ configurationInProgress = false;
126
145
  }
127
146
  }
@@ -1,12 +1,43 @@
1
1
  /**
2
2
  * Initialization Cache
3
3
  * Manages promise caching and user state for initialization
4
+ * Thread-safe: Uses mutex pattern to prevent race conditions
4
5
  */
5
6
 
6
7
  export class InitializationCache {
7
8
  private initPromise: Promise<boolean> | null = null;
8
9
  private currentUserId: string | null = null;
10
+ // Mutex to prevent race condition during initialization
11
+ private initializationInProgress = false;
12
+ // Track which userId the promise is for (separate from currentUserId which is set after completion)
13
+ private promiseUserId: string | null = null;
9
14
 
15
+ /**
16
+ * Atomically check if reinitialization is needed AND reserve the slot
17
+ * Returns: { shouldInit: boolean, existingPromise: Promise | null }
18
+ */
19
+ tryAcquireInitialization(userId: string): { shouldInit: boolean; existingPromise: Promise<boolean> | null } {
20
+ // If initialization is in progress for the same user, return existing promise
21
+ if (this.initializationInProgress && this.promiseUserId === userId && this.initPromise) {
22
+ return { shouldInit: false, existingPromise: this.initPromise };
23
+ }
24
+
25
+ // If already initialized for this user and promise resolved successfully, return it
26
+ if (this.initPromise && this.currentUserId === userId && !this.initializationInProgress) {
27
+ return { shouldInit: false, existingPromise: this.initPromise };
28
+ }
29
+
30
+ // Different user or no initialization - need to reinitialize
31
+ // Atomically set the flag
32
+ this.initializationInProgress = true;
33
+ this.promiseUserId = userId;
34
+
35
+ return { shouldInit: true, existingPromise: null };
36
+ }
37
+
38
+ /**
39
+ * @deprecated Use tryAcquireInitialization instead for atomic operations
40
+ */
10
41
  shouldReinitialize(userId: string): boolean {
11
42
  if (!this.initPromise) {
12
43
  return true;
@@ -19,13 +50,38 @@ export class InitializationCache {
19
50
  return false;
20
51
  }
21
52
 
53
+ /**
54
+ * @deprecated Use tryAcquireInitialization instead for atomic operations
55
+ */
22
56
  getExistingPromise(): Promise<boolean> | null {
23
57
  return this.initPromise;
24
58
  }
25
59
 
26
60
  setPromise(promise: Promise<boolean>, userId: string): void {
27
61
  this.initPromise = promise;
28
- this.currentUserId = userId;
62
+ this.promiseUserId = userId;
63
+
64
+ // Chain to mark completion and set currentUserId only on success
65
+ promise
66
+ .then((result) => {
67
+ if (result && this.promiseUserId === userId) {
68
+ this.currentUserId = userId;
69
+ }
70
+ return result;
71
+ })
72
+ .catch(() => {
73
+ // On failure, clear the promise so retry is possible
74
+ if (this.promiseUserId === userId) {
75
+ this.initPromise = null;
76
+ this.promiseUserId = null;
77
+ }
78
+ })
79
+ .finally(() => {
80
+ // Always release the mutex
81
+ if (this.promiseUserId === userId) {
82
+ this.initializationInProgress = false;
83
+ }
84
+ });
29
85
  }
30
86
 
31
87
  getCurrentUserId(): string | null {
@@ -35,5 +91,7 @@ export class InitializationCache {
35
91
  reset(): void {
36
92
  this.initPromise = null;
37
93
  this.currentUserId = null;
94
+ this.initializationInProgress = false;
95
+ this.promiseUserId = null;
38
96
  }
39
97
  }