Current section
10 Versions
Jump to
Current section
10 Versions
Compare versions
4
files changed
+26
additions
-6
deletions
| @@ -34,6 +34,13 @@ The queue is designed to be used from more complex examples in which the | |
| 34 34 | producer and consumer are in separate processes and run assynchronously to each |
| 35 35 | other. |
| 36 36 | |
| 37 | + An example of an infinite stream: |
| 38 | + ```elixir |
| 39 | + {:ok, pid} = BlockingQueue.start_link(:infinity) |
| 40 | + BlockingQueue.push(pid, "Hi") |
| 41 | + BlockingQueue.pop(pid) # should return "Hi" |
| 42 | + ``` |
| 43 | + |
| 37 44 | An example using the `Stream` API |
| 38 45 | |
| 39 46 | ```elixir |
| @@ -10,4 +10,4 @@ | |
| 10 10 | {<<"maintainers">>,[<<"Joseph Kain">>]}. |
| 11 11 | {<<"name">>,<<"blocking_queue">>}. |
| 12 12 | {<<"requirements">>,[]}. |
| 13 | - {<<"version">>,<<"1.0.3">>}. |
| 13 | + {<<"version">>,<<"1.0.4">>}. |
| @@ -14,6 +14,10 @@ defmodule BlockingQueue do | |
| 14 14 | {:ok, pid} = BlockingQueue.start_link(5) |
| 15 15 | BlockingQueue.push(pid, "Hi") |
| 16 16 | BlockingQueue.pop(pid) # should return "Hi" |
| 17 | + |
| 18 | + {:ok, pid} = BlockingQueue.start_link(:infinity) |
| 19 | + BlockingQueue.push(pid, "Hi") |
| 20 | + BlockingQueue.pop(pid) # should return "Hi" |
| 17 21 | """ |
| 18 22 | use GenServer |
| 19 23 | |
| @@ -23,9 +27,13 @@ defmodule BlockingQueue do | |
| 23 27 | @doc """ |
| 24 28 | Start a queue process with GenServer.start_link/2. |
| 25 29 | |
| 26 | - `n` Is the maximum queue depth. |
| 30 | + `n` Is the maximum queue depth. Pass the atom `:infinity` to start a queue |
| 31 | + with no maximum. An infinite queue will never block in `push/2` but may |
| 32 | + block in `pop/1` |
| 27 33 | """ |
| 28 | - @spec start_link(pos_integer()) :: on_start |
| 34 | + @type maximum_t :: pos_integer() |
| 35 | + | :infinity |
| 36 | + @spec start_link(maximum_t) :: on_start |
| 29 37 | def start_link(n), do: GenServer.start_link(__MODULE__, n) |
| 30 38 | def init(n), do: {:ok, {n, []}} |
| 31 39 | |
| @@ -89,6 +97,7 @@ defmodule BlockingQueue do | |
| 89 97 | spawn_link(fn -> |
| 90 98 | Enum.each(stream, &push(pid, &1)) |
| 91 99 | end) |
| 100 | + nil |
| 92 101 | end |
| 93 102 | |
| 94 103 | @doc """ |
| @@ -96,8 +105,12 @@ defmodule BlockingQueue do | |
| 96 105 | |
| 97 106 | `pid` is the process ID of the BlockingQueue server. |
| 98 107 | """ |
| 99 | - @spec pop_stream(pid) :: Stream.t |
| 108 | + @spec pop_stream(pid) :: Enumerable.t |
| 100 109 | def pop_stream(pid) do |
| 101 110 | Stream.repeatedly(fn -> BlockingQueue.pop(pid) end) |
| 102 111 | end |
| 112 | + |
| 113 | + def test__ do |
| 114 | + start_link(:infinity) |
| 115 | + end |
| 103 116 | end |
| @@ -3,7 +3,7 @@ defmodule BlockingQueue.Mixfile do | |
| 3 3 | |
| 4 4 | def project do |
| 5 5 | [app: :blocking_queue, |
| 6 | - version: "1.0.3", |
| 6 | + version: "1.0.4", |
| 7 7 | elixir: "~> 1.0", |
| 8 8 | build_embedded: Mix.env == :prod, |
| 9 9 | start_permanent: Mix.env == :prod, |
| @@ -22,7 +22,7 @@ defmodule BlockingQueue.Mixfile do | |
| 22 22 | {:triq, github: "krestenkrab/triq", only: :test}, |
| 23 23 | {:inch_ex, only: :docs}, |
| 24 24 | {:earmark, "~> 0.1", only: :dev}, |
| 25 | - {:ex_doc, "~> 0.7", only: :dev} |
| 25 | + {:ex_doc, "~> 0.10", only: :dev} |
| 26 26 | ] |
| 27 27 | end |