-
-
Notifications
You must be signed in to change notification settings - Fork 293
Feat: Seed remote feature flag controller with default flags #9747
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
Open
Cal-L
wants to merge
2
commits into
main
Choose a base branch
from
feat/seed-defaults-remote-feature-flag-controller
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -214,6 +214,8 @@ export class RemoteFeatureFlagController extends BaseController< | |
|
|
||
| readonly #clientVersion: SemVerVersion; | ||
|
|
||
| readonly #defaultFeatureFlags: FeatureFlags; | ||
|
|
||
| #processedRemoteFeatureFlags: FeatureFlags = {}; | ||
|
|
||
| /** | ||
|
|
@@ -228,6 +230,7 @@ export class RemoteFeatureFlagController extends BaseController< | |
| * @param options.getMetaMetricsId - Returns metaMetricsId. | ||
| * @param options.clientVersion - The current client version for version-based feature flag filtering. Must be a valid 3-part SemVer version string. | ||
| * @param options.prevClientVersion - The previous client version for feature flag cache invalidation. | ||
| * @param options.defaultFeatureFlags - Client-side default feature flags used as the lowest-precedence layer under processed remote flags and local overrides. Not persisted. | ||
| */ | ||
| constructor({ | ||
| messenger, | ||
|
|
@@ -238,6 +241,7 @@ export class RemoteFeatureFlagController extends BaseController< | |
| getMetaMetricsId, | ||
| clientVersion, | ||
| prevClientVersion, | ||
| defaultFeatureFlags = {}, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ability for platforms to provide feature flag defaults |
||
| }: { | ||
| messenger: RemoteFeatureFlagControllerMessenger; | ||
| state?: Partial<RemoteFeatureFlagControllerState>; | ||
|
|
@@ -247,6 +251,7 @@ export class RemoteFeatureFlagController extends BaseController< | |
| disabled?: boolean; | ||
| clientVersion: string; | ||
| prevClientVersion?: string; | ||
| defaultFeatureFlags?: FeatureFlags; | ||
| }) { | ||
| if (!isValidSemVerVersion(clientVersion)) { | ||
| throw new Error( | ||
|
|
@@ -264,6 +269,23 @@ export class RemoteFeatureFlagController extends BaseController< | |
| prevClientVersion !== clientVersion; | ||
|
|
||
| const localOverrides = initialState.localOverrides ?? {}; | ||
| const rawRemoteFeatureFlags = initialState.rawRemoteFeatureFlags ?? {}; | ||
|
|
||
| // Rebuild the processed remote layer from last session's effective flags by | ||
| // stripping local overrides and default-only keys (absent from raw). | ||
| const processedRemoteFeatureFlags = { | ||
| ...initialState.remoteFeatureFlags, | ||
| }; | ||
| for (const [flagName, overrideValue] of Object.entries(localOverrides)) { | ||
| if (processedRemoteFeatureFlags[flagName] === overrideValue) { | ||
| delete processedRemoteFeatureFlags[flagName]; | ||
| } | ||
| } | ||
| for (const flagName of Object.keys(defaultFeatureFlags)) { | ||
| if (rawRemoteFeatureFlags[flagName] === undefined) { | ||
| delete processedRemoteFeatureFlags[flagName]; | ||
| } | ||
| } | ||
|
|
||
| super({ | ||
| name: controllerName, | ||
|
|
@@ -272,7 +294,8 @@ export class RemoteFeatureFlagController extends BaseController< | |
| state: { | ||
| ...initialState, | ||
| remoteFeatureFlags: { | ||
| ...initialState.remoteFeatureFlags, | ||
| ...defaultFeatureFlags, | ||
| ...processedRemoteFeatureFlags, | ||
| ...localOverrides, | ||
| }, | ||
| cacheTimestamp: hasClientVersionChanged | ||
|
|
@@ -281,15 +304,8 @@ export class RemoteFeatureFlagController extends BaseController< | |
| }, | ||
| }); | ||
|
|
||
| this.#processedRemoteFeatureFlags = { | ||
| ...initialState.remoteFeatureFlags, | ||
| }; | ||
| for (const [flagName, overrideValue] of Object.entries(localOverrides)) { | ||
| if (this.#processedRemoteFeatureFlags[flagName] === overrideValue) { | ||
| delete this.#processedRemoteFeatureFlags[flagName]; | ||
| } | ||
| } | ||
|
|
||
| this.#defaultFeatureFlags = defaultFeatureFlags; | ||
| this.#processedRemoteFeatureFlags = processedRemoteFeatureFlags; | ||
| this.#fetchInterval = fetchInterval; | ||
| this.#disabled = disabled; | ||
| this.#clientConfigApiService = clientConfigApiService; | ||
|
|
@@ -302,6 +318,25 @@ export class RemoteFeatureFlagController extends BaseController< | |
| ); | ||
| } | ||
|
|
||
|
cursor[bot] marked this conversation as resolved.
|
||
| /** | ||
| * Computes effective feature flags with precedence: | ||
| * defaults < processed remote < local overrides. | ||
| * | ||
| * @param processedRemote - The processed remote feature flags. | ||
| * @param localOverrides - Local overrides. Defaults to current state overrides. | ||
| * @returns The effective feature flags. | ||
| */ | ||
| #getEffectiveFeatureFlags( | ||
| processedRemote: FeatureFlags, | ||
| localOverrides: FeatureFlags = this.state.localOverrides ?? {}, | ||
| ): FeatureFlags { | ||
| return { | ||
| ...this.#defaultFeatureFlags, | ||
| ...processedRemote, | ||
| ...localOverrides, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if the cached feature flags are expired based on the fetch interval. | ||
| * | ||
|
|
@@ -388,10 +423,9 @@ export class RemoteFeatureFlagController extends BaseController< | |
| this.update(() => { | ||
| return { | ||
| ...this.state, | ||
| remoteFeatureFlags: { | ||
| ...redactedProcessedFlags, | ||
| ...this.state.localOverrides, | ||
| }, | ||
| remoteFeatureFlags: this.#getEffectiveFeatureFlags( | ||
| redactedProcessedFlags, | ||
| ), | ||
| rawRemoteFeatureFlags: redactMetaMetricsIds(remoteFeatureFlags), | ||
| cacheTimestamp: Date.now(), | ||
| thresholdCache: updatedThresholdCache, | ||
|
|
@@ -543,10 +577,10 @@ export class RemoteFeatureFlagController extends BaseController< | |
| return { | ||
| ...this.state, | ||
| localOverrides, | ||
| remoteFeatureFlags: { | ||
| ...this.state.remoteFeatureFlags, | ||
| [flagName]: value, | ||
| }, | ||
| remoteFeatureFlags: this.#getEffectiveFeatureFlags( | ||
| this.#processedRemoteFeatureFlags, | ||
| localOverrides, | ||
| ), | ||
| }; | ||
| }); | ||
| } | ||
|
|
@@ -560,20 +594,14 @@ export class RemoteFeatureFlagController extends BaseController< | |
| const newLocalOverrides = { ...this.state.localOverrides }; | ||
| delete newLocalOverrides[flagName]; | ||
|
|
||
| const remoteFeatureFlags = { ...this.state.remoteFeatureFlags }; | ||
| const processedValue = this.#processedRemoteFeatureFlags[flagName]; | ||
|
|
||
| if (processedValue === undefined) { | ||
| delete remoteFeatureFlags[flagName]; | ||
| } else { | ||
| remoteFeatureFlags[flagName] = processedValue; | ||
| } | ||
|
|
||
| this.update(() => { | ||
| return { | ||
| ...this.state, | ||
| localOverrides: newLocalOverrides, | ||
| remoteFeatureFlags, | ||
| remoteFeatureFlags: this.#getEffectiveFeatureFlags( | ||
| this.#processedRemoteFeatureFlags, | ||
| newLocalOverrides, | ||
| ), | ||
| }; | ||
| }); | ||
| } | ||
|
|
@@ -586,7 +614,10 @@ export class RemoteFeatureFlagController extends BaseController< | |
| return { | ||
| ...this.state, | ||
| localOverrides: {}, | ||
| remoteFeatureFlags: { ...this.#processedRemoteFeatureFlags }, | ||
| remoteFeatureFlags: this.#getEffectiveFeatureFlags( | ||
| this.#processedRemoteFeatureFlags, | ||
| {}, | ||
| ), | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: what is the different between
rawRemoteFeatureFlagsanddefaultFeatureFlags?