Last active
June 13, 2018 07:23
-
-
Save devonestes/3c17565f39f21966a9fa5352b991efbc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule SendAfter do | |
def send_after(pid, message, nanoseconds) do | |
{_, os_name} = :os.type() | |
increment = | |
if os_name == :Windows do | |
# monotonic time for Windows in miliseconds, in Linux and MacOS it's nanoseconds | |
# so we'll need to convert to miliseconds for windows | |
nanoseconds / 1_000_000 | |
else | |
nanoseconds | |
end | |
start_time = :erlang.monotonic_time() | |
end_time = start_time + increment | |
Task.async(fn -> wait_and_send(end_time, pid, message) end) | |
end | |
def wait_and_send(end_time, pid, message) do | |
if :erlang.monotonic_time() >= end_time do | |
send(pid, message) | |
else | |
wait_and_send(end_time, pid, message) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment