@@ -40,8 +40,8 @@ target_match
4040}.
4141
4242-type all_flags_state_options () :: #{
43- with_reasons => boolean ()
44- % % client_side_only => boolean(), % TODO: Support.
43+ with_reasons => boolean (),
44+ client_side_only => boolean ()
4545% % details_only_for_tracked_flags => boolean() % TODO: Support.
4646}.
4747
@@ -113,6 +113,26 @@ all_flags_state(Context, Options, Tag) ->
113113is_not_deleted (#{deleted := true }) -> false ;
114114is_not_deleted (_ ) -> true .
115115
116+ -spec is_prereq_of (FlagKey :: ldclient_flag :key (), Event :: ldclient_event :event ()) -> boolean ().
117+ is_prereq_of (FlagKey , #{data := #{prereq_of := PrereqOf }} = _Event ) ->
118+ FlagKey =:= PrereqOf .
119+
120+ -spec maybe_add_prerequisites (Prerequisites :: [ldclient_flag :key ()], Map :: map ()) -> map ().
121+ maybe_add_prerequisites ([] = _Prerequisites , Map ) -> Map ;
122+ maybe_add_prerequisites (Prerequisites , Map ) -> Map #{<<" prerequisites" >> => Prerequisites }.
123+
124+
125+ -spec is_visible (Item :: map (), Options :: all_flags_state_options ()) -> boolean ().
126+ is_visible (
127+ #{clientSideAvailability := #{
128+ usingEnvironmentId := UsingEnvironmentId
129+ }} = _Item ,
130+ #{client_side_only := true } = _Options
131+ ) -> UsingEnvironmentId ;
132+ % % All flags will have the availability set via parsing. So when client_side_only is not true
133+ % % we want everything to be visible.
134+ is_visible (_ , _ ) -> true .
135+
116136-spec all_flags_state (
117137 Context :: ldclient_context :context (),
118138 Options :: all_flags_state_options (),
@@ -127,17 +147,26 @@ all_flags_state(_Context, _Options, _Tag, _, not_initialized) ->
127147all_flags_state (Context , #{with_reasons := WithReason } = _Options , Tag , Offline , store_initialized ) ->
128148 error_logger :warning_msg (" Called allFlagsState before client initialization; using last known values from data store." ),
129149 all_flags_state (Context , #{with_reasons := WithReason } = _Options , Tag , Offline , initialized );
130- all_flags_state (Context , #{with_reasons := WithReason } = _Options , Tag , _ , initialized ) ->
150+ all_flags_state (Context , #{with_reasons := WithReason } = Options , Tag , _ , initialized ) ->
131151 FeatureStore = ldclient_config :get_value (Tag , feature_store ),
132- AllFlags = [Flag || Flag = {_ , FlagValue } <- FeatureStore :all (Tag , features ), is_not_deleted (FlagValue )],
152+ AllFlags = [Flag || Flag = {_ , FlagValue } <- FeatureStore :all (Tag , features ), is_not_deleted (FlagValue ), is_visible ( FlagValue , Options ) ],
133153 EvalFun = fun ({FlagKey , #{version := Version } = Flag }, #{<<" $flagsState" >> := FlagsState } = Acc ) ->
134154 % Here the state is either initialized, or store_initialized, and we are online. Call directly to that version
135155 % of flag_key_for_context. This will prevent additional warnings for the client initialization not being
136156 % complete in the store_initialized state.
137- {{VariationIndex , V , Reason }, _Events } = flag_key_for_context (Tag , FlagKey , Context , null , online , initialized ),
138- FlagState = maybe_add_track_events (Flag ,
157+ {{VariationIndex , V , Reason }, Events } = flag_key_for_context (Tag , FlagKey , Context , null , online , initialized ),
158+ DirectPrereqEvents = lists :filter (fun (Event ) -> is_prereq_of (FlagKey , Event ) end , Events ),
159+ Prereqs = lists :reverse (lists :map (fun (Event ) ->
160+ #{data := #{key := Key }} = Event ,
161+ Key
162+ end ,
163+ DirectPrereqEvents
164+ )),
165+ FlagState =
166+ maybe_add_prerequisites (Prereqs ,
167+ maybe_add_track_events (Flag ,
139168 maybe_add_debug_events_until_date (Flag , #{
140- <<" version" >> => Version })),
169+ <<" version" >> => Version }))) ,
141170 UpdatedFlagState = case is_integer (VariationIndex ) of
142171 true -> FlagState #{
143172 <<" variation" >> => VariationIndex
0 commit comments