Skip to content

Commit 0f16606

Browse files
DDDDDanicaPatrykLucka
authored andcommitted
fix(3742): change getMetaMetricsId to only sync func type (#5108)
## Explanation Changes introduced in this PR including: - remove fallback of metaMetricsId and rely on client side always returning a value - refactor getMetaMetricsId to only handle synchronous (extension) function <!-- Thanks for your contribution! Take a moment to answer these questions so that reviewers have the information they need to properly understand your changes: * What is the current state of things and why does it need to change? * What is the solution your changes offer and how does it work? * Are there any changes whose purpose might not obvious to those unfamiliar with the domain? * If your primary goal was to update one package but you found you had to update another one along the way, why did you do so? * If you had to upgrade a dependency, why did you do so? --> ## References Addressed feedback #5051 (comment) <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> <!-- If you're making any consumer-facing changes, list those changes here as if you were updating a changelog, using the template below as a guide. (CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or FIXED. For security-related issues, follow the Security Advisory process.) Please take care to name the exact pieces of the API you've added or changed (e.g. types, interfaces, functions, or methods). If there are any breaking changes, make sure to offer a solution for consumers to follow once they upgrade to the changes. Finally, if you're only making changes to development scripts or tests, you may replace the template below with "None". --> ### `@metamask/remote-feature-flag-controller` - **CHANGED**: Modify signature of `getMetaMetricsId` to handle only synchronous function - **CHANGED**: Remove fallback of `metaMetricsId` ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've highlighted breaking changes using the "BREAKING" category above as appropriate - [x] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes
1 parent a6e5334 commit 0f16606

File tree

4 files changed

+8
-52
lines changed

4 files changed

+8
-52
lines changed

packages/remote-feature-flag-controller/src/remote-feature-flag-controller.test.ts

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import type {
1414
RemoteFeatureFlagControllerStateChangeEvent,
1515
} from './remote-feature-flag-controller';
1616
import type { FeatureFlags } from './remote-feature-flag-controller-types';
17-
import * as userSegmentationUtils from './utils/user-segmentation-utils';
1817

1918
const MOCK_FLAGS: FeatureFlags = {
2019
feature1: true,
@@ -42,7 +41,6 @@ const MOCK_FLAGS_WITH_THRESHOLD = {
4241
};
4342

4443
const MOCK_METRICS_ID = 'f9e8d7c6-b5a4-4210-9876-543210fedcba';
45-
const MOCK_METRICS_ID_2 = '987fcdeb-51a2-4c4b-9876-543210fedcba';
4644

4745
/**
4846
* Creates a controller instance with default parameters for testing
@@ -59,7 +57,7 @@ function createController(
5957
state: Partial<RemoteFeatureFlagControllerState>;
6058
clientConfigApiService: AbstractClientConfigApiService;
6159
disabled: boolean;
62-
getMetaMetricsId: Promise<string | undefined>;
60+
getMetaMetricsId: () => string;
6361
}> = {},
6462
) {
6563
return new RemoteFeatureFlagController({
@@ -68,7 +66,7 @@ function createController(
6866
clientConfigApiService:
6967
options.clientConfigApiService ?? buildClientConfigApiService(),
7068
disabled: options.disabled,
71-
getMetaMetricsId: options.getMetaMetricsId,
69+
getMetaMetricsId: options.getMetaMetricsId ?? (() => MOCK_METRICS_ID),
7270
});
7371
}
7472

@@ -271,7 +269,7 @@ describe('RemoteFeatureFlagController', () => {
271269
});
272270
const controller = createController({
273271
clientConfigApiService,
274-
getMetaMetricsId: Promise.resolve(MOCK_METRICS_ID),
272+
getMetaMetricsId: () => MOCK_METRICS_ID,
275273
});
276274
await controller.updateRemoteFeatureFlags();
277275

@@ -289,34 +287,14 @@ describe('RemoteFeatureFlagController', () => {
289287
});
290288
const controller = createController({
291289
clientConfigApiService,
292-
getMetaMetricsId: Promise.resolve(MOCK_METRICS_ID),
290+
getMetaMetricsId: () => MOCK_METRICS_ID,
293291
});
294292
await controller.updateRemoteFeatureFlags();
295293

296294
const { testFlagForThreshold, ...nonThresholdFlags } =
297295
controller.state.remoteFeatureFlags;
298296
expect(nonThresholdFlags).toStrictEqual(MOCK_FLAGS);
299297
});
300-
301-
it('uses a fallback metaMetricsId if none is provided', async () => {
302-
jest
303-
.spyOn(userSegmentationUtils, 'generateFallbackMetaMetricsId')
304-
.mockReturnValue(MOCK_METRICS_ID_2);
305-
const clientConfigApiService = buildClientConfigApiService({
306-
remoteFeatureFlags: MOCK_FLAGS_WITH_THRESHOLD,
307-
});
308-
const controller = createController({
309-
clientConfigApiService,
310-
});
311-
await controller.updateRemoteFeatureFlags();
312-
313-
expect(
314-
controller.state.remoteFeatureFlags.testFlagForThreshold,
315-
).toStrictEqual({
316-
name: 'groupA',
317-
value: 'valueA',
318-
});
319-
});
320298
});
321299

322300
describe('enable and disable', () => {

packages/remote-feature-flag-controller/src/remote-feature-flag-controller.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import type {
1414
import {
1515
generateDeterministicRandomNumber,
1616
isFeatureFlagWithScopeValue,
17-
generateFallbackMetaMetricsId,
1817
} from './utils/user-segmentation-utils';
1918

2019
// === GENERAL ===
@@ -103,7 +102,7 @@ export class RemoteFeatureFlagController extends BaseController<
103102

104103
#inProgressFlagUpdate?: Promise<ServiceResponse>;
105104

106-
#getMetaMetricsId?: Promise<string | undefined>;
105+
#getMetaMetricsId: () => string;
107106

108107
/**
109108
* Constructs a new RemoteFeatureFlagController instance.
@@ -114,7 +113,7 @@ export class RemoteFeatureFlagController extends BaseController<
114113
* @param options.clientConfigApiService - The service instance to fetch remote feature flags.
115114
* @param options.fetchInterval - The interval in milliseconds before cached flags expire. Defaults to 1 day.
116115
* @param options.disabled - Determines if the controller should be disabled initially. Defaults to false.
117-
* @param options.getMetaMetricsId - Promise that resolves to a metaMetricsId.
116+
* @param options.getMetaMetricsId - Returns metaMetricsId.
118117
*/
119118
constructor({
120119
messenger,
@@ -127,7 +126,7 @@ export class RemoteFeatureFlagController extends BaseController<
127126
messenger: RemoteFeatureFlagControllerMessenger;
128127
state?: Partial<RemoteFeatureFlagControllerState>;
129128
clientConfigApiService: AbstractClientConfigApiService;
130-
getMetaMetricsId?: Promise<string | undefined>;
129+
getMetaMetricsId: () => string;
131130
fetchInterval?: number;
132131
disabled?: boolean;
133132
}) {
@@ -209,8 +208,7 @@ export class RemoteFeatureFlagController extends BaseController<
209208
remoteFeatureFlags: FeatureFlags,
210209
): Promise<FeatureFlags> {
211210
const processedRemoteFeatureFlags: FeatureFlags = {};
212-
const metaMetricsId =
213-
(await this.#getMetaMetricsId) || generateFallbackMetaMetricsId();
211+
const metaMetricsId = this.#getMetaMetricsId();
214212
const thresholdValue = generateDeterministicRandomNumber(metaMetricsId);
215213

216214
for (const [

packages/remote-feature-flag-controller/src/utils/user-segmentation-utils.test.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import { validate as uuidValidate, version as uuidVersion } from 'uuid';
2-
31
import {
42
generateDeterministicRandomNumber,
53
isFeatureFlagWithScopeValue,
6-
generateFallbackMetaMetricsId,
74
} from './user-segmentation-utils';
85

96
const MOCK_METRICS_IDS = [
@@ -75,12 +72,4 @@ describe('user-segmentation-utils', () => {
7572
).toBe(false);
7673
});
7774
});
78-
79-
describe('generateFallbackMetaMetricsId', () => {
80-
it('returns a valid uuidv4', () => {
81-
const result = generateFallbackMetaMetricsId();
82-
expect(uuidValidate(result)).toBe(true);
83-
expect(uuidVersion(result)).toBe(4);
84-
});
85-
});
8675
});

packages/remote-feature-flag-controller/src/utils/user-segmentation-utils.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { Json } from '@metamask/utils';
2-
import { v4 as uuidV4 } from 'uuid';
32

43
import type { FeatureFlagScopeValue } from '../remote-feature-flag-controller-types';
54

@@ -39,11 +38,3 @@ export const isFeatureFlagWithScopeValue = (
3938
'scope' in featureFlag
4039
);
4140
};
42-
43-
/**
44-
* Generates UUIDv4 as a fallback metaMetricsId
45-
* @returns A UUIDv4 string
46-
*/
47-
export function generateFallbackMetaMetricsId(): string {
48-
return uuidV4();
49-
}

0 commit comments

Comments
 (0)