Current section
Files
Jump to
Current section
Files
lib/feature_flags.ex
defmodule Flagship.FeatureFlags do
@moduledoc false
use GenServer
require Logger
@check_ms 500
def start_link(opts) do
GenServer.start_link(__MODULE__, :ok, opts)
end
@doc """
Gets a feature flag value (or fallback) for the given user key if targeted individually
## Examples
iex> App.FeatureFlags.get(FeatureFlags, "flag_name", false, :none)
true
Flag not set, and fallback is true
iex> App.FeatureFlags.get("flag_name", true, "OH")
true
In the UI, add "CO" as an individual target
iex> App.FeatureFlags.get("flag_name", false, "OH")
false
iex> App.FeatureFlags.get("flag_name", false, "CO")
true
"""
def get(key, fallback, location_code \\ :none) do
GenServer.call(__MODULE__, {:get, key, fallback, String.upcase(to_string(location_code))})
end
def init(:ok) do
ldclient_options = Application.get_env(:flagship, :ld_client_options, %{})
launch_darkly_sdk_key = String.to_charlist(Application.get_env(:flagship, :ld_sdk_key))
:ldclient.start_instance(launch_darkly_sdk_key, ldclient_options)
wait_for_initialization()
{:ok, %{}}
end
def handle_call({:get, key, fallback, location_code}, _from, state) do
{:reply, :ldclient.variation(key, %{:kind => "location", :key => location_code}, fallback),
state}
end
def initialized? do
:ldclient.initialized(:default)
end
def wait_for_initialization do
Logger.info("Waiting for LaunchDarkly flag data...")
if Flagship.FeatureFlags.initialized?() do
:initialized
else
Process.sleep(@check_ms)
wait_for_initialization()
end
end
end