DevJoy.Session (DevJoy v2.0.0)
View SourceProvides 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 usesnotify_delayed_job/2in 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 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 accumulated items for the specified
Item module and group.
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.
Delayed
@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()@spec drop(pos_integer(), async_opts()) :: :ok
Drops the current session entry.
Usage
# :ok = DevJoy.API.Storage.set_history([…])
:ok = DevJoy.Session.drop()@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()@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})@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
@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)@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@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@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@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@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
@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:
locale- the locale to use when retrieving item or accumulated itemtime- amount of the miliseconds to wait before sending delayed job notification
@type entry() :: {DevJoy.Scene.t(), DevJoy.Scene.Part.name(), DevJoy.API.Item.index()} | nil
The type representing the session's entry.
The entry is 3-element tuple containing:
@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 usingNotifier.notify_index_out_of_bounds/0