class State
Represents the active handlers for a single process signal.
Definitions
def initialize(previous, handlers = {}.compare_by_identity.freeze, ignored = {}.compare_by_identity.freeze)
Initialize the signal state.
Signature
-
parameter
previousObject The signal handler that was installed before this controller took ownership.
-
parameter
handlersHash(Registration, Proc) The active handlers for the signal.
-
parameter
ignoredHash(Registration, Nil) The active ignored signals.
Implementation
def initialize(previous, handlers = {}.compare_by_identity.freeze, ignored = {}.compare_by_identity.freeze)
@previous = previous
@handlers = handlers
@ignored = ignored
end
attr :previous
Signature
-
attribute
Object The signal handler that was installed before this controller took ownership.
attr :handlers
Signature
-
attribute
Hash(Registration, Proc) The active handlers for the signal.
attr :ignored
Signature
-
attribute
Hash(Registration, Nil) The active ignored signals.
def add(registration, handler)
Add a signal handler to this state.
Signature
-
parameter
registrationRegistration The registration that owns the handler.
-
parameter
handlerProc | Nil The handler to add, or
nilto ignore the signal.-
returns
State The updated state.
Implementation
def add(registration, handler)
if handler
handlers = @handlers.dup
handlers[registration] = handler
State.new(@previous, handlers.freeze, @ignored)
else
ignored = @ignored.dup
ignored[registration] = nil
State.new(@previous, @handlers, ignored.freeze)
end
end
def remove(registration)
Remove a signal handler from this state.
Signature
-
parameter
registrationRegistration The registration that owns the handler.
-
returns
State The updated state.
Implementation
def remove(registration)
if @handlers.key?(registration)
handlers = @handlers.dup
handlers.delete(registration)
return State.new(@previous, handlers.freeze, @ignored)
else
ignored = @ignored.dup
ignored.delete(registration)
return State.new(@previous, @handlers, ignored.freeze)
end
end
def empty?
Whether this state has any active handlers.
Signature
-
returns
Boolean True if no handlers or ignored signals are active.
Implementation
def empty?
@handlers.empty? && @ignored.empty?
end
def callbacks
The active callable signal handlers.
Signature
-
returns
Array(Array(Proc, Context)) The active handlers and the contexts that installed them.
Implementation
def callbacks
@handlers.map do |registration, handler|
[handler, registration.context]
end.freeze
end