@@ -858,8 +858,9 @@ defmodule GenStage do
858
858
end
859
859
860
860
The returned tuple may also contain 3 or 4 elements. The third
861
- element may be the `:hibernate` atom or a set of options defined
862
- below.
861
+ element may be a set of options defined below. The fourth element
862
+ is a timeout, the `:hibernate` atom or a `:continue` tuple. See
863
+ the return values for `c:GenServer.init/1` for more information.
863
864
864
865
Returning `:ignore` will cause `start_link/3` to return `:ignore`
865
866
and the process will exit normally without entering the loop or
@@ -910,14 +911,14 @@ defmodule GenStage do
910
911
@ callback init ( args :: term ) ::
911
912
{ :producer , state }
912
913
| { :producer , state , [ producer_option ] }
913
- | { :producer , state , [ producer_option ] , { :continue , term } | :hibernate }
914
+ | { :producer , state , [ producer_option ] , timeout ( ) | { :continue , term } | :hibernate }
914
915
| { :producer_consumer , state }
915
916
| { :producer_consumer , state , [ producer_consumer_option ] }
916
917
| { :producer_consumer , state , [ producer_consumer_option ] ,
917
- { :continue , term } | :hibernate }
918
+ timeout ( ) | { :continue , term } | :hibernate }
918
919
| { :consumer , state }
919
920
| { :consumer , state , [ consumer_option ] }
920
- | { :consumer , state , [ consumer_option ] , { :continue , term } | :hibernate }
921
+ | { :consumer , state , [ consumer_option ] , timeout ( ) | { :continue , term } | :hibernate }
921
922
| :ignore
922
923
| { :stop , reason :: any }
923
924
when state: any
@@ -999,6 +1000,7 @@ defmodule GenStage do
999
1000
"""
1000
1001
@ callback handle_demand ( demand :: pos_integer , state :: term ) ::
1001
1002
{ :noreply , [ event ] , new_state }
1003
+ | { :noreply , [ event ] , new_state , timeout ( ) }
1002
1004
| { :noreply , [ event ] , new_state , :hibernate }
1003
1005
| { :noreply , [ event ] , new_state , { :continue , term } }
1004
1006
| { :stop , reason , new_state }
@@ -1079,6 +1081,7 @@ defmodule GenStage do
1079
1081
state :: term
1080
1082
) ::
1081
1083
{ :noreply , [ event ] , new_state }
1084
+ | { :noreply , [ event ] , new_state , timeout ( ) }
1082
1085
| { :noreply , [ event ] , new_state , :hibernate }
1083
1086
| { :noreply , [ event ] , new_state , { :continue , term } }
1084
1087
| { :stop , reason , new_state }
@@ -1093,6 +1096,7 @@ defmodule GenStage do
1093
1096
"""
1094
1097
@ callback handle_events ( events :: [ event ] , from , state :: term ) ::
1095
1098
{ :noreply , [ event ] , new_state }
1099
+ | { :noreply , [ event ] , new_state , timeout ( ) }
1096
1100
| { :noreply , [ event ] , new_state , :hibernate }
1097
1101
| { :noreply , [ event ] , new_state , { :continue , term } }
1098
1102
| { :stop , reason , new_state }
@@ -1135,9 +1139,11 @@ defmodule GenStage do
1135
1139
"""
1136
1140
@ callback handle_call ( request :: term , from :: GenServer . from ( ) , state :: term ) ::
1137
1141
{ :reply , reply , [ event ] , new_state }
1142
+ | { :reply , reply , [ event ] , new_state , timeout ( ) }
1138
1143
| { :reply , reply , [ event ] , new_state , :hibernate }
1139
1144
| { :reply , reply , [ event ] , new_state , { :continue , term } }
1140
1145
| { :noreply , [ event ] , new_state }
1146
+ | { :noreply , [ event ] , new_state , timeout ( ) }
1141
1147
| { :noreply , [ event ] , new_state , :hibernate }
1142
1148
| { :noreply , [ event ] , new_state , { :continue , term } }
1143
1149
| { :stop , reason , reply , new_state }
@@ -1154,12 +1160,21 @@ defmodule GenStage do
1154
1160
the loop with new state `new_state`. Only `:producer` and `:producer_consumer`
1155
1161
stages can return a non-empty list of events.
1156
1162
1163
+ Returning `{:noreply, [event], state, timeout}` is similar to `{:noreply, state}`
1164
+ , except that it also sets a timeout. See the "Timeouts" section in the
1165
+ `GenServer` documentation for more information.
1166
+
1157
1167
Returning `{:noreply, [event], new_state, :hibernate}` is similar to
1158
1168
`{:noreply, new_state}` except the process is hibernated before continuing the
1159
1169
loop. See the return values for `c:GenServer.handle_call/3` for more information
1160
1170
on hibernation. Only `:producer` and `:producer_consumer` stages can return a
1161
1171
non-empty list of events.
1162
1172
1173
+ Returning `{:noreply, [event], new_state, {:continue, continue_arg}}` is similar
1174
+ to `{:noreply, new_state}` except that immediately after entering the loop, the
1175
+ `c:handle_continue/2` callback will be invoked with `continue_arg` as the first
1176
+ argument and `state` as the second one.
1177
+
1163
1178
Returning `{:stop, reason, new_state}` stops the loop and `terminate/2` is
1164
1179
called with the reason `reason` and state `new_state`. The process exits with
1165
1180
reason `reason`.
@@ -1169,6 +1184,7 @@ defmodule GenStage do
1169
1184
"""
1170
1185
@ callback handle_cast ( request :: term , state :: term ) ::
1171
1186
{ :noreply , [ event ] , new_state }
1187
+ | { :noreply , [ event ] , new_state , timeout ( ) }
1172
1188
| { :noreply , [ event ] , new_state , :hibernate }
1173
1189
| { :noreply , [ event ] , new_state , { :continue , term } }
1174
1190
| { :stop , reason :: term , new_state }
@@ -1190,6 +1206,7 @@ defmodule GenStage do
1190
1206
"""
1191
1207
@ callback handle_info ( message :: term , state :: term ) ::
1192
1208
{ :noreply , [ event ] , new_state }
1209
+ | { :noreply , [ event ] , new_state , timeout ( ) }
1193
1210
| { :noreply , [ event ] , new_state , :hibernate }
1194
1211
| { :noreply , [ event ] , new_state , { :continue , term } }
1195
1212
| { :stop , reason :: term , new_state }
@@ -1212,6 +1229,7 @@ defmodule GenStage do
1212
1229
"""
1213
1230
@ callback handle_continue ( continue :: term , state :: term ) ::
1214
1231
{ :noreply , [ event ] , new_state }
1232
+ | { :noreply , [ event ] , new_state , timeout ( ) }
1215
1233
| { :noreply , [ event ] , new_state , :hibernate }
1216
1234
| { :noreply , [ event ] , new_state , { :continue , term } }
1217
1235
| { :stop , reason :: term , new_state }
@@ -1995,12 +2013,20 @@ defmodule GenStage do
1995
2013
{ :noreply , stage , { :continue , _term } = continue } ->
1996
2014
{ :ok , stage , continue }
1997
2015
2016
+ { :noreply , stage , timeout } ->
2017
+ { :ok , stage , timeout }
2018
+
1998
2019
{ :stop , reason , stage } ->
1999
2020
{ :stop , reason , stage }
2000
2021
end
2001
2022
end
2002
2023
2003
2024
defp handle_gen_server_init_args ( :hibernate , stage ) , do: { :ok , stage , :hibernate }
2025
+
2026
+ defp handle_gen_server_init_args ( timeout , stage )
2027
+ when ( is_integer ( timeout ) and timeout >= 0 ) or timeout == :infinity ,
2028
+ do: { :ok , stage , timeout }
2029
+
2004
2030
defp handle_gen_server_init_args ( nil , stage ) , do: { :ok , stage }
2005
2031
2006
2032
@ doc false
@@ -2041,6 +2067,10 @@ defmodule GenStage do
2041
2067
stage = dispatch_events ( events , length ( events ) , % { stage | state: state } )
2042
2068
{ :reply , reply , stage , continue }
2043
2069
2070
+ { :reply , reply , events , state , timeout } ->
2071
+ stage = dispatch_events ( events , length ( events ) , % { stage | state: state } )
2072
+ { :reply , reply , stage , timeout }
2073
+
2044
2074
{ :stop , reason , reply , state } ->
2045
2075
{ :stop , reason , reply , % { stage | state: state } }
2046
2076
@@ -2338,6 +2368,10 @@ defmodule GenStage do
2338
2368
stage = dispatch_events ( events , length ( events ) , % { stage | state: state } )
2339
2369
{ :noreply , stage , continue }
2340
2370
2371
+ { :noreply , events , state , timeout } when is_list ( events ) ->
2372
+ stage = dispatch_events ( events , length ( events ) , % { stage | state: state } )
2373
+ { :noreply , stage , timeout }
2374
+
2341
2375
{ :stop , reason , state } ->
2342
2376
{ :stop , reason , % { stage | state: state } }
2343
2377
@@ -2699,6 +2733,11 @@ defmodule GenStage do
2699
2733
ask ( from , ask , [ :noconnect ] )
2700
2734
consumer_dispatch ( batches , from , mod , state , stage , continue )
2701
2735
2736
+ { :noreply , events , state , timeout } ->
2737
+ stage = dispatch_events ( events , length ( events ) , stage )
2738
+ ask ( from , ask , [ :noconnect ] )
2739
+ consumer_dispatch ( batches , from , mod , state , stage , timeout )
2740
+
2702
2741
{ :stop , reason , state } ->
2703
2742
{ :stop , reason , % { stage | state: state } }
2704
2743
0 commit comments