Skip to content

Commit ca7c351

Browse files
Address PR feedback
1 parent 8f1e4ca commit ca7c351

File tree

2 files changed

+22
-46
lines changed

2 files changed

+22
-46
lines changed

lib/gen_stage.ex

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,10 +1121,12 @@ defmodule GenStage do
11211121
when new_state: term, event: term
11221122

11231123
@doc """
1124-
Invoked to handle `continue` instructions.
1124+
Invoked to handle `:continue` instructions.
11251125
1126-
It is useful for performing work after initialization or for splitting the work
1127-
in a callback in multiple steps, updating the process state along the way.
1126+
This callback can be used to perform work right after emitting events from
1127+
other callbacks. The "continue mechanism" makes sure that no messages,
1128+
calls, casts, or anything else will be handled between a callback emitting
1129+
a `:continue` tuple and the `c:handle_continue/2` callback being invoked.
11281130
11291131
Return values are the same as `c:handle_cast/2`.
11301132
@@ -1759,56 +1761,29 @@ defmodule GenStage do
17591761
{:producer, state} ->
17601762
init_producer(mod, [], state, nil)
17611763

1762-
{:producer, state, {:continue, _term} = continue} ->
1763-
init_producer(mod, [], state, continue)
1764-
1765-
{:producer, state, :hibernate} ->
1766-
init_producer(mod, [], state, :hibernate)
1767-
17681764
{:producer, state, opts} when is_list(opts) ->
17691765
init_producer(mod, opts, state, nil)
17701766

1771-
{:producer, state, {:continue, _term} = continue, opts} when is_list(opts) ->
1772-
init_producer(mod, opts, state, continue)
1773-
1774-
{:producer, state, :hibernate, opts} when is_list(opts) ->
1775-
init_producer(mod, opts, state, :hibernate)
1767+
{:producer, state, opts, continue_or_hibernate} when is_list(opts) ->
1768+
init_producer(mod, opts, state, continue_or_hibernate)
17761769

17771770
{:producer_consumer, state} ->
17781771
init_producer_consumer(mod, [], state, nil)
17791772

1780-
{:producer_consumer, state, {:continue, _term} = continue} ->
1781-
init_producer_consumer(mod, [], state, continue)
1782-
1783-
{:producer_consumer, state, :hibernate} ->
1784-
init_producer_consumer(mod, [], state, :hibernate)
1785-
17861773
{:producer_consumer, state, opts} when is_list(opts) ->
17871774
init_producer_consumer(mod, opts, state, nil)
17881775

1789-
{:producer_consumer, state, {:continue, _term} = continue, opts} when is_list(opts) ->
1790-
init_producer_consumer(mod, opts, state, continue)
1791-
1792-
{:producer_consumer, state, :hibernate, opts} when is_list(opts) ->
1793-
init_producer_consumer(mod, opts, state, :hibernate)
1776+
{:producer_consumer, state, opts, continue_or_hibernate} when is_list(opts) ->
1777+
init_producer_consumer(mod, opts, state, continue_or_hibernate)
17941778

17951779
{:consumer, state} ->
17961780
init_consumer(mod, [], state, nil)
17971781

1798-
{:consumer, state, {:continue, _term} = continue} ->
1799-
init_consumer(mod, [], state, continue)
1800-
1801-
{:consumer, state, :hibernate} ->
1802-
init_consumer(mod, [], state, :hibernate)
1803-
18041782
{:consumer, state, opts} when is_list(opts) ->
18051783
init_consumer(mod, opts, state, nil)
18061784

1807-
{:consumer, state, {:continue, _term} = continue, opts} when is_list(opts) ->
1808-
init_consumer(mod, opts, state, continue)
1809-
1810-
{:consumer, state, :hibernate, opts} when is_list(opts) ->
1811-
init_consumer(mod, opts, state, :hibernate)
1785+
{:consumer, state, opts, continue_or_hibernate} when is_list(opts) ->
1786+
init_consumer(mod, opts, state, continue_or_hibernate)
18121787

18131788
{:stop, _} = stop ->
18141789
stop

test/gen_stage_test.exs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ defmodule GenStageTest do
442442
if otp_version >= 21 do
443443
describe "handle_continue tests" do
444444
test "producing_init with continue instruction setting counter start position" do
445-
{:ok, producer} = Counter.start_link({:producer, 0, {:continue, 500}})
445+
{:ok, producer} = Counter.start_link({:producer, 0, [], {:continue, 500}})
446446
{:ok, _} = Forwarder.start_link({:consumer, self(), subscribe_to: [producer]})
447447

448448
batch = Enum.to_list(0..499)
@@ -454,7 +454,7 @@ defmodule GenStageTest do
454454
end
455455

456456
test "producer_init with nested continue instruction setting counter start position" do
457-
{:ok, producer} = CounterNestedContinue.start_link({:producer, 0, {:continue, 500}})
457+
{:ok, producer} = CounterNestedContinue.start_link({:producer, 0, [], {:continue, 500}})
458458
{:ok, _} = Forwarder.start_link({:consumer, self(), subscribe_to: [producer]})
459459

460460
# The nested continue sets the counter to 2000
@@ -471,11 +471,11 @@ defmodule GenStageTest do
471471
end
472472

473473
test "consumer_init with continue instruction" do
474-
{:ok, producer} = Counter.start_link({:producer, 0, {:continue, 500}})
474+
{:ok, producer} = Counter.start_link({:producer, 0, [], {:continue, 500}})
475475

476476
{:ok, _} =
477477
Forwarder.start_link(
478-
{:consumer, self(), {:continue, :continue_reached}, subscribe_to: [producer]}
478+
{:consumer, self(), [subscribe_to: [producer]], {:continue, :continue_reached}}
479479
)
480480

481481
assert_receive :continue_reached
@@ -486,8 +486,9 @@ defmodule GenStageTest do
486486

487487
{:ok, _doubler} =
488488
Doubler.start_link(
489-
{:producer_consumer, self(), {:continue, :continue_reached},
490-
subscribe_to: [{producer, max_demand: 100, min_demand: 80}]}
489+
{:producer_consumer, self(),
490+
[subscribe_to: [{producer, max_demand: 100, min_demand: 80}]],
491+
{:continue, :continue_reached}}
491492
)
492493

493494
assert_receive :continue_reached
@@ -497,7 +498,7 @@ defmodule GenStageTest do
497498

498499
describe "hibernate tests" do
499500
test "producer_init with hibernate instruction" do
500-
{:ok, producer} = Counter.start_link({:producer, 0, :hibernate})
501+
{:ok, producer} = Counter.start_link({:producer, 0, [], :hibernate})
501502

502503
assert :erlang.process_info(producer, :current_function) ==
503504
{:current_function, {:erlang, :hibernate, 3}}
@@ -507,7 +508,7 @@ defmodule GenStageTest do
507508
{:ok, producer} = Counter.start_link({:producer, 0})
508509

509510
{:ok, consumer} =
510-
Forwarder.start_link({:consumer, self(), :hibernate, subscribe_to: [producer]})
511+
Forwarder.start_link({:consumer, self(), [subscribe_to: [producer]], :hibernate})
511512

512513
assert :erlang.process_info(consumer, :current_function) ==
513514
{:current_function, {:erlang, :hibernate, 3}}
@@ -518,8 +519,8 @@ defmodule GenStageTest do
518519

519520
{:ok, doubler} =
520521
Doubler.start_link(
521-
{:producer_consumer, self(), :hibernate,
522-
subscribe_to: [{producer, max_demand: 100, min_demand: 80}]}
522+
{:producer_consumer, self(),
523+
[subscribe_to: [{producer, max_demand: 100, min_demand: 80}]], :hibernate}
523524
)
524525

525526
assert :erlang.process_info(doubler, :current_function) ==

0 commit comments

Comments
 (0)