Class: QML::Reactive::Signal

Inherits:
Object
  • Object
show all
Defined in:
lib/qml/reactive/signal.rb,
lib/qml/reactive/signal_spy.rb

Direct Known Subclasses

QtSignal, ChainedSignal

Defined Under Namespace

Classes: Connection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Signal

Initializes the Signal.

Parameters:

  • params (Array<#to_sym>, nil)

    the parameter names (the signal will be variadic if nil).



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

#arityObject (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.

Yields:

  • called when #emit is called.

Returns:



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_countInteger

Returns the number of connections.

Returns:

  • (Integer)


62
63
64
# File 'lib/qml/reactive/signal.rb', line 62

def connection_count
  @listeners.size
end

#disconnect(listener) ⇒ self

Disconnects a procedure.

Parameters:

  • listener

Returns:

  • (self)


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.

Parameters:

  • args

    the arguments.



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

#parametersArray<Array<Symbol>>

Returns the format of the parameters in the same format as Proc#parameters.

Returns:

  • (Array<Array<Symbol>>)


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

#spyObject



5
6
7
# File 'lib/qml/reactive/signal_spy.rb', line 5

def spy
  SignalSpy.new(self)
end

#to_procProc

Converts the signal into a Proc.

Returns:

  • (Proc)


56
57
58
# File 'lib/qml/reactive/signal.rb', line 56

def to_proc
  method(:emit).to_proc
end