Skip to content

Commit

Permalink
Allow no value finalizers
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Apr 23, 2013
1 parent 9529693 commit 90106af
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/finalizer.ex
Expand Up @@ -7,15 +7,31 @@
# 0. You just DO WHAT THE FUCK YOU WANT TO.

defmodule Finalizer do
defp receiver(fun) do
defp receiver(fun) when is_function fun, 0 do
fn ->
receive do
_ -> fun.()
end
end
end

defp receiver(fun) when is_function fun, 1 do
fn ->
receive do
data -> fun.(data)
end
end
end

def define(data, fun) when is_function fun do
def define(fun) when is_function fun, 0 do
:resource.notify_when_destroyed(spawn(receiver(fun)), nil)
end

def define(pid) when is_pid pid do
:resource.notify_when_destroyed(pid, nil)
end

def define(data, fun) when is_function fun, 1 do
:resource.notify_when_destroyed(spawn(receiver(fun)), data)
end

Expand Down

0 comments on commit 90106af

Please sign in to comment.