-module(bigben@fake_clock). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -define(FILEPATH, "src/bigben/fake_clock.gleam"). -export([now/1, set_now/2, advance/2, new_at/1, new/0]). -export_type([fake_clock/0, message/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(" A fake clock for manipulating the flow of time.\n"). -opaque fake_clock() :: {fake_clock, gleam@erlang@process:subject(message())}. -type message() :: {get, gleam@erlang@process:subject(gleam@time@timestamp:timestamp())} | {set, gleam@time@timestamp:timestamp()} | {advance, gleam@time@duration:duration()}. -file("src/bigben/fake_clock.gleam", 28). ?DOC(" Returns the current time on the given `FakeClock`.\n"). -spec now(fake_clock()) -> gleam@time@timestamp:timestamp(). now(Clock) -> gleam@erlang@process:call( erlang:element(2, Clock), 10, fun(Field@0) -> {get, Field@0} end ). -file("src/bigben/fake_clock.gleam", 33). ?DOC(" Sets the current time on the given `FakeClock` to the specified value.\n"). -spec set_now(fake_clock(), gleam@time@timestamp:timestamp()) -> nil. set_now(Clock, Now) -> gleam@erlang@process:send(erlang:element(2, Clock), {set, Now}). -file("src/bigben/fake_clock.gleam", 38). ?DOC(" Advances the given `FakeClock` by the specified duration.\n"). -spec advance(fake_clock(), gleam@time@duration:duration()) -> nil. advance(Clock, Duration) -> gleam@erlang@process:send(erlang:element(2, Clock), {advance, Duration}). -file("src/bigben/fake_clock.gleam", 48). -spec handle_message(gleam@time@timestamp:timestamp(), message()) -> gleam@otp@actor:next(gleam@time@timestamp:timestamp(), any()). handle_message(State, Message) -> case Message of {get, Client} -> gleam@erlang@process:send(Client, State), gleam@otp@actor:continue(State); {set, Now} -> gleam@otp@actor:continue(Now); {advance, Duration} -> gleam@otp@actor:continue(gleam@time@timestamp:add(State, Duration)) end. -file("src/bigben/fake_clock.gleam", 21). ?DOC(" Returns a new `FakeClock` at the given time.\n"). -spec new_at(gleam@time@timestamp:timestamp()) -> fake_clock(). new_at(Now) -> Actor@1 = case begin _pipe = gleam@otp@actor:new(Now), _pipe@1 = gleam@otp@actor:on_message(_pipe, fun handle_message/2), gleam@otp@actor:start(_pipe@1) end of {ok, Actor} -> Actor; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"bigben/fake_clock"/utf8>>, function => <<"new_at"/utf8>>, line => 22, value => _assert_fail, start => 557, 'end' => 649, pattern_start => 568, pattern_end => 577}) end, {fake_clock, erlang:element(3, Actor@1)}. -file("src/bigben/fake_clock.gleam", 16). ?DOC( " Returns a new `FakeClock`.\n" "\n" " The current time will be the instant the clock was created.\n" ). -spec new() -> fake_clock(). new() -> new_at(gleam@time@timestamp:system_time()).