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, opts = {}) ⇒ Signal

Initializes the Signal. The signal will be variadic if an empty array is given.

Parameters:

  • params (Array<#to_sym>, Array<()>)

    The parameter names.

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • (false) (Boolean)

    :variadic



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

#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:



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_countInteger

Returns the number of connections.

Returns:

  • (Integer)


66
67
68
# File 'lib/qml/reactive/signal.rb', line 66

def connection_count
  @listeners.size
end

#disconnect(listener) ⇒ self

Disconnects a procedure.

Parameters:

  • listener

Returns:

  • (self)


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.

Parameters:

  • args

    the arguments.



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

#parametersArray<Array<Symbol>>

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

Returns:

  • (Array<Array<Symbol>>)


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

#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)


60
61
62
# File 'lib/qml/reactive/signal.rb', line 60

def to_proc
  method(:emit).to_proc
end