Class: MotionWiretap::Wiretap

Inherits:
Object
  • Object
show all
Defined in:
lib/motion-wiretap/all/wiretap.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Wiretap

Returns a new instance of Wiretap.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/motion-wiretap/all/wiretap.rb', line 5

def initialize(&block)
  @is_torn_down = false
  @is_completed = false
  @is_error = false
  @queue = nil

  @listener_handlers = []
  @completion_handlers = []
  @error_handlers = []

  listen &block if block
end

Instance Method Details

#and_then(wiretap = nil, &block) ⇒ Object

called when no more values are expected



84
85
86
87
88
89
90
91
92
# File 'lib/motion-wiretap/all/wiretap.rb', line 84

def and_then(wiretap=nil, &block)
  raise "Block or Wiretap is expected in #{self.class.name}##{__method__}" unless block || wiretap
  raise "Only Block *or* Wiretap is expected in #{self.class.name}##{__method__}" if block && wiretap
  @completion_handlers << (block ? block.weak! : wiretap)
  if @is_completed
    trigger_completed_on(block || wiretap)
  end
  return self
end

#cancel!Object

this is the preferred way to turn off a wiretap; child classes override ‘teardown`, which is only ever called once.



25
26
27
28
29
# File 'lib/motion-wiretap/all/wiretap.rb', line 25

def cancel!
  return if @is_torn_down
  @is_torn_down = true
  teardown
end

#combine(&block) ⇒ Object

Returns a Wiretap that combines all the values into one value (the values are all passed in at the same time)

Examples:

wiretap.combine do |one, two|
  one ? two : nil
end


166
167
168
# File 'lib/motion-wiretap/all/wiretap.rb', line 166

def combine(&block)
  return WiretapCombiner.new(self, block)
end

#deallocObject



18
19
20
21
# File 'lib/motion-wiretap/all/wiretap.rb', line 18

def dealloc
  self.cancel!
  super
end

#enqueue(&block) ⇒ Object

send a block to the GCD queue



43
44
45
46
47
48
49
# File 'lib/motion-wiretap/all/wiretap.rb', line 43

def enqueue(&block)
  if @queue
    @queue.async(&block)
  else
    block.call
  end
end

#filter(&block) ⇒ Object

Returns a Wiretap that will only be called if the &condition block returns true



156
157
158
# File 'lib/motion-wiretap/all/wiretap.rb', line 156

def filter(&block)
  return WiretapFilter.new(self, block)
end

#listen(wiretap = nil, &block) ⇒ Object

called when the value changes



56
57
58
59
60
61
# File 'lib/motion-wiretap/all/wiretap.rb', line 56

def listen(wiretap=nil, &block)
  raise "Block or Wiretap is expected in #{self.class.name}##{__method__}" unless block || wiretap
  raise "Only Block *or* Wiretap is expected in #{self.class.name}##{__method__}" if block && wiretap
  @listener_handlers << (block ? block.weak! : wiretap)
  self
end

#map(&block) ⇒ Object

Returns a Wiretap that passes each value through the provided block

Examples:

wiretap.map do |item|
  item.to_s
end


186
187
188
# File 'lib/motion-wiretap/all/wiretap.rb', line 186

def map(&block)
  return WiretapMapper.new(self, block)
end

#on_error(wiretap = nil, &block) ⇒ Object

called when an error occurs, and no more values are expected



117
118
119
120
121
122
123
124
125
# File 'lib/motion-wiretap/all/wiretap.rb', line 117

def on_error(wiretap=nil, &block)
  raise "Block or Wiretap is expected in #{self.class.name}##{__method__}" unless block || wiretap
  raise "Only Block *or* Wiretap is expected in #{self.class.name}##{__method__}" if block && wiretap
  @error_handlers << (block ? block.weak! : wiretap)
  if @is_error
    trigger_error_on(block || wiretap, @is_error)
  end
  return self
end

#queue(queue) ⇒ Object

specify the GCD queue that the listeners should be run on



37
38
39
40
# File 'lib/motion-wiretap/all/wiretap.rb', line 37

def queue(queue)
  @queue = queue
  return self
end

#reduce(memo = nil, &block) ⇒ Object

Returns a Wiretap that passes each value through the block, and also the previous return value (memo).

Examples:

# returns the total of all the prices
wiretap.reduce(0) do |memo, item|
  memo + item.price
end


177
178
179
# File 'lib/motion-wiretap/all/wiretap.rb', line 177

def reduce(memo=nil, &block)
  return WiretapReducer.new(self, memo, block)
end

#teardownObject

Overridden by subclasses to turn off observation, unregister notifications, etc.



33
34
# File 'lib/motion-wiretap/all/wiretap.rb', line 33

def teardown
end

#trigger_changed(*values) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/motion-wiretap/all/wiretap.rb', line 63

def trigger_changed(*values)
  return if @is_torn_down || @is_completed || @is_error

  @listener_handlers.each do |block_or_wiretap|
    trigger_changed_on(block_or_wiretap, values)
  end

  return self
end

#trigger_changed_on(block_or_wiretap, values) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/motion-wiretap/all/wiretap.rb', line 73

def trigger_changed_on(block_or_wiretap, values)
  if block_or_wiretap.is_a? Wiretap
    block_or_wiretap.trigger_changed(*values)
  else
    enqueue do
      block_or_wiretap.call(*values)
    end
  end
end

#trigger_completedObject



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/motion-wiretap/all/wiretap.rb', line 94

def trigger_completed
  return if @is_torn_down || @is_completed || @is_error
  @is_completed = true

  @completion_handlers.each do |block_or_wiretap|
    trigger_completed_on(block_or_wiretap)
  end

  cancel!
  return self
end

#trigger_completed_on(block_or_wiretap) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/motion-wiretap/all/wiretap.rb', line 106

def trigger_completed_on(block_or_wiretap)
  if block_or_wiretap.is_a? Wiretap
    block_or_wiretap.trigger_completed
  else
    enqueue do
      block_or_wiretap.call
    end
  end
end

#trigger_error(error) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/motion-wiretap/all/wiretap.rb', line 127

def trigger_error(error)
  return if @is_torn_down || @is_completed || @is_error
  error ||= true
  @is_error = error

  @error_handlers.each do |block_or_wiretap|
    trigger_error_on(block_or_wiretap, error)
  end

  cancel!
  return self
end

#trigger_error_on(block_or_wiretap, error) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/motion-wiretap/all/wiretap.rb', line 140

def trigger_error_on(block_or_wiretap, error)
  if block_or_wiretap.is_a? Wiretap
    block_or_wiretap.trigger_error(error)
  else
    enqueue do
      block_or_wiretap.call(error)
    end
  end
end