-module(glisten). -compile(no_auto_import). -export([serve/3]). -export_type([start_error/0]). -type start_error() :: listener_closed | listener_timeout | acceptor_timeout | {acceptor_failed, gleam@otp@process:exit_reason()} | {acceptor_crashed, gleam@dynamic:dynamic()}. -spec serve( integer(), fun((glisten@tcp:handler_message(), glisten@tcp:loop_state(GVG)) -> gleam@otp@actor:next(glisten@tcp:loop_state(GVG))), GVG ) -> {ok, nil} | {error, start_error()}. serve(Port, Handler, Initial_state) -> case begin _pipe = Port, _pipe@1 = glisten@tcp:listen(_pipe, []), _pipe@2 = gleam@result:map_error(_pipe@1, fun(Err) -> case Err of closed -> listener_closed; timeout -> listener_timeout end end), gleam@result:then( _pipe@2, fun(Socket) -> _pipe@3 = Socket, _pipe@4 = glisten@tcp:start_acceptor_pool( _pipe@3, Handler, Initial_state, 10 ), gleam@result:map_error(_pipe@4, fun(Err@1) -> case Err@1 of init_timeout -> acceptor_timeout; {init_failed, Reason} -> {acceptor_failed, Reason}; {init_crashed, Reason@1} -> {acceptor_crashed, Reason@1} end end) end ) end of {error, _try} -> {error, _try}; {ok, _@1} -> {ok, nil} end.