diff --git a/src/machinery.erl b/src/machinery.erl index 24dcc69..3c62261 100644 --- a/src/machinery.erl +++ b/src/machinery.erl @@ -25,15 +25,15 @@ -type aux_state(T) :: T. -type event_cursor() :: undefined | event_id(). --type limit() :: undefined | pos_integer(). +-type limit() :: undefined | non_neg_integer(). -type direction() :: forward | backward. -type range() :: {event_cursor(), limit(), direction()}. -type signal(T) :: {init, args(T)} | timeout. --type machine(T) :: #{ +-type machine(E, A) :: #{ namespace := namespace(), id := id(), - history := history(T), - aux_state := aux_state(_) + history := history(E), + aux_state := aux_state(A) %% TODO %% history_range ? %% timer ? @@ -44,7 +44,7 @@ -export_type([range/0]). -export_type([args/1]). -export_type([response/1]). --export_type([machine/1]). +-export_type([machine/2]). -type modopts(O) :: module() | {module(), O}. @@ -76,15 +76,15 @@ -export([dispatch_call/4]). %% Behaviour definition --type seconds() :: pos_integer(). +-type seconds() :: non_neg_integer(). -type timer() :: {timeout, seconds()} | {deadline, timestamp()}. --type result(T) :: #{ - events => [event_body(T)], +-type result(E, A) :: #{ + events => [event_body(E)], action => action() | [action()], - aux_state => aux_state(_) + aux_state => aux_state(A) }. -type action() :: @@ -93,17 +93,19 @@ continue | remove. --export_type([result/1]). +-export_type([timestamp/0]). +-export_type([seconds/0]). +-export_type([result/2]). -export_type([action/0]). --callback init(args(_), machine(T), handler_args(_), handler_opts(_)) -> - result(T). +-callback init(args(_), machine(E, A), handler_args(_), handler_opts(_)) -> + result(E, A). --callback process_timeout(machine(T), handler_args(_), handler_opts(_)) -> - result(T). +-callback process_timeout(machine(E, A), handler_args(_), handler_opts(_)) -> + result(E, A). --callback process_call(args(_), machine(T), handler_args(_), handler_opts(_)) -> - {response(_), result(T)}. +-callback process_call(args(_), machine(E, A), handler_args(_), handler_opts(_)) -> + {response(_), result(E, A)}. %% API @@ -125,26 +127,26 @@ call(NS, ID, Range, Args, Backend) -> machinery_backend:call(Module, NS, ID, Range, Args, Opts). -spec get(namespace(), id(), backend(_)) -> - {ok, machine(_)} | {error, notfound}. + {ok, machine(_, _)} | {error, notfound}. get(NS, ID, Backend) -> get(NS, ID, {undefined, undefined, forward}, Backend). -spec get(namespace(), id(), range(), backend(_)) -> - {ok, machine(_)} | {error, notfound}. + {ok, machine(_, _)} | {error, notfound}. get(NS, ID, Range, Backend) -> {Module, Opts} = machinery_utils:get_backend(Backend), machinery_backend:get(Module, NS, ID, Range, Opts). %% Internal API --spec dispatch_signal(signal(_), machine(T), logic_handler(_), handler_opts(_)) -> - result(T). +-spec dispatch_signal(signal(_), machine(E, A), logic_handler(_), handler_opts(_)) -> + result(E, A). dispatch_signal({init, Args}, Machine, {Handler, HandlerArgs}, Opts) -> Handler:init(Args, Machine, HandlerArgs, Opts); dispatch_signal(timeout, Machine, {Handler, HandlerArgs}, Opts) -> Handler:process_timeout(Machine, HandlerArgs, Opts). --spec dispatch_call(args(_), machine(T), logic_handler(_), handler_opts(_)) -> - {response(_), result(T)}. +-spec dispatch_call(args(_), machine(E, A), logic_handler(_), handler_opts(_)) -> + {response(_), result(E, A)}. dispatch_call(Args, Machine, {Handler, HandlerArgs}, Opts) -> Handler:process_call(Args, Machine, HandlerArgs, Opts). diff --git a/src/machinery_backend.erl b/src/machinery_backend.erl index 951282d..e9e402a 100644 --- a/src/machinery_backend.erl +++ b/src/machinery_backend.erl @@ -23,7 +23,7 @@ {ok, machinery:response(_)} | {error, notfound}. -callback get(namespace(), id(), range(), backend_opts()) -> - {ok, machinery:machine(_)} | {error, notfound}. + {ok, machinery:machine(_, _)} | {error, notfound}. %% API @@ -40,6 +40,6 @@ call(Backend, Namespace, Id, Range, Args, Opts) -> Backend:call(Namespace, Id, Range, Args, Opts). -spec get(backend(), namespace(), id(), range(), backend_opts()) -> - {ok, machinery:machine(_)} | {error, notfound}. + {ok, machinery:machine(_, _)} | {error, notfound}. get(Backend, Namespace, Id, Range, Opts) -> Backend:get(Namespace, Id, Range, Opts). diff --git a/src/machinery_machine_unique_tag.erl b/src/machinery_machine_unique_tag.erl index 2108b5e..80a6b5f 100644 --- a/src/machinery_machine_unique_tag.erl +++ b/src/machinery_machine_unique_tag.erl @@ -70,9 +70,9 @@ construct_namespace(NS) -> %% --type machine() :: machinery:machine(ev()). +-type machine() :: machinery:machine(ev(), _). -type handler_opts() :: machinery:handler_opts(_). --type result() :: machinery:result(ev()). +-type result() :: machinery:result(ev(), _). -type response() :: machinery:response( ok | {error, id()} ). diff --git a/src/machinery_mg_backend.erl b/src/machinery_mg_backend.erl index e53285d..fc3d4fd 100644 --- a/src/machinery_mg_backend.erl +++ b/src/machinery_mg_backend.erl @@ -13,7 +13,7 @@ -type range() :: machinery:range(). -type args(T) :: machinery:args(T). -type response(T) :: machinery:response(T). --type machine(T) :: machinery:machine(T). +-type machine(E, A) :: machinery:machine(E, A). -type logic_handler(T) :: machinery:logic_handler(T). -define(BACKEND_CORE_OPTS, @@ -138,7 +138,7 @@ call(NS, ID, Range, Args, Opts) -> end. -spec get(namespace(), id(), range(), backend_opts()) -> - {ok, machine(_)} | {error, notfound}. + {ok, machine(_, _)} | {error, notfound}. get(NS, ID, Range, Opts) -> Client = get_client(Opts), Schema = get_schema(Opts), @@ -304,7 +304,7 @@ marshal(action, V) -> marshal(timer, {timeout, V}) when V > 0 -> {timeout, marshal(integer, V)}; -marshal(deadline, {deadline, V}) -> +marshal(timer, {deadline, V}) -> {deadline, marshal(timestamp, V)}; marshal(namespace, V) ->