Maxine.Callbacks (Maxine v0.2.6)

Helper functions for use in callbacks. These get their own module because they really don't belong with generate/2 and advance/3. The idea is that you

  import Maxine

when you're controlling state machines, and you

  import Maxine.Callbacks

when you're writing callback functions.

Link to this section Summary

Functions

Avoid annoying one-layer "deep merge" issues when sticking things in %Data{} structs.

Tag a %Data{} with an event to fire automatically next. (Note here: Checking the key on the tmp map directly as we do in the examples below is not how you want to use it, and means implementation is leaking into the doctests, which isn't ideal, but the corresponding function to read this stuff is private in Maxine so it's frankly easier and more transparent here to just roll with it, as long as we're all clear that you should probably not do things like this in production/real life.)

Link to this section Functions

Link to this function

merge_data(data, section, to_merge)

Specs

merge_data(
  %Maxine.Data{app: term(), options: term(), tmp: term()},
  Maxine.Data.sections(),
  map()
) :: %Maxine.Data{app: term(), options: term(), tmp: term()}

Avoid annoying one-layer "deep merge" issues when sticking things in %Data{} structs.

Parameters

  • data: The %Data{} struct we're merging into
  • section: The slice of data we're using; can be :app, :options or :tmp
  • to_merge: A map holding the data we want to merge

Examples

iex> new_data = Maxine.Callbacks.merge_data(%Maxine.Data{}, :app, %{hello: "world"})
iex> new_data.app[:hello]
"world"
Link to this function

request(data, event, options \\ [])

Specs

request(
  data :: %Maxine.Data{app: term(), options: term(), tmp: term()},
  event :: Maxine.Machine.event_name(),
  options :: Maxine.Machine.event_options()
) :: %Maxine.Data{app: term(), options: term(), tmp: term()}

Tag a %Data{} with an event to fire automatically next. (Note here: Checking the key on the tmp map directly as we do in the examples below is not how you want to use it, and means implementation is leaking into the doctests, which isn't ideal, but the corresponding function to read this stuff is private in Maxine so it's frankly easier and more transparent here to just roll with it, as long as we're all clear that you should probably not do things like this in production/real life.)

Parameters

  • data: the %Data{} struct we're tagging - event: the event we'd like fired when this callback cycle is done (atom) - options: Optional, any thing we'd like to go along with the event

Examples

iex> new_data = Maxine.Callbacks.request(%Maxine.Data{}, :ship, foo: "bar")
iex> new_data.tmp[:_maxine_next_event]
:ship

iex> new_data = Maxine.Callbacks.request(%Maxine.Data{}, :ship, foo: "bar")
iex> new_data.tmp[:_maxine_next_options][:foo]
"bar"