11const utils = require ( './utils' ) ;
22
3+ /**
4+ * FlagStore - Centralized flag store and access point for all feature flags
5+ *
6+ * This module manages two types of feature flags:
7+ * 1. Regular flags - Retrieved from LaunchDarkly servers or bootstrap data
8+ * 2. Override flags - Local overrides for debugging/testing
9+ *
10+ * When a flag is requested:
11+ * - If an override exists for that flag, the override value is returned
12+ * - Otherwise, the regular flag value is returned
13+ */
314function FlagStore ( ) {
415 let flags = { } ;
16+ // The flag overrides are set lazily to allow bypassing property checks when no overrides are present.
517 let flagOverrides ;
618
19+ /**
20+ * Gets a single flag by key, with overrides taking precedence over regular flags
21+ * @param {string } key The flag key to retrieve
22+ * @returns {Object|null } The flag object or null if not found
23+ */
724 function get ( key ) {
825 // Check overrides first, then real flags
926 if ( flagOverrides && utils . objectHasOwnProperty ( flagOverrides , key ) && flagOverrides [ key ] ) {
@@ -17,6 +34,10 @@ function FlagStore() {
1734 return null ;
1835 }
1936
37+ /**
38+ * Gets all flags with overrides applied
39+ * @returns {Object } Object containing all flags with any overrides applied
40+ */
2041 function getFlagsWithOverrides ( ) {
2142 const result = { } ;
2243
@@ -41,17 +62,30 @@ function FlagStore() {
4162 return result ;
4263 }
4364
65+ /**
66+ * Replaces all flags with new flag data
67+ * @param {Object } newFlags - Object containing the new flag data
68+ */
4469 function setFlags ( newFlags ) {
4570 flags = { ...newFlags } ;
4671 }
4772
73+ /**
74+ * Sets an override value for a specific flag
75+ * @param {string } key The flag key to override
76+ * @param {* } value The override value for the flag
77+ */
4878 function setOverride ( key , value ) {
4979 if ( ! flagOverrides ) {
5080 flagOverrides = { } ;
5181 }
5282 flagOverrides [ key ] = { value } ;
5383 }
5484
85+ /**
86+ * Removes an override for a specific flag
87+ * @param {string } key The flag key to remove the override for
88+ */
5589 function removeOverride ( key ) {
5690 if ( ! flagOverrides || ! flagOverrides [ key ] ) {
5791 return ; // No override to remove
@@ -65,6 +99,10 @@ function FlagStore() {
6599 }
66100 }
67101
102+ /**
103+ * Clears all flag overrides and returns the cleared overrides
104+ * @returns {Object } The overrides that were cleared, useful for tracking what was removed
105+ */
68106 function clearAllOverrides ( ) {
69107 if ( ! flagOverrides ) {
70108 return { } ; // No overrides to clear, return empty object for consistency
@@ -75,10 +113,18 @@ function FlagStore() {
75113 return clearedOverrides ;
76114 }
77115
116+ /**
117+ * Gets the internal flag state without overrides applied
118+ * @returns {Object } The internal flag data structure
119+ */
78120 function getFlags ( ) {
79121 return flags ;
80122 }
81123
124+ /**
125+ * Gets the flag overrides data
126+ * @returns {Object } The flag overrides object, or empty object if no overrides exist
127+ */
82128 function getFlagOverrides ( ) {
83129 return flagOverrides || { } ;
84130 }
0 commit comments