Skip to content

Commit

Permalink
Disarm functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgreg committed May 21, 2021
1 parent a2dac77 commit c8b9f8d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ Ever wanted to implement something like how UberEats and Deliveroo ping you with

## TODO:
- Tests
- Disarm functionality
- Publish on Hex

## Installation
Expand All @@ -22,6 +21,35 @@ def deps do
end
```

## Example Usage

Sparking a fuse on an action

```elixir
)> Timebomb.start_link
{:ok, #PID<0.200.0>}
> id = Timebomb.spark(fuse: 10_000, bomb: 1+5)
"53b45c7f-8bde-4d24-ae99-a6f215bb7104"

...10 seconds later

6
```


Stopping a payload from firing

```elixir
> Timebomb.start_link
{:ok, #PID<0.200.0>}
> id = Timebomb.spark(fuse: 10_000, bomb: 1+5)
"53b45c7f-8bde-4d24-ae99-a6f215bb7104"
> Timebomb.disarm(id)
Payload disarmed
:ok
```


Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/timebomb](https://hexdocs.pm/timebomb).
34 changes: 29 additions & 5 deletions lib/timebomb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,46 @@ defmodule Timebomb do
end

def handle_info({:explode, id}, state) do
[{_, payload}] = :ets.lookup(@table, id)
case find_payload(id) do
[{_, payload}] ->
{:ok, code} = payload |> Base.url_decode64()

{:ok, code} = payload |> Base.url_decode64()
code
|> :erlang.binary_to_term()
|> IO.inspect()

code
|> :erlang.binary_to_term()
|> IO.inspect()
[] ->
nil
end

{:noreply, state}
end

def handle_cast({:disarm, id}, state) do
case :ets.delete(@table, id) do
true ->
IO.puts("Payload disarmed")

false ->
IO.puts("Unable to disarm")
end

{:noreply, state}
end

defp find_payload(id) do
:ets.lookup(@table, id)
end

def spark(opts) do
id = opts[:id] || UUID.uuid4()
opts = Keyword.put(opts, :id, id)

GenServer.cast(__MODULE__, {:spark, opts})
id
end

def disarm(id) do
GenServer.cast(__MODULE__, {:disarm, id})
end
end

0 comments on commit c8b9f8d

Please sign in to comment.