@umituz/react-native-subscription 2.37.10 → 2.37.12

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.10",
3
+ "version": "2.37.12",
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",
@@ -77,6 +77,27 @@ export function buildCreditsData({
77
77
  };
78
78
  }
79
79
 
80
+ /**
81
+ * Compare two Firestore Timestamp-like values by their underlying time.
82
+ * Handles Timestamp objects (with toMillis/seconds+nanoseconds), null, and undefined.
83
+ */
84
+ function timestampsEqual(a: unknown, b: unknown): boolean {
85
+ if (a === b) return true;
86
+ if (a == null || b == null) return a == b;
87
+ if (typeof a === "object" && typeof b === "object" && a !== null && b !== null) {
88
+ if ("toMillis" in a && "toMillis" in b &&
89
+ typeof (a as { toMillis: unknown }).toMillis === "function" &&
90
+ typeof (b as { toMillis: unknown }).toMillis === "function") {
91
+ return (a as { toMillis: () => number }).toMillis() === (b as { toMillis: () => number }).toMillis();
92
+ }
93
+ if ("seconds" in a && "seconds" in b && "nanoseconds" in a && "nanoseconds" in b) {
94
+ return (a as { seconds: number; nanoseconds: number }).seconds === (b as { seconds: number; nanoseconds: number }).seconds &&
95
+ (a as { seconds: number; nanoseconds: number }).nanoseconds === (b as { seconds: number; nanoseconds: number }).nanoseconds;
96
+ }
97
+ }
98
+ return false;
99
+ }
100
+
80
101
  export function shouldSkipStatusSyncWrite(
81
102
  purchaseId: string,
82
103
  existingData: any,
@@ -92,7 +113,7 @@ export function shouldSkipStatusSyncWrite(
92
113
  existingData.creditLimit === newCreditsData.creditLimit &&
93
114
  existingData.productId === newCreditsData.productId &&
94
115
  existingData.willRenew === newCreditsData.willRenew &&
95
- existingData.expirationDate === newCreditsData.expirationDate &&
96
- existingData.canceledAt === newCreditsData.canceledAt &&
97
- existingData.billingIssueDetectedAt === newCreditsData.billingIssueDetectedAt;
116
+ timestampsEqual(existingData.expirationDate, newCreditsData.expirationDate) &&
117
+ timestampsEqual(existingData.canceledAt, newCreditsData.canceledAt) &&
118
+ timestampsEqual(existingData.billingIssueDetectedAt, newCreditsData.billingIssueDetectedAt);
98
119
  }
@@ -1,4 +1,4 @@
1
- import { useCallback } from "react";
1
+ import { useCallback, useRef } from "react";
2
2
  import { useMutation, useQueryClient } from "@umituz/react-native-design-system";
3
3
  import { getCreditsRepository } from "../../infrastructure/CreditsRepositoryManager";
4
4
  import type { UseDeductCreditParams, UseDeductCreditResult } from "./types";
@@ -17,9 +17,13 @@ export const useDeductCredit = ({
17
17
  createDeductCreditMutationConfig(userId, repository, queryClient)
18
18
  );
19
19
 
20
+ // Use ref for stable reference to mutateAsync — avoids re-creating callbacks every render
21
+ const mutateAsyncRef = useRef(mutation.mutateAsync);
22
+ mutateAsyncRef.current = mutation.mutateAsync;
23
+
20
24
  const deductCredit = useCallback(async (cost: number = 1): Promise<boolean> => {
21
25
  try {
22
- const res = await mutation.mutateAsync(cost);
26
+ const res = await mutateAsyncRef.current(cost);
23
27
  if (!res.success) {
24
28
  if (res.error?.code === "CREDITS_EXHAUSTED") {
25
29
  onCreditsExhausted?.();
@@ -35,7 +39,7 @@ export const useDeductCredit = ({
35
39
  });
36
40
  throw error;
37
41
  }
38
- }, [mutation, onCreditsExhausted, userId]);
42
+ }, [onCreditsExhausted, userId]);
39
43
 
40
44
  const deductCredits = useCallback(async (cost: number): Promise<boolean> => {
41
45
  return await deductCredit(cost);
@@ -40,15 +40,19 @@ export class ConfigurationStateManager {
40
40
  completeConfiguration(success: boolean): void {
41
41
  this._isPurchasesConfigured = success;
42
42
 
43
- // Cleanup promise state immediately (no setTimeout)
44
- // If promise hasn't resolved yet, that's fine - it will still resolve via the callback
45
- if (this._configurationPromise) {
46
- this._configurationPromise = null;
47
- }
43
+ // Note: Do NOT null _configurationPromise here.
44
+ // The promise is resolved externally via resolveConfig() in RevenueCatInitializer.
45
+ // Nulling it before resolution creates a race where concurrent callers
46
+ // see isConfiguring=false and start a duplicate SDK configuration.
47
+ // The promise is cleaned up in the next startConfiguration() or reset() call.
48
+ // We only clear the resolve function since it's no longer needed.
49
+ this._resolveConfiguration = null;
50
+ }
48
51
 
49
- // Clear resolve function if it still exists
50
- if (this._resolveConfiguration) {
51
- this._resolveConfiguration = null;
52
+ /** Called after resolveConfig() to clean up the completed promise */
53
+ clearCompletedConfiguration(): void {
54
+ if (this._isPurchasesConfigured || !this._resolveConfiguration) {
55
+ this._configurationPromise = null;
52
56
  }
53
57
  }
54
58
 
@@ -72,5 +72,7 @@ export async function initializeSDK(
72
72
  const result = await handleInitialConfiguration(deps, userId, key);
73
73
  configState.completeConfiguration(result.success);
74
74
  resolveConfig(result);
75
+ // Clean up the resolved promise so future callers don't await a stale promise
76
+ configState.clearCompletedConfiguration();
75
77
  return result;
76
78
  }
@@ -37,7 +37,9 @@ export async function startBackgroundInitialization(config: SubscriptionInitConf
37
37
  await initializeInBackground(revenueCatUserId);
38
38
  lastUserId = revenueCatUserId;
39
39
  } catch (error) {
40
- // Background re-initialization errors are non-critical, already logged by SubscriptionManager
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;
41
43
  console.error('[BackgroundInitializer] Reinitialization failed:', {
42
44
  userId: revenueCatUserId,
43
45
  error: error instanceof Error ? error.message : String(error)