View Source ConfigCat.Hooks (ConfigCat v4.0.2)

Subscribe to events fired by the SDK.

Hooks are callback functions that are called by the SDK when certain events happen. Client applications can register more than one callback for each hook.

Callbacks are called within the same process that generated the event. Any exceptions that are raised by a callback are rescued, logged, and reported to any registered on_error callbacks.

The following callbacks are available:

  • on_client_ready: This event is sent when the SDK reaches the ready state. If the SDK is set up with lazy load or manual polling it's considered ready right after instantiation. If it's using auto polling, the ready state is reached when the SDK has a valid config JSON loaded into memory either from cache or from HTTP.
  • on_config_changed(config: map()): This event is sent when the SDK loads a valid config JSON into memory from cache, and each subsequent time when the loaded config JSON changes via HTTP.
  • on_flag_evaluated(evaluation_details: EvaluationDetails.t()): This event is sent each time when the SDK evaluates a feature flag or setting. The event sends the same evaluation details that you would get from get_value_details.
  • on_error(error: String.t()): This event is sent when an error occurs within the ConfigCat SDK.

Summary

Types

A hook callback is either an anonymous function or a module/function name/extra_arguments tuple.

t()

Functions

Add an on_client_ready callback.

Add an on_config_changed callback.

Add an on_error callback.

Add an on_flag_evaluated callback.

Returns a specification to start this module under a supervisor.

Types

@type named_callback() :: {module(), atom(), list()}

A hook callback is either an anonymous function or a module/function name/extra_arguments tuple.

Each callback is passed specific arguments. These specific arguments are prepended to the extra arguments provided in the tuple (if any).

For example, you might want to define a callback that sends a message to another process which the config changes. You can pass the pid of that process as an extra argument:

def MyModule do
  def subscribe_to_config_changes(subscriber_pid) do
    ConfigCat.hooks()
    |> ConfigCat.Hooks.add_on_config_changed({__MODULE__, :on_config_changed, [subscriber_pid]})
  end

  def on_config_changed(config, pid) do
    send pid, {:config_changed, config}
  end
end
Link to this type

on_client_ready_callback()

View Source
@type on_client_ready_callback() :: (-> any()) | named_callback()
Link to this type

on_config_changed_callback()

View Source
@type on_config_changed_callback() ::
  (ConfigCat.Config.settings() -> any()) | named_callback()
@type on_error_callback() :: (String.t() -> any()) | named_callback()
Link to this type

on_flag_evaluated_callback()

View Source
@type on_flag_evaluated_callback() ::
  (ConfigCat.EvaluationDetails.t() -> any()) | named_callback()
@type option() ::
  {:on_client_ready, on_client_ready_callback()}
  | {:on_config_changed, on_config_changed_callback()}
  | {:on_error, on_error_callback()}
  | {:on_flag_evaluated, on_flag_evaluated_callback()}
@type start_option() :: {:hooks, t()} | {:instance_id, ConfigCat.instance_id()}
@opaque t()

Functions

Link to this function

add_on_client_ready(instance_id, callback)

View Source
@spec add_on_client_ready(t(), on_client_ready_callback()) :: t()

Add an on_client_ready callback.

Link to this function

add_on_config_changed(instance_id, callback)

View Source
@spec add_on_config_changed(t(), on_config_changed_callback()) :: t()

Add an on_config_changed callback.

Link to this function

add_on_error(instance_id, callback)

View Source
@spec add_on_error(t(), on_error_callback()) :: t()

Add an on_error callback.

Link to this function

add_on_flag_evaluated(instance_id, callback)

View Source
@spec add_on_flag_evaluated(t(), on_flag_evaluated_callback()) :: t()

Add an on_flag_evaluated callback.

Returns a specification to start this module under a supervisor.

See Supervisor.