@@ -18,7 +18,8 @@ defmodule Flagsmith.Client.Poller do
1818 :configuration ,
1919 :environment ,
2020 :refresh ,
21- :refresh_monitor
21+ :refresh_monitor ,
22+ identities_with_overrides: % { }
2223 ]
2324
2425 #################################
@@ -114,9 +115,17 @@ defmodule Flagsmith.Client.Poller do
114115
115116 def handle_event ( { :call , from } , { :get_identity_flags , identifier , traits } , _ , % __MODULE__ {
116117 environment: env ,
117- configuration: config
118+ configuration: config ,
119+ identities_with_overrides: overrides
118120 } ) do
119- identity = Schemas.Identity . from_id_traits ( identifier , traits , env . api_key )
121+ identity =
122+ case Map . get ( overrides , identifier ) do
123+ nil ->
124+ Schemas.Identity . from_id_traits ( identifier , traits , env . api_key )
125+
126+ existing ->
127+ % Schemas.Identity { existing | traits: Flagsmith.Schemas.Traits.Trait . from ( traits ) }
128+ end
120129
121130 flags =
122131 env
@@ -148,7 +157,7 @@ defmodule Flagsmith.Client.Poller do
148157 def handle_event ( :internal , :initial_load , :loading , % __MODULE__ { configuration: config } = data ) do
149158 case Flagsmith.Client . get_environment_request ( config ) do
150159 { :ok , environment } ->
151- { :next_state , :on , % __MODULE__ { data | environment: environment } ,
160+ { :next_state , :on , update_data ( data , environment ) ,
152161 [ { :next_event , :internal , :set_refresh } ] }
153162
154163 error ->
@@ -209,7 +218,7 @@ defmodule Flagsmith.Client.Poller do
209218 # a process other than the one we have stored under the `:refresh_monitor` key
210219 # we still make sure it's matching.
211220 #
212- # Then we just check if the response is an `:ok` tuple with an `Environment.t`
221+ # Then we just check if the response is an `:ok` tuple with an `Environment.t`
213222 # we replace the `:environment` key on our statem data and following user queries
214223 # will receive the new env or flags. If not we let it stay as is.
215224 #
@@ -222,8 +231,7 @@ defmodule Flagsmith.Client.Poller do
222231 ) do
223232 case result do
224233 { :ok , % Schemas.Environment { } = env } ->
225- { :keep_state , % { data | refresh_monitor: nil , environment: env } ,
226- [ { :next_event , :internal , :set_refresh } ] }
234+ { :keep_state , update_data ( data , env ) , [ { :next_event , :internal , :set_refresh } ] }
227235
228236 error ->
229237 Logger . error (
@@ -252,6 +260,21 @@ defmodule Flagsmith.Client.Poller do
252260 % __MODULE__ { configuration: config , refresh: refresh_milliseconds }
253261 end
254262
263+ # Update identities with overrides along with the environment.
264+ defp update_data ( data , environment ) do
265+ % __MODULE__ {
266+ data
267+ | refresh_monitor: nil ,
268+ environment: environment ,
269+ identities_with_overrides:
270+ Enum . reduce (
271+ environment . identity_overrides ,
272+ % { } ,
273+ fn identity , acc -> Map . put ( acc , identity . identifier , identity ) end
274+ )
275+ }
276+ end
277+
255278 @ doc false
256279 # this function is just so we can spawn a proper function with an MFA tuple
257280 def get_environment ( pid , config ) do
0 commit comments