Skip to content

Commit

Permalink
feat(3742): replace metaMetricId to a promise getMetaMetricId
Browse files Browse the repository at this point in the history
  • Loading branch information
DDDDDanica committed Dec 12, 2024
1 parent 63a7274 commit 1f94695
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function createController(
state: Partial<RemoteFeatureFlagControllerState>;
clientConfigApiService: AbstractClientConfigApiService;
disabled: boolean;
metaMetricsId: string;
getMetaMetricsId: Promise<string | undefined>;
}> = {},
) {
return new RemoteFeatureFlagController({
Expand All @@ -68,7 +68,7 @@ function createController(
clientConfigApiService:
options.clientConfigApiService ?? buildClientConfigApiService(),
disabled: options.disabled,
metaMetricsId: options.metaMetricsId,
getMetaMetricsId: options.getMetaMetricsId,
});
}

Expand Down Expand Up @@ -271,7 +271,7 @@ describe('RemoteFeatureFlagController', () => {
});
const controller = createController({
clientConfigApiService,
metaMetricsId: MOCK_METRICS_ID,
getMetaMetricsId: Promise.resolve(MOCK_METRICS_ID),
});
await controller.updateRemoteFeatureFlags();

Expand All @@ -289,7 +289,7 @@ describe('RemoteFeatureFlagController', () => {
});
const controller = createController({
clientConfigApiService,
metaMetricsId: MOCK_METRICS_ID,
getMetaMetricsId: Promise.resolve(MOCK_METRICS_ID),
});
await controller.updateRemoteFeatureFlags();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class RemoteFeatureFlagController extends BaseController<

#inProgressFlagUpdate?: Promise<ServiceResponse>;

#metaMetricsId?: string | undefined;
#getMetaMetricsId?: Promise<string | undefined>;

/**
* Constructs a new RemoteFeatureFlagController instance.
Expand All @@ -114,20 +114,20 @@ export class RemoteFeatureFlagController extends BaseController<
* @param options.clientConfigApiService - The service instance to fetch remote feature flags.
* @param options.fetchInterval - The interval in milliseconds before cached flags expire. Defaults to 1 day.
* @param options.disabled - Determines if the controller should be disabled initially. Defaults to false.
* @param options.metaMetricsId - Determines the threshold value for the feature flag to return
* @param options.getMetaMetricsId - Promise that resolves to a metaMetricsId.
*/
constructor({
messenger,
state,
clientConfigApiService,
fetchInterval = DEFAULT_CACHE_DURATION,
disabled = false,
metaMetricsId,
getMetaMetricsId,
}: {
messenger: RemoteFeatureFlagControllerMessenger;
state?: Partial<RemoteFeatureFlagControllerState>;
clientConfigApiService: AbstractClientConfigApiService;
metaMetricsId?: string | undefined;
getMetaMetricsId?: Promise<string | undefined>;
fetchInterval?: number;
disabled?: boolean;
}) {
Expand All @@ -144,7 +144,7 @@ export class RemoteFeatureFlagController extends BaseController<
this.#fetchInterval = fetchInterval;
this.#disabled = disabled;
this.#clientConfigApiService = clientConfigApiService;
this.#metaMetricsId = metaMetricsId;
this.#getMetaMetricsId = getMetaMetricsId;
}

/**
Expand Down Expand Up @@ -184,7 +184,7 @@ export class RemoteFeatureFlagController extends BaseController<
this.#inProgressFlagUpdate = undefined;
}

this.#updateCache(serverData.remoteFeatureFlags);
await this.#updateCache(serverData.remoteFeatureFlags);
}

/**
Expand All @@ -193,9 +193,10 @@ export class RemoteFeatureFlagController extends BaseController<
* @param remoteFeatureFlags - The new feature flags to cache.
* @private
*/
#updateCache(remoteFeatureFlags: FeatureFlags) {
const processedRemoteFeatureFlags =
this.#processRemoteFeatureFlags(remoteFeatureFlags);
async #updateCache(remoteFeatureFlags: FeatureFlags) {
const processedRemoteFeatureFlags = await this.#processRemoteFeatureFlags(
remoteFeatureFlags,
);
this.update(() => {
return {
remoteFeatureFlags: processedRemoteFeatureFlags,
Expand All @@ -204,17 +205,20 @@ export class RemoteFeatureFlagController extends BaseController<
});
}

#processRemoteFeatureFlags(remoteFeatureFlags: FeatureFlags): FeatureFlags {
async #processRemoteFeatureFlags(
remoteFeatureFlags: FeatureFlags,
): Promise<FeatureFlags> {
const processedRemoteFeatureFlags: FeatureFlags = {};
const thresholdValue = generateDeterministicRandomNumber(
this.#metaMetricsId || generateFallbackMetaMetricsId(),
);
const metaMetricsId =
(await this.#getMetaMetricsId) || generateFallbackMetaMetricsId();
const thresholdValue = generateDeterministicRandomNumber(metaMetricsId);

for (const [
remoteFeatureFlagName,
remoteFeatureFlagValue,
] of Object.entries(remoteFeatureFlags)) {
let processedValue = remoteFeatureFlagValue;

if (Array.isArray(remoteFeatureFlagValue) && thresholdValue) {
const selectedGroup = remoteFeatureFlagValue.find(
(featureFlag): featureFlag is FeatureFlagScopeValue => {
Expand All @@ -235,7 +239,6 @@ export class RemoteFeatureFlagController extends BaseController<

processedRemoteFeatureFlags[remoteFeatureFlagName] = processedValue;
}

return processedRemoteFeatureFlags;
}

Expand Down

0 comments on commit 1f94695

Please sign in to comment.