Skip to content

Commit

Permalink
Move #wait implementation to Memento.Table
Browse files Browse the repository at this point in the history
  • Loading branch information
sheharyarn committed Apr 15, 2021
1 parent 71c7c50 commit 955d37c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 35 deletions.
40 changes: 5 additions & 35 deletions lib/memento/memento.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,42 +104,12 @@ defmodule Memento do



@doc """
Wait until specified tables are ready.
Before performing some tasks, it's necessary that certain tables
are ready and accessible. This call hangs until all tables
specified are accessible, or until timeout is reached
(default: 3000ms).
The `timeout` value can either be `:infinity` or an integer
representing time in milliseconds. If you pass a Table/Module that
does not exist along with `:infinity` as timeout, it will hang your
process until that table is created and ready.
For more information, see `:mnesia.wait_for_tables/2`.
## Examples
```
# Wait until the `Movies` table is ready
Memento.wait(Movies, :infinity)
# Wait a maximum of 3 seconds until the two tables are ready
Memento.wait([TableA, TableB])
```
"""
@spec wait(list(Memento.Table.name), integer | :infinity) :: :ok | {:timeout, list(Memento.Table.t)} | {:error, any}
def wait(tables, timeout \\ 3000) do
tables = List.wrap(tables)
Memento.Mnesia.call(:wait_for_tables, [tables, timeout])
end



# Delegates

defdelegate transaction(fun), to: Memento.Transaction, as: :execute
defdelegate transaction!(fun), to: Memento.Transaction, as: :execute!
defdelegate wait(tables), to: Memento.Table
defdelegate wait(tables, timeout), to: Memento.Table

defdelegate transaction(fun), to: Memento.Transaction, as: :execute
defdelegate transaction!(fun), to: Memento.Transaction, as: :execute!

end
36 changes: 36 additions & 0 deletions lib/memento/table/table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,42 @@ defmodule Memento.Table do



@doc """
Wait until specified tables are ready.
Before performing some tasks, it's necessary that certain tables
are ready and accessible. This call hangs until all tables
specified are accessible, or until timeout is reached
(default: 3000ms).
The `timeout` value can either be `:infinity` or an integer
representing time in milliseconds. If you pass a Table/Module that
does not exist along with `:infinity` as timeout, it will hang your
process until that table is created and ready.
This method can be accessed directly on the `Memento` module as well.
For more information, see `:mnesia.wait_for_tables/2`.
## Examples
```
# Wait until the `Movies` table is ready
Memento.Table.wait(Movies, :infinity)
# Wait a maximum of 3 seconds until the two tables are ready
Memento.wait([TableA, TableB])
```
"""
@spec wait(list(name), integer | :infinity) :: :ok | {:timeout, list(name)} | {:error, any}
def wait(tables, timeout \\ 3000) do
tables = List.wrap(tables)
Memento.Mnesia.call(:wait_for_tables, [tables, timeout])
end





# Private Helpers
# ---------------
Expand Down

0 comments on commit 955d37c

Please sign in to comment.