Current section

Files

Jump to
telega src telega@flow@storage.erl
Raw

src/telega@flow@storage.erl

-module(telega@flow@storage).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/telega/flow/storage.gleam").
-export([generate_id/3, create_ets_storage/0, create_noop_storage/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Storage utilities for flow persistence.\n"
"\n"
" `FlowStorage` is now derived from the unified `telega/storage`\n"
" `KeyValueStorage` contract. The ETS implementation lives in\n"
" `telega/storage/ets`; this module wires it into the flow contract.\n"
).
-file("src/telega/flow/storage.gleam", 16).
?DOC(" Generate a flow instance ID from user, chat, and flow name\n").
-spec generate_id(integer(), integer(), binary()) -> binary().
generate_id(User_id, Chat_id, Flow_name) ->
<<<<<<<<Flow_name/binary, "_"/utf8>>/binary,
(erlang:integer_to_binary(Chat_id))/binary>>/binary,
"_"/utf8>>/binary,
(erlang:integer_to_binary(User_id))/binary>>.
-file("src/telega/flow/storage.gleam", 25).
?DOC(
" Create ETS-backed storage for flow instances.\n"
"\n"
" Data persists in memory for the lifetime of the VM but does NOT survive VM\n"
" restarts. For persistence across restarts, build a `FlowStorage` from a\n"
" database-backed `KeyValueStorage` via `storage.flow_storage_from_storage`.\n"
).
-spec create_ets_storage() -> {ok, telega@flow@types:flow_storage(any())} |
{error, telega@error:telega_error()}.
create_ets_storage() ->
gleam@result:'try'(
telega@storage@ets:new(<<"telega_flow_storage"/utf8>>),
fun(Kv) -> {ok, telega@storage:flow_storage_from_storage(Kv)} end
).
-file("src/telega/flow/storage.gleam", 32).
?DOC(
" Create no-op storage that discards all data.\n"
" Useful for testing or stateless flows that don't need persistence.\n"
).
-spec create_noop_storage() -> telega@flow@types:flow_storage(any()).
create_noop_storage() ->
{flow_storage,
fun(_) -> {ok, nil} end,
fun(_) -> {ok, none} end,
fun(_) -> {ok, nil} end,
fun(_, _) -> {ok, []} end}.