@umituz/react-native-subscription 2.2.1 → 2.2.3

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.2.1",
3
+ "version": "2.2.3",
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",
@@ -125,7 +125,12 @@ export const useInitializeCredits = ({
125
125
  },
126
126
  onSuccess: (result) => {
127
127
  if (userId && result.success && result.data) {
128
+ // Set the data immediately for optimistic UI
128
129
  queryClient.setQueryData(creditsQueryKeys.user(userId), result.data);
130
+ // Also invalidate to ensure all subscribers get the update
131
+ queryClient.invalidateQueries({
132
+ queryKey: creditsQueryKeys.user(userId),
133
+ });
129
134
  }
130
135
  },
131
136
  });
@@ -17,10 +17,17 @@ export interface CreditCheckResult {
17
17
  export interface CreditCheckerConfig {
18
18
  repository: CreditsRepository;
19
19
  getCreditType: (operationType: string) => CreditType;
20
+ /**
21
+ * Optional callback called after successful credit deduction.
22
+ * Use this to invalidate TanStack Query cache or trigger UI updates.
23
+ * @param userId - The user whose credits were deducted
24
+ * @param creditType - The type of credit that was deducted
25
+ */
26
+ onCreditDeducted?: (userId: string, creditType: CreditType) => void;
20
27
  }
21
28
 
22
29
  export const createCreditChecker = (config: CreditCheckerConfig) => {
23
- const { repository, getCreditType } = config;
30
+ const { repository, getCreditType, onCreditDeducted } = config;
24
31
 
25
32
  const checkCreditsAvailable = async (
26
33
  userId: string | undefined,
@@ -59,6 +66,8 @@ export const createCreditChecker = (config: CreditCheckerConfig) => {
59
66
  for (let attempt = 0; attempt < maxRetries; attempt++) {
60
67
  const result = await repository.deductCredit(userId, creditType);
61
68
  if (result.success) {
69
+ // Notify subscribers that credits were deducted
70
+ onCreditDeducted?.(userId, creditType);
62
71
  return;
63
72
  }
64
73
  lastError = new Error(result.error?.message || "Deduction failed");
@@ -77,3 +86,4 @@ export const createCreditChecker = (config: CreditCheckerConfig) => {
77
86
  };
78
87
 
79
88
  export type CreditChecker = ReturnType<typeof createCreditChecker>;
89
+