Class: RMXFirebaseCoordinator

Inherits:
Object
  • Object
show all
Includes:
RMXCommonMethods
Defined in:
lib/motion/RMXFirebaseCoordinator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRMXFirebaseCoordinator

Returns a new instance of RMXFirebaseCoordinator.



7
8
9
# File 'lib/motion/RMXFirebaseCoordinator.rb', line 7

def initialize
  @watches = {}
end

Instance Attribute Details

#watchesObject (readonly)

Returns the value of attribute watches.



5
6
7
# File 'lib/motion/RMXFirebaseCoordinator.rb', line 5

def watches
  @watches
end

Instance Method Details

#attr(keypath) ⇒ Object



97
98
99
# File 'lib/motion/RMXFirebaseCoordinator.rb', line 97

def attr(keypath)
  valueForKeyPath(keypath)
end

#cancelled!Object



42
43
44
45
46
47
48
# File 'lib/motion/RMXFirebaseCoordinator.rb', line 42

def cancelled!
  RMXFirebase::QUEUE.barrier_async do
    @cancelled = true
    RMX(self).trigger(:cancelled, self)
    RMX(self).trigger(:finished, self)
  end
end

#cancelled?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/motion/RMXFirebaseCoordinator.rb', line 30

def cancelled?
  !!@cancelled
end

#clear!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/motion/RMXFirebaseCoordinator.rb', line 11

def clear!
  RMXFirebase::QUEUE.barrier_async do
    @cancelled = false
    @ready = false
    keys = watches.keys.dup
    while keys.size > 0
      name = keys.shift
      watch = watches[name]
      watch.stop!
    end
    watches.clear
  end
  self
end

#ready!Object



34
35
36
37
38
39
40
# File 'lib/motion/RMXFirebaseCoordinator.rb', line 34

def ready!
  RMXFirebase::QUEUE.barrier_async do
    @ready = true
    RMX(self).trigger(:ready, self)
    RMX(self).trigger(:finished, self)
  end
end

#ready?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/motion/RMXFirebaseCoordinator.rb', line 26

def ready?
  !!@ready
end

#stub(name) ⇒ Object



50
51
52
53
# File 'lib/motion/RMXFirebaseCoordinator.rb', line 50

def stub(name)
  RMX(self).require_queue!(RMXFirebase::QUEUE, __FILE__, __LINE__) if RMX::DEBUG_QUEUES
  watches[name] ||= RMXFirebaseListener.new
end

#valueForKey(key) ⇒ Object



101
102
103
# File 'lib/motion/RMXFirebaseCoordinator.rb', line 101

def valueForKey(key)
  watches[key]
end

#valueForUndefinedKey(key) ⇒ Object



105
106
107
# File 'lib/motion/RMXFirebaseCoordinator.rb', line 105

def valueForUndefinedKey(key)
  nil
end

#watch(name, ref, opts = {}, &block) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/motion/RMXFirebaseCoordinator.rb', line 55

def watch(name, ref, opts={}, &block)
  RMX(self).require_queue!(RMXFirebase::QUEUE, __FILE__, __LINE__) if RMX::DEBUG_QUEUES
  data = RMXFirebaseListener.new
  data.ref = ref
  if opts[:required]
    data.value_required = true
  end
  unless block.nil?
    data.callback = block.weak!
    data.callback_owner = block.owner
  end
  if current = watches[name]
    if current == data || current.description == data.description
      return
    end
    current.stop!
    RMX(current).off(:finished, self)
  end
  watches[name] = data
  RMX(data).on(:finished, :queue => RMXFirebase::QUEUE) do
    RMX(self).require_queue!(RMXFirebase::QUEUE, __FILE__, __LINE__) if RMX::DEBUG_QUEUES
    readies = 0
    cancelled = 0
    size = watches.size
    watches.each_pair do |k, v|
      if v.ready?
        readies += 1
      elsif v.cancelled?
        cancelled += 1
        break
      end
    end
    if cancelled > 0
      cancelled!
    elsif readies == size
      ready!
    end
  end
  data.start!
  data
end