Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(clerk-js): Introduce Clerk.client.clearCache() #1545

Merged
merged 2 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/swift-rivers-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@clerk/clerk-js': minor
'@clerk/types': patch
---

Introduce Clerk.client.clearCache() method
4 changes: 4 additions & 0 deletions packages/clerk-js/src/core/resources/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ export class Client extends BaseResource implements ClientResource {
});
}

clearCache(): void {
return this.sessions.forEach(s => s.clearCache());
}

fromJSON(data: ClientJSON | null): this {
if (data) {
this.id = data.id;
Expand Down
4 changes: 4 additions & 0 deletions packages/clerk-js/src/core/resources/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export class Session extends BaseResource implements SessionResource {
});
};

clearCache = (): void => {
return SessionTokenCache.clear();
};

getToken: GetToken = async (options?: GetTokenOptions): Promise<string | null> => {
return runWithExponentialBackOff(() => this._getToken(options), {
shouldRetry: (error: unknown, currentIteration: number) => !is4xxError(error) && currentIteration < 4,
Expand Down
11 changes: 10 additions & 1 deletion packages/clerk-js/src/core/tokenCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ export class TokenCacheKey {
const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {
const cache = new Map<string, TokenCacheValue>();

let timer: ReturnType<typeof setTimeout>;

const size = () => {
return cache.size;
};

const clear = () => {
clearTimeout(timer);
cache.clear();
};

Expand Down Expand Up @@ -83,7 +86,13 @@ const MemoryTokenCache = (prefix = KEY_PREFIX): TokenCache => {

// Mutate cached value and set expirations
value.expiresIn = expiresIn;
setTimeout(deleteKey, expiresIn * 1000);
timer = setTimeout(deleteKey, expiresIn * 1000);

// Teach ClerkJS not to block the exit of the event loop when used in Node environments.
// More info at https://nodejs.org/api/timers.html#timeoutunref
if (typeof timer.unref === 'function') {
timer.unref();
}
})
.catch(() => {
deleteKey();
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface ClientResource extends ClerkResource {
isNew: () => boolean;
create: () => Promise<ClientResource>;
destroy: () => Promise<void>;
clearCache: () => void;
lastActiveSessionId: string | null;
createdAt: Date | null;
updatedAt: Date | null;
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface SessionResource extends ClerkResource {
remove: () => Promise<SessionResource>;
touch: () => Promise<SessionResource>;
getToken: GetToken;
clearCache: () => void;
createdAt: Date;
updatedAt: Date;
}
Expand Down