@@ -3,35 +3,42 @@ defmodule GenStage.Streamer do
3
3
use GenStage
4
4
5
5
def start_link ( { _ , opts } = pair ) do
6
- GenStage . start_link ( __MODULE__ , pair , opts )
6
+ { :current_stacktrace , [ _info_call | stack ] } = Process . info ( self ( ) , :current_stacktrace )
7
+ GenStage . start_link ( __MODULE__ , { pair , stack } , opts )
7
8
end
8
9
9
- def init ( { stream , opts } ) do
10
+ def init ( { { stream , opts } , stack } ) do
10
11
continuation =
11
12
& Enumerable . reduce ( stream , & 1 , fn
12
13
x , { acc , 1 } -> { :suspend , { [ x | acc ] , 0 } }
13
14
x , { acc , counter } -> { :cont , { [ x | acc ] , counter - 1 } }
14
15
end )
15
16
16
- { :producer , continuation , Keyword . take ( opts , [ :dispatcher , :demand ] ) }
17
+ { :producer , { stack , continuation } , Keyword . take ( opts , [ :dispatcher , :demand ] ) }
17
18
end
18
19
19
- def handle_demand ( _demand , continuation ) when is_atom ( continuation ) do
20
- { :noreply , [ ] , continuation }
20
+ def handle_demand ( _demand , { stack , continuation } ) when is_atom ( continuation ) do
21
+ { :noreply , [ ] , { stack , continuation } }
21
22
end
22
23
23
- def handle_demand ( demand , continuation ) when demand > 0 do
24
+ def handle_demand ( demand , { stack , continuation } ) when demand > 0 do
24
25
case continuation . ( { :cont , { [ ] , demand } } ) do
25
26
{ :suspended , { list , 0 } , continuation } ->
26
- { :noreply , :lists . reverse ( list ) , continuation }
27
+ { :noreply , :lists . reverse ( list ) , { stack , continuation } }
27
28
28
29
{ status , { list , _ } } ->
29
30
GenStage . async_info ( self ( ) , :stop )
30
- { :noreply , :lists . reverse ( list ) , status }
31
+ { :noreply , :lists . reverse ( list ) , { stack , status } }
31
32
end
32
33
end
33
34
34
35
def handle_info ( :stop , state ) do
35
36
{ :stop , :normal , state }
36
37
end
38
+
39
+ def handle_info ( msg , { stack , continuation } ) do
40
+ log = '** Undefined handle_info in ~tp~n** Unhandled message: ~tp~n** Stream started at:~n~ts'
41
+ :error_logger . warning_msg ( log , [ inspect ( __MODULE__ ) , msg , Exception . format_stacktrace ( stack ) ] )
42
+ { :noreply , [ ] , { stack , continuation } }
43
+ end
37
44
end
0 commit comments