Skip to content

Commit 7fc1248

Browse files
committed
wip
1 parent c07fe17 commit 7fc1248

File tree

4 files changed

+58
-5
lines changed

4 files changed

+58
-5
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import type { QueryClient } from './query-core';
2+
3+
type TaggedQueryClient = { __tag: 'clerk-rq-client'; client: QueryClient };
4+
5+
class LazyQueryController {
6+
#queryClient: QueryClient | undefined;
7+
#requested = false;
8+
static #instance: LazyQueryController | undefined;
9+
10+
static get(): LazyQueryController {
11+
if (!this.#instance) {
12+
this.#instance = new LazyQueryController();
13+
}
14+
return this.#instance;
15+
}
16+
17+
constructor() {}
18+
19+
get client(): TaggedQueryClient | undefined {
20+
if (!this.#requested) {
21+
void import('./query-core')
22+
.then(module => module.QueryClient)
23+
.then(QueryClient => {
24+
if (this.#queryClient) {
25+
return;
26+
}
27+
this.#queryClient = new QueryClient();
28+
// @ts-expect-error - queryClientStatus is not typed
29+
this.#publicEventBus.emit('queryClientStatus', 'ready');
30+
});
31+
}
32+
33+
return this.#queryClient
34+
? {
35+
__tag: 'clerk-rq-client',
36+
client: this.#queryClient,
37+
}
38+
: undefined;
39+
}
40+
41+
invalidate(...params: Parameters<QueryClient['invalidateQueries']>): void {
42+
// Only invalidate if the query client exists
43+
this.#queryClient?.invalidateQueries(...params);
44+
}
45+
}
46+
47+
export const lazyQueryController = LazyQueryController.get();

packages/clerk-js/src/core/resources/BillingCheckout.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
import { unixEpochToDate } from '@/utils/date';
1313

1414
import { billingTotalsFromJSON } from '../../utils';
15+
import { lazyQueryController } from '../lazy-query-client';
1516
import { BillingPayer } from './BillingPayer';
1617
import { BaseResource, BillingPaymentSource, BillingPlan } from './internal';
1718

@@ -54,11 +55,11 @@ export class BillingCheckout extends BaseResource implements BillingCheckoutReso
5455
return this;
5556
}
5657

57-
confirm = (params: ConfirmCheckoutParams): Promise<this> => {
58+
confirm = async (params: ConfirmCheckoutParams): Promise<this> => {
5859
// Retry confirmation in case of a 500 error
5960
// This will retry up to 3 times with an increasing delay
6061
// It retries at 2s, 4s, 6s and 8s
61-
return retry(
62+
const result = await retry(
6263
() =>
6364
this._basePatch({
6465
path: this.payer.organizationId
@@ -85,5 +86,9 @@ export class BillingCheckout extends BaseResource implements BillingCheckoutReso
8586
},
8687
},
8788
);
89+
90+
// TODO: We can use the public bus instead.
91+
lazyQueryController.invalidate({ queryKey: ['commerce-subscription'] });
92+
return result;
8893
};
8994
}

packages/clerk-js/src/core/resources/BillingSubscription.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
import { unixEpochToDate } from '@/utils/date';
1414

1515
import { billingMoneyAmountFromJSON } from '../../utils';
16+
import { lazyQueryController } from '../lazy-query-client';
1617
import { BaseResource, BillingPlan, DeletedObject } from './internal';
1718

1819
export class BillingSubscription extends BaseResource implements BillingSubscriptionResource {
@@ -117,6 +118,7 @@ export class BillingSubscriptionItem extends BaseResource implements BillingSubs
117118
})
118119
)?.response as unknown as DeletedObjectJSON;
119120

121+
lazyQueryController.invalidate({ queryKey: ['commerce-subscription'] });
120122
return new DeletedObject(json);
121123
}
122124
}

packages/clerk-js/src/ui/contexts/components/Plans.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export const usePlansContext = () => {
115115
return false;
116116
}, [clerk, subscriberType]);
117117

118-
const { subscriptionItems, revalidate: revalidateSubscriptions, data: topLevelSubscription } = useSubscription();
118+
const { subscriptionItems, data: topLevelSubscription } = useSubscription();
119119

120120
// Invalidates cache but does not fetch immediately
121121
const { data: plans, revalidate: revalidatePlans } = usePlans({ mode: 'cache' });
@@ -127,11 +127,10 @@ export const usePlansContext = () => {
127127

128128
const revalidateAll = useCallback(() => {
129129
// Revalidate the plans and subscriptions
130-
void revalidateSubscriptions();
131130
void revalidatePlans();
132131
void revalidateStatements();
133132
void revalidatePaymentSources();
134-
}, [revalidateSubscriptions, revalidatePlans, revalidateStatements, revalidatePaymentSources]);
133+
}, [revalidatePlans, revalidateStatements, revalidatePaymentSources]);
135134

136135
// should the default plan be shown as active
137136
const isDefaultPlanImplicitlyActiveOrUpcoming = useMemo(() => {

0 commit comments

Comments
 (0)