Class: QML::Reactive::Signal
- Inherits:
-
Object
- Object
- QML::Reactive::Signal
- Defined in:
- lib/qml/reactive/signal.rb,
lib/qml/reactive/signal_spy.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Connection
Instance Attribute Summary collapse
-
#arity ⇒ Object
readonly
Returns the value of attribute arity.
Instance Method Summary collapse
-
#connect { ... } ⇒ QML::Reactive::Signal::Connection
(also: #each)
Connects a procedure.
-
#connection_count ⇒ Integer
Returns the number of connections.
-
#disconnect(listener) ⇒ self
Disconnects a procedure.
-
#emit(*args) ⇒ Object
(also: #call, #[])
Calls every connected procedure with given arguments.
-
#initialize(params) ⇒ Signal
constructor
Initializes the Signal.
-
#map(&block) ⇒ QML::Reactive::Signal
(also: #collect)
Creates a transformed version of the signal.
-
#merge(*others) ⇒ QML::Reactive::Signal
Creates a merged signal.
-
#parameters ⇒ Array<Array<Symbol>>
Returns the format of the parameters in the same format as Proc#parameters.
-
#select(&block) ⇒ QML::Reactive::Signal
Creates a filtered version of the signal.
- #spy ⇒ Object
-
#to_proc ⇒ Proc
Converts the signal into a Proc.
Constructor Details
#initialize(params) ⇒ Signal
Initializes the Signal.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/qml/reactive/signal.rb', line 22 def initialize(params) @listeners = [] if params @params = params.map(&:to_sym) @arity = params.size else @params = nil @arity = -1 end end |
Instance Attribute Details
#arity ⇒ Object (readonly)
Returns the value of attribute arity.
18 19 20 |
# File 'lib/qml/reactive/signal.rb', line 18 def arity @arity end |
Instance Method Details
#connect { ... } ⇒ QML::Reactive::Signal::Connection Also known as: each
Connects a procedure.
69 70 71 72 73 |
# File 'lib/qml/reactive/signal.rb', line 69 def connect(&listener) @listeners << listener connected(listener) Connection.new(self, listener) end |
#connection_count ⇒ Integer
Returns the number of connections.
62 63 64 |
# File 'lib/qml/reactive/signal.rb', line 62 def connection_count @listeners.size end |
#disconnect(listener) ⇒ self
Disconnects a procedure.
80 81 82 83 84 |
# File 'lib/qml/reactive/signal.rb', line 80 def disconnect(listener) @listeners.delete(listener) disconnected(listener) self end |
#emit(*args) ⇒ Object Also known as: call, []
Calls every connected procedure with given arguments. Raises an ArgumentError when the arity is wrong.
36 37 38 39 40 41 42 43 |
# File 'lib/qml/reactive/signal.rb', line 36 def emit(*args) if @arity >= 0 && args.size != @arity fail ::ArgumentError ,"wrong number of arguments for signal (#{args.size} for #{@arity})" end @listeners.each do |listener| listener.call(*args) end end |
#map(&block) ⇒ QML::Reactive::Signal Also known as: collect
Creates a transformed version of the signal.
88 89 90 |
# File 'lib/qml/reactive/signal.rb', line 88 def map(&block) Signals::MapSignal.new(self, block) end |
#merge(*others) ⇒ QML::Reactive::Signal
Creates a merged signal.
102 103 104 |
# File 'lib/qml/reactive/signal.rb', line 102 def merge(*others) Signals::MergeSignal.new([self] + others) end |
#parameters ⇒ Array<Array<Symbol>>
Returns the format of the parameters in the same format as Proc#parameters.
50 51 52 |
# File 'lib/qml/reactive/signal.rb', line 50 def parameters @params ? @params.map { |arg| [:req, arg] } : [[:rest, :args]] end |
#select(&block) ⇒ QML::Reactive::Signal
Creates a filtered version of the signal.
96 97 98 |
# File 'lib/qml/reactive/signal.rb', line 96 def select(&block) Signals::SelectSignal.new(self, block) end |
#to_proc ⇒ Proc
Converts the signal into a Proc.
56 57 58 |
# File 'lib/qml/reactive/signal.rb', line 56 def to_proc method(:emit).to_proc end |