View Source ECSx.Persistence.Behaviour behaviour (ECSx v0.5.1)

By default, ECSx persists component data by writing a binary file to disk, then reading the file when the server restarts. If you would like to use a different method, you can create a module which implements this behaviour, and update the ECSx configuration to use your module instead of the default.

persist_components-2-and-retrieve_components-1

persist_components/2 and retrieve_components/1

To create your own persistence adapter, you only need to implement two functions:

  • persist_components/2 - This function takes a map, where keys are component type modules, and values are lists of persistable components of that type. A keyword list of options is also be passed as a second argument. The function should store the data, then return :ok.
  • retrieve_components/1 - This function takes a list of options, and should return {:ok, component_map} where component_map stores lists of component tuples as values, with the keys being the component type module corresponding to each list.

configuring-ecsx-to-use-a-custom-persistence-adapter

Configuring ECSx to use a custom persistence adapter

Once you have created a persistence adapter module, simply update your application config to use it:

config :ecsx,
  ...
  persistence_adapter: MyAdapterModule

Link to this section Summary

Link to this section Types

@type components() :: %{required(module()) => [tuple()]}

Link to this section Callbacks

Link to this callback

persist_components(components, opts)

View Source
@callback persist_components(components :: components(), opts :: keyword()) :: :ok
Link to this callback

retrieve_components(opts)

View Source
@callback retrieve_components(opts :: keyword()) ::
  {:ok, components()} | {:error, :fresh_server | any()}