Skip to content
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
5 changes: 5 additions & 0 deletions packages/remote-feature-flag-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add optional `force` argument to `updateRemoteFeatureFlags` to fetch even when the cache has not expired ([#9966](https://github.com/MetaMask/core/pull/9966))
- Defaults to `false`. Does not fetch when the controller is disabled.

## [6.0.0]

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type { RemoteFeatureFlagController } from './remote-feature-flag-controll
* Retrieves the remote feature flags, fetching from the API if necessary.
* Uses caching to prevent redundant API calls and handles concurrent fetches.
*
* @param force - When `true`, fetch even if the cache has not expired.
* Defaults to `false`. Has no effect when the controller is disabled.
* @returns A promise that resolves to the current set of feature flags.
*/
export type RemoteFeatureFlagControllerUpdateRemoteFeatureFlagsAction = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,52 @@ describe('RemoteFeatureFlagController', () => {
expect(controller.state.remoteFeatureFlags).toStrictEqual(MOCK_FLAGS);
});

it('fetches and updates state when cache is not expired if force is true', async () => {
const clientConfigApiService = buildClientConfigApiService({
remoteFeatureFlags: MOCK_FLAGS_TWO,
});
const { controller, messenger } = createController({
state: {
remoteFeatureFlags: MOCK_FLAGS,
cacheTimestamp: Date.now() - 10,
},
clientConfigApiService,
});

await messenger.call(
'RemoteFeatureFlagController:updateRemoteFeatureFlags',
true,
);

expect(
clientConfigApiService.fetchRemoteFeatureFlags,
).toHaveBeenCalledTimes(1);
expect(controller.state.remoteFeatureFlags).toStrictEqual(MOCK_FLAGS_TWO);
});

it('does not fetch when disabled even if force is true', async () => {
const clientConfigApiService = buildClientConfigApiService({
remoteFeatureFlags: MOCK_FLAGS_TWO,
});
const { controller, messenger } = createController({
state: {
remoteFeatureFlags: MOCK_FLAGS,
},
clientConfigApiService,
disabled: true,
});

await messenger.call(
'RemoteFeatureFlagController:updateRemoteFeatureFlags',
true,
);

expect(
clientConfigApiService.fetchRemoteFeatureFlags,
).not.toHaveBeenCalled();
expect(controller.state.remoteFeatureFlags).toStrictEqual(MOCK_FLAGS);
});

it('resets cache and fetch when clientVersion changes', async () => {
const versionedFlags = {
exploreFeature: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,12 @@ export class RemoteFeatureFlagController extends BaseController<
* Retrieves the remote feature flags, fetching from the API if necessary.
* Uses caching to prevent redundant API calls and handles concurrent fetches.
*
* @param force - When `true`, fetch even if the cache has not expired.
* Defaults to `false`. Has no effect when the controller is disabled.
* @returns A promise that resolves to the current set of feature flags.
*/
async updateRemoteFeatureFlags(): Promise<void> {
if (this.#disabled || !this.#isCacheExpired()) {
async updateRemoteFeatureFlags(force = false): Promise<void> {
if (this.#disabled || (!force && !this.#isCacheExpired())) {
return;
}

Expand Down