@@ -166,7 +166,7 @@ defmodule GenEvent do
166
166
167
167
Keep in mind though Elixir and Erlang gen events are not 100% compatible.
168
168
The `:gen_event.add_sup_handler/3` is not supported by Elixir's GenEvent,
169
- which in turn supports `monitor: true` in ` GenEvent.add_handler/4 `.
169
+ which in turn supports `GenEvent.add_mon_handler/3 `.
170
170
171
171
The benefits of the monitoring approach are described in the "Don't drink
172
172
too much kool aid" section of the "Learn you some Erlang" link above. Due
@@ -316,11 +316,28 @@ defmodule GenEvent do
316
316
317
317
If the given handler was previously installed at the manager, this
318
318
function returns `{:error, :already_present}`.
319
+ """
320
+ @ spec add_handler ( manager , handler , term , [ monitor: boolean ] ) :: :ok | { :error , term }
321
+ def add_handler ( manager , handler , args , options \\ [ ] ) do
322
+ cond do
323
+ Keyword . get ( options , :monitor , false ) ->
324
+ IO . write :stderr , "warning: the :monitor option in GenEvent.add_handler/4 is deprecated, " <>
325
+ "please use GenEvent.add_mon_handler/3 instead\n #{ Exception . format_stacktrace } "
326
+ rpc ( manager , { :add_mon_handler , handler , args , self ( ) } )
327
+ true ->
328
+ rpc ( manager , { :add_handler , handler , args } )
329
+ end
330
+ end
331
+
332
+ @ doc """
333
+ Adds a monitored event handler to the event `manager`.
334
+
335
+ Expects the same input and returns the same values as `add_handler/3`.
319
336
320
337
## Monitored handlers
321
338
322
- When adding a handler, a `:monitor` option with value `true` can be given.
323
- This means the calling process will now be monitored by the GenEvent handler .
339
+ A monitored handler implies the calling process will now be monitored
340
+ by the GenEvent manager .
324
341
325
342
If the calling process later terminates with `reason`, the event manager
326
343
will delete the event handler by calling the `terminate/2` callback with
@@ -352,14 +369,9 @@ defmodule GenEvent do
352
369
Finally, this functionality only works with GenEvent started via this
353
370
module (it is not backwards compatible with Erlang's `:gen_event`).
354
371
"""
355
- @ spec add_handler ( manager , handler , term , [ monitor: boolean ] ) :: :ok | { :error , term }
356
- def add_handler ( manager , handler , args , options \\ [ ] ) do
357
- cond do
358
- Keyword . get ( options , :monitor , false ) ->
359
- rpc ( manager , { :add_mon_handler , handler , args , self ( ) } )
360
- true ->
361
- rpc ( manager , { :add_handler , handler , args } )
362
- end
372
+ @ spec add_mon_handler ( manager , handler , term ) :: :ok | { :error , term }
373
+ def add_mon_handler ( manager , handler , args ) do
374
+ rpc ( manager , { :add_mon_handler , handler , args , self ( ) } )
363
375
end
364
376
365
377
@ doc """
@@ -474,23 +486,31 @@ defmodule GenEvent do
474
486
is not installed or if the handler fails to terminate with a given reason
475
487
in which case `state = {:error, term}`.
476
488
477
- A `:monitor` option can also be set to specify if the new handler
478
- should be monitored by the manager. See `add_handler/4` for more
479
- information.
480
-
481
489
If `init/1` in the second handler returns a correct value, this
482
490
function returns `:ok`.
483
491
"""
484
492
@ spec swap_handler ( manager , handler , term , handler , term , [ monitor: boolean ] ) :: :ok | { :error , term }
485
493
def swap_handler ( manager , handler1 , args1 , handler2 , args2 , options \\ [ ] ) do
486
494
cond do
487
495
Keyword . get ( options , :monitor , false ) ->
496
+ IO . write :stderr , "warning: the :monitor option in GenEvent.swap_handler/6 is deprecated, " <>
497
+ "please use GenEvent.swap_mon_handler/5 instead\n #{ Exception . format_stacktrace } "
488
498
rpc ( manager , { :swap_mon_handler , handler1 , args1 , handler2 , args2 , self ( ) } )
489
499
true ->
490
500
rpc ( manager , { :swap_handler , handler1 , args1 , handler2 , args2 } )
491
501
end
492
502
end
493
503
504
+ @ doc """
505
+ Replaces an old event handler with a new monitored one in the event `manager`.
506
+
507
+ Read the docs for `add_mon_handler/3` and `swap_handler/5` for more information.
508
+ """
509
+ @ spec swap_mon_handler ( manager , handler , term , handler , term ) :: :ok | { :error , term }
510
+ def swap_mon_handler ( manager , handler1 , args1 , handler2 , args2 ) do
511
+ rpc ( manager , { :swap_mon_handler , handler1 , args1 , handler2 , args2 , self ( ) } )
512
+ end
513
+
494
514
@ doc """
495
515
Returns a list of all event handlers installed in the `manager`.
496
516
"""
0 commit comments