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, opts = {}) ⇒ 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, opts = {}) ⇒ Signal
Initializes the Signal. The signal will be variadic if an empty array is given.
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/qml/reactive/signal.rb', line 25 def initialize(params, opts = {}) opts = {variadic: false}.merge opts @listeners = [] if opts[:variadic] @params = nil @arity = -1 else @params = params.map(&:to_sym) @arity = params.size 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.
73 74 75 76 77 |
# File 'lib/qml/reactive/signal.rb', line 73 def connect(&listener) @listeners << listener connected(listener) Connection.new(self, listener) end |
#connection_count ⇒ Integer
Returns the number of connections.
66 67 68 |
# File 'lib/qml/reactive/signal.rb', line 66 def connection_count @listeners.size end |
#disconnect(listener) ⇒ self
Disconnects a procedure.
84 85 86 87 88 |
# File 'lib/qml/reactive/signal.rb', line 84 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.
40 41 42 43 44 45 46 47 |
# File 'lib/qml/reactive/signal.rb', line 40 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.
92 93 94 |
# File 'lib/qml/reactive/signal.rb', line 92 def map(&block) Signals::MapSignal.new(self, block) end |
#merge(*others) ⇒ QML::Reactive::Signal
Creates a merged signal.
106 107 108 |
# File 'lib/qml/reactive/signal.rb', line 106 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.
54 55 56 |
# File 'lib/qml/reactive/signal.rb', line 54 def parameters @params ? @params.map { |arg| [:req, arg] } : [[:rest, :args]] end |
#select(&block) ⇒ QML::Reactive::Signal
Creates a filtered version of the signal.
100 101 102 |
# File 'lib/qml/reactive/signal.rb', line 100 def select(&block) Signals::SelectSignal.new(self, block) end |
#to_proc ⇒ Proc
Converts the signal into a Proc.
60 61 62 |
# File 'lib/qml/reactive/signal.rb', line 60 def to_proc method(:emit).to_proc end |