Signals are used to generate IO.Promises that resolve when a specific signal is received.
A Signal can be in one of 3 states:
- Right after construction it's initial.
- While it is listening for signals it's running.
- If it has stopped for some reason it's finished.
This together with whether it was set up as repeating with Signal.mk determines the behavior
of all functions on Signals.
Instances For
This creates a Signal in the initial state and doesn't start listening yet.
- If
repeatingisfalsethis constructs a signal handler that resolves once when the specified signalsignumis received, then automatically stops listening. - If
repeatingistruethis constructs a signal handler that resolves each time the specified signalsignumis received and continues listening. While it listens, a signal that arrives with no promise fromnextpending resolves the promise of the followingnext. A repeating signal handler will only be freed afterSignal.stopis called, or once nothing references it aftercancel.
This function has different behavior depending on the state and configuration of the Signal:
- if
repeatingisfalseand:- it is initial, start listening and return a new
IO.Promisethat is set to resolve once the signalsignumis received. After thisIO.Promiseis resolved theSignalis finished. - it is running, or finished after receiving the signal, return the same
IO.Promisethat the last call tonextreturned, or a new one ifcanceldropped it. A signal received aftercancelresolves that promise. - it was stopped with
stopbefore receiving the signal, return a newIO.Promisethat is never resolved.
- it is initial, start listening and return a new
- if
repeatingistrueand:- it is initial, start listening and return a new
IO.Promisethat resolves when the next signalsignumis received. - it is running, check whether the last returned
IO.Promiseis already resolved:- If it is, return a new
IO.Promisethat resolves upon receiving the next signal, or that is already resolved if signals arrived while no promise was pending. Those signals are reported once, however many arrived. - If it is not, return the last
IO.PromiseThis ensures that the returnedIO.Promiseresolves for a signal that no earlier promise reported.
- If it is, return a new
- if it is finished, return a new
IO.Promisethat is never resolved, asstopdropped the last one.
- it is initial, start listening and return a new
A promise from next may also be resolved by the code holding it; the handler then treats it as
fulfilled when the signal arrives. The resolved IO.Promise contains the signal number that was
received.
This function has different behavior depending on the state of the Signal:
- If it is initial or finished this is a no-op.
- If it is running the signal handler is stopped, it is put into the finished state and the last
IO.Promisegenerated bynextis released. If that promise is still pending it is never resolved; once nothing else references it, itsresult?resolves tonone.
This function has different behavior depending on the state of the Signal:
- If it is initial or finished this is a no-op.
- If it's running then it drops the last
IO.Promisegenerated bynext. The handler keeps listening, so a signal that arrives afterwards is reported by the next call tonext. Until that call the event loop does not keep theSignalalive, so dropping it stops listening.