DevJoy.Session (DevJoy v2.0.0)

View Source

Provides functionality to work with the session.

Logic

Session module serves as a wrapper for the following sub-modules:

All functions support passing the optional locale option to retrieve the data without a need to set a locale using Storage.set_locale/1.

Groups

  • Delayed- A group of functions that uses notify_delayed_job/2 in order to run a job after the specified time and therefore send delayed notifications.

  • Immediate- A group of functions that sends the notifications immediately.

Summary

Delayed

If session is reversible navigates back in the session history.

If session is forwardable navigates forward in the session history.

Gets an item from the scene using a part name and index.

Gets a next item from the scene based on the current session item.

Immediate

Finds the accumulated choice by it's index.

Gets the current session item based on the current entry.

Gets all accumulated items for the current session entry. The items are grouped by the Item module.

Gets the current scene part based on the current entry.

Gets an action of the current scene element and calls it with a character parameter.

Types

The type representing the options used in asynchronous function group and when calling the choice action

The type representing the session's entry.

The type representing the session action to perform when getting the next session item while the current one is the last in current scene's part.

Delayed

back(opts \\ [time: 0])

@spec back(async_opts()) :: :ok

If session is reversible navigates back in the session history.

Usage

# :ok = DevJoy.API.Storage.set_history([…])
:ok = DevJoy.Session.back()

drop(amount \\ 1, opts \\ [time: 0])

@spec drop(pos_integer(), async_opts()) :: :ok

Drops the current session entry.

Usage

# :ok = DevJoy.API.Storage.set_history([…])
:ok = DevJoy.Session.drop()

forward(opts \\ [time: 0])

@spec forward(async_opts()) :: :ok

If session is forwardable navigates forward in the session history.

Usage

# :ok = DevJoy.API.Storage.set_history([…])
# :ok = DevJoy.API.Storage.set_history_index(…)
:ok = DevJoy.Session.forward()

get(arg, opts \\ [time: 0])

@spec get(entry(), async_opts()) :: :ok

Gets an item from the scene using a part name and index.

Usage

:ok = DevJoy.Session.get({MyApp.SceneName, :part_name, 1})

next(opts \\ [on_part_end: :get_current, time: 0])

@spec next(
  locale: DevJoy.API.Storage.locale(),
  on_part_end: on_part_end(),
  time: non_neg_integer()
) ::
  :ok

Gets a next item from the scene based on the current session item.

If the session history is empty it does nothing. If the current session item is the last item in the scene's part, one of the possible session actions is applied based on the on_part_end/0.

Usage

# :ok = DevJoy.API.Storage.set_history([…])
:ok = DevJoy.Session.next()

Immediate

choose(choice_index, opts \\ [time: 0])

@spec choose(pos_integer(), async_opts()) :: any()

Finds the accumulated choice by it's index.

If the choice is found it's action function would be called. Otherwise a delayed job notification would be sent with an anonymous function which logs the unexpected index error.

Usage

# :ok = DevJoy.API.Storage.set_history([…])
:ok = DevJoy.Session.choose(1)

current(opts \\ [])

@spec current([{:locale, DevJoy.API.Storage.locale()}]) :: DevJoy.API.Item.t() | nil

Gets the current session item based on the current entry.

Returns nil if the history is empty.

Usage

# :ok = DevJoy.API.Storage.set_history([…])
item = DevJoy.Session.current()
is_nil(item) or is_struct(item)
# => true

current_acc(opts \\ [])

@spec current_acc([{:locale, DevJoy.API.Storage.locale()}]) ::
  DevJoy.Scene.acc() | nil

Gets all accumulated items for the current session entry. The items are grouped by the Item module.

Returns nil if the history is empty.

Usage

# :ok = DevJoy.API.Storage.set_history([…])
acc = DevJoy.Session.current_acc()
is_nil(acc) or is_map(acc)
# => true

current_acc_for(struct_module, group \\ nil, opts \\ [])

@spec current_acc_for(DevJoy.API.Item.struct_module(), atom() | nil, [
  {:locale, DevJoy.API.Storage.locale()}
]) :: [DevJoy.API.Item.t_acc()] | nil

Gets accumulated items for the specified Item module and group.

Usage

# :ok = DevJoy.API.Storage.set_history([…])
acc = DevJoy.Session.current_acc_for(ItemModule, :group_name)
is_nil(acc) or is_list(acc)
# => true

current_part(opts \\ [])

@spec current_part([{:locale, DevJoy.API.Storage.locale()}]) ::
  DevJoy.Scene.Part.t() | nil

Gets the current scene part based on the current entry.

Returns nil if the history is empty.

Usage

# :ok = DevJoy.API.Storage.set_history([…])
part = DevJoy.Session.current_part()
is_nil(part) or is_struct(part, DevJoy.Scene.Part)
# => true

execute(opts \\ [time: 0])

@spec execute(async_opts()) :: any()

Gets an action of the current scene element and calls it with a character parameter.

The result of the action is compared against the content of the accumulated choices. If the choice is found it's action would be called. Otherwise a fallback action would be called which logs the unexpected return value.

Usage

# :ok = DevJoy.API.Storage.set_history([…])
:ok = DevJoy.Session.execute()

Types

async_opts()

@type async_opts() :: [locale: DevJoy.API.Storage.locale(), time: non_neg_integer()]

The type representing the options used in asynchronous function group and when calling the choice action:

  1. locale - the locale to use when retrieving item or accumulated item
  2. time - amount of the miliseconds to wait before sending delayed job notification

entry()

The type representing the session's entry.

The entry is 3-element tuple containing:

  1. scene name
  2. part name
  3. item index

on_part_end()

@type on_part_end() :: :get_current | :get_next | :notify_index_out_of_bounds

The type representing the session action to perform when getting the next session item while the current one is the last in current scene's part.

  • :get_current - returns current session entry after dropping any forward history entries and the current history entry
  • :get_next - returns next session entry after dropping any forward history entries and the current history entry
  • :notify_index_out_of_bounds - sends out of bounds notification using Notifier.notify_index_out_of_bounds/0