From 955d37cf4a85ebbfe3cdba6c5562e80e3c2390fe Mon Sep 17 00:00:00 2001 From: Sheharyar Naseer Date: Thu, 15 Apr 2021 17:51:49 +0500 Subject: [PATCH] Move #wait implementation to Memento.Table --- lib/memento/memento.ex | 40 +++++--------------------------------- lib/memento/table/table.ex | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 35 deletions(-) diff --git a/lib/memento/memento.ex b/lib/memento/memento.ex index 5aca6bc..21c9f0c 100644 --- a/lib/memento/memento.ex +++ b/lib/memento/memento.ex @@ -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 diff --git a/lib/memento/table/table.ex b/lib/memento/table/table.ex index 4787394..60e3d43 100644 --- a/lib/memento/table/table.ex +++ b/lib/memento/table/table.ex @@ -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 # ---------------