Class: KO::Signals::Signal

Inherits:
Object
  • Object
show all
Defined in:
lib/ko/signals/signal.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, arg_types) ⇒ Signal

Returns a new instance of Signal.



8
9
10
11
12
13
14
# File 'lib/ko/signals/signal.rb', line 8

def initialize(name, arg_types)
  @name = name
  @arg_types = arg_types

  @validator = Validator.new(self)
  @connections = {}
end

Instance Attribute Details

#arg_typesObject (readonly)

Returns the value of attribute arg_types.



6
7
8
# File 'lib/ko/signals/signal.rb', line 6

def arg_types
  @arg_types
end

#connectionsObject (readonly)

Returns the value of attribute connections.



6
7
8
# File 'lib/ko/signals/signal.rb', line 6

def connections
  @connections
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/ko/signals/signal.rb', line 6

def name
  @name
end

#parentObject

Returns the value of attribute parent.



6
7
8
# File 'lib/ko/signals/signal.rb', line 6

def parent
  @parent
end

Instance Method Details

#callObject



49
# File 'lib/ko/signals/signal.rb', line 49

def call(...) = emit(...)

#connect(receiver = nil, mode: :direct, one_shot: false, &block) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/ko/signals/signal.rb', line 27

def connect(receiver = nil, mode: :direct, one_shot: false, &block)
  receiver = normalize_receiver(receiver) || block
  @validator.validate_receiver!(receiver)

  raise "ALREADY CONNECTED" if @connections.include?(receiver)

  Connection.new(receiver, self, mode:, one_shot:).tap { @connections[receiver] = _1 }
end

#disconnect(receiver) ⇒ Object

Raises:

  • (ArgumentError)


36
37
38
39
# File 'lib/ko/signals/signal.rb', line 36

def disconnect(receiver)
  receiver = normalize_receiver(receiver)
  raise ArgumentError, "given receiver is not connected to this signal" if @connections.delete(receiver).nil?
end

#dupObject



23
# File 'lib/ko/signals/signal.rb', line 23

def dup = self.class.new(name, arg_types)

#emit(*args) ⇒ Object



41
42
43
44
45
# File 'lib/ko/signals/signal.rb', line 41

def emit(*args)
  @validator.validate_args!(args)

  notify_subscribers(args)
end

#inspectObject



47
# File 'lib/ko/signals/signal.rb', line 47

def inspect = "#<#{self.class}@#{object_id}[#{name.inspect}] connections=#{@connections.count}>"

#receiver_nameObject



25
# File 'lib/ko/signals/signal.rb', line 25

def receiver_name = :"on_#{name}"