Skip to content

Commit

Permalink
feat(analytics-remote-config): support custom session id (#929)
Browse files Browse the repository at this point in the history
  • Loading branch information
junjie-amplitude authored Dec 6, 2024
2 parents 3846eae + e5937be commit 5faf163
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
10 changes: 5 additions & 5 deletions packages/analytics-remote-config/src/remote-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class RemoteConfigFetch<RemoteConfig extends { [key: string]: object }>
localConfig: Config;
retryTimeout = 1000;
attempts = 0;
lastFetchedSessionId: number | undefined;
lastFetchedSessionId: number | string | undefined;
sessionTargetingMatch = false;
configKeys: string[];
metrics: RemoteConfigMetric = {};
Expand All @@ -36,7 +36,7 @@ export class RemoteConfigFetch<RemoteConfig extends { [key: string]: object }>
getRemoteConfig = async <K extends keyof RemoteConfig>(
configNamespace: string,
key: K,
sessionId?: number,
sessionId?: number | string,
): Promise<RemoteConfig[K] | undefined> => {
const fetchStartTime = Date.now();
// Finally fetch via API
Expand Down Expand Up @@ -64,7 +64,7 @@ export class RemoteConfigFetch<RemoteConfig extends { [key: string]: object }>
return REMOTE_CONFIG_SERVER_URL;
}

fetchWithTimeout = async (sessionId?: number): Promise<RemoteConfigAPIResponse<RemoteConfig> | void> => {
fetchWithTimeout = async (sessionId?: number | string): Promise<RemoteConfigAPIResponse<RemoteConfig> | void> => {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000);
const remoteConfig = await this.fetchRemoteConfig(controller.signal, sessionId);
Expand All @@ -74,7 +74,7 @@ export class RemoteConfigFetch<RemoteConfig extends { [key: string]: object }>

fetchRemoteConfig = async (
signal: AbortController['signal'],
sessionId?: number,
sessionId?: number | string,
): Promise<RemoteConfigAPIResponse<RemoteConfig> | void> => {
if (sessionId === this.lastFetchedSessionId && this.attempts >= this.localConfig.flushMaxRetries) {
return this.completeRequest({ err: MAX_RETRIES_EXCEEDED_MESSAGE });
Expand Down Expand Up @@ -128,7 +128,7 @@ export class RemoteConfigFetch<RemoteConfig extends { [key: string]: object }>

retryFetch = async (
signal: AbortController['signal'],
sessionId?: number,
sessionId?: number | string,
): Promise<RemoteConfigAPIResponse<RemoteConfig> | void> => {
await new Promise((resolve) => setTimeout(resolve, this.attempts * this.retryTimeout));
return this.fetchRemoteConfig(signal, sessionId);
Expand Down
7 changes: 5 additions & 2 deletions packages/analytics-remote-config/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface BaseRemoteConfigFetch<T> {
getRemoteConfig: <K extends keyof T>(
configNamespace: string,
key: K,
sessionId?: number,
sessionId?: number | string,
) => Promise<T[K] | undefined>;
}

Expand All @@ -31,7 +31,10 @@ export interface RemoteConfigMetric {

export interface RemoteConfigIDBStore<RemoteConfig extends { [key: string]: object }>
extends BaseRemoteConfigFetch<RemoteConfig> {
storeRemoteConfig: (remoteConfig: RemoteConfigAPIResponse<RemoteConfig>, sessionId?: number) => Promise<void>;
storeRemoteConfig: (
remoteConfig: RemoteConfigAPIResponse<RemoteConfig>,
sessionId?: number | string,
) => Promise<void>;
getLastFetchedSessionId: () => Promise<number | void>;
remoteConfigHasValues: (configNamespace: string) => Promise<boolean>;
}
Expand Down

0 comments on commit 5faf163

Please sign in to comment.