Skip to content

Commit 674c1dc

Browse files
Address PR feedback
1 parent c48f7a8 commit 674c1dc

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
@@ -1199,10 +1199,12 @@ defmodule GenStage do
11991199
when new_state: term, event: term
12001200

12011201
@doc """
1202-
Invoked to handle `continue` instructions.
1202+
Invoked to handle `:continue` instructions.
12031203
1204-
It is useful for performing work after initialization or for splitting the work
1205-
in a callback in multiple steps, updating the process state along the way.
1204+
This callback can be used to perform work right after emitting events from
1205+
other callbacks. The "continue mechanism" makes sure that no messages,
1206+
calls, casts, or anything else will be handled between a callback emitting
1207+
a `:continue` tuple and the `c:handle_continue/2` callback being invoked.
12061208
12071209
Return values are the same as `c:handle_cast/2`.
12081210
@@ -1852,56 +1854,29 @@ defmodule GenStage do
18521854
{:producer, state} ->
18531855
init_producer(mod, [], state, nil)
18541856

1855-
{:producer, state, {:continue, _term} = continue} ->
1856-
init_producer(mod, [], state, continue)
1857-
1858-
{:producer, state, :hibernate} ->
1859-
init_producer(mod, [], state, :hibernate)
1860-
18611857
{:producer, state, opts} when is_list(opts) ->
18621858
init_producer(mod, opts, state, nil)
18631859

1864-
{:producer, state, {:continue, _term} = continue, opts} when is_list(opts) ->
1865-
init_producer(mod, opts, state, continue)
1866-
1867-
{:producer, state, :hibernate, opts} when is_list(opts) ->
1868-
init_producer(mod, opts, state, :hibernate)
1860+
{:producer, state, opts, continue_or_hibernate} when is_list(opts) ->
1861+
init_producer(mod, opts, state, continue_or_hibernate)
18691862

18701863
{:producer_consumer, state} ->
18711864
init_producer_consumer(mod, [], state, nil)
18721865

1873-
{:producer_consumer, state, {:continue, _term} = continue} ->
1874-
init_producer_consumer(mod, [], state, continue)
1875-
1876-
{:producer_consumer, state, :hibernate} ->
1877-
init_producer_consumer(mod, [], state, :hibernate)
1878-
18791866
{:producer_consumer, state, opts} when is_list(opts) ->
18801867
init_producer_consumer(mod, opts, state, nil)
18811868

1882-
{:producer_consumer, state, {:continue, _term} = continue, opts} when is_list(opts) ->
1883-
init_producer_consumer(mod, opts, state, continue)
1884-
1885-
{:producer_consumer, state, :hibernate, opts} when is_list(opts) ->
1886-
init_producer_consumer(mod, opts, state, :hibernate)
1869+
{:producer_consumer, state, opts, continue_or_hibernate} when is_list(opts) ->
1870+
init_producer_consumer(mod, opts, state, continue_or_hibernate)
18871871

18881872
{:consumer, state} ->
18891873
init_consumer(mod, [], state, nil)
18901874

1891-
{:consumer, state, {:continue, _term} = continue} ->
1892-
init_consumer(mod, [], state, continue)
1893-
1894-
{:consumer, state, :hibernate} ->
1895-
init_consumer(mod, [], state, :hibernate)
1896-
18971875
{:consumer, state, opts} when is_list(opts) ->
18981876
init_consumer(mod, opts, state, nil)
18991877

1900-
{:consumer, state, {:continue, _term} = continue, opts} when is_list(opts) ->
1901-
init_consumer(mod, opts, state, continue)
1902-
1903-
{:consumer, state, :hibernate, opts} when is_list(opts) ->
1904-
init_consumer(mod, opts, state, :hibernate)
1878+
{:consumer, state, opts, continue_or_hibernate} when is_list(opts) ->
1879+
init_consumer(mod, opts, state, continue_or_hibernate)
19051880

19061881
{:stop, _} = stop ->
19071882
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)