Class: RMXFirebaseBatch

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRMXFirebaseBatch

Returns a new instance of RMXFirebaseBatch.



15
16
17
18
19
# File 'lib/motion/RMXFirebaseBatch.rb', line 15

def initialize
  @models = []
  @ready_models = []
  @complete_blocks = {}
end

Class Method Details

.new(*models) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/motion/RMXFirebaseBatch.rb', line 5

def self.new(*models)
  _models = models.dup
  _models = _models.flatten.compact
  x = super()
  RMXFirebase::QUEUE.barrier_async do
    x.setup_models(_models)
  end
  x
end

Instance Method Details

#cancel!Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/motion/RMXFirebaseBatch.rb', line 70

def cancel!
  RMXFirebase::QUEUE.barrier_async do
    models_outstanding = @complete_blocks.keys.dup
    while models_outstanding.size > 0
      model = models_outstanding.shift
      if blk = @complete_blocks[model]
        @complete_blocks.delete(model)
        model.cancel_block(&blk)
      end
    end
  end
end

#cancel_block(&block) ⇒ Object



98
99
100
# File 'lib/motion/RMXFirebaseBatch.rb', line 98

def cancel_block(&block)
  RMX(self).off(:ready, &block)
end

#once(queue = nil, &block) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/motion/RMXFirebaseBatch.rb', line 87

def once(queue=nil, &block)
  RMXFirebase::QUEUE.barrier_async do
    if ready?
      RMXFirebase.block_on_queue(queue, @ready_models.dup, &block)
    else
      RMX(self).once(:ready, :strong => true, :queue => queue, &block)
    end
  end
  self
end

#ready!Object



61
62
63
64
65
66
67
68
# File 'lib/motion/RMXFirebaseBatch.rb', line 61

def ready!
  RMXFirebase::QUEUE.barrier_async do
    @ready = true
    # p "models", models.dup
    # p "ready_models", ready_models.dup
    RMX(self).trigger(:ready, @ready_models.dup)
  end
end

#ready?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/motion/RMXFirebaseBatch.rb', line 83

def ready?
  !!@ready
end

#setup_models(the_models) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/motion/RMXFirebaseBatch.rb', line 21

def setup_models(the_models)
  RMX(self).require_queue!(RMXFirebase::QUEUE, __FILE__, __LINE__) if RMX::DEBUG_QUEUES
  @ready = false
  @models = the_models.dup
  @ready_count = 0
  @pending_count = @models.size
  if @models.any?
    _models = @models.dup
    _pairs = []
    i = 0
    while _models.size > 0
      ii = i # strange: proc doesnt seem to close over i correctly
      model = _models.shift
      blk = proc do
        RMX(self).require_queue!(RMXFirebase::QUEUE, __FILE__, __LINE__) if RMX::DEBUG_QUEUES
        # p "COMPLETE!", ii, model
        @complete_blocks.delete(model)
        @ready_models[ii] = model
        @ready_count += 1
        @pending_count -= 1
        if @pending_count == 0
          ready!
        end
      end
      @complete_blocks[model] = blk
      _pairs << [ model, blk ]
      i += 1
    end
    RMXFirebase::QUEUE.barrier_async do
      while pair = _pairs.shift
        pair[0].once(RMXFirebase::QUEUE, &pair[1])
      end
    end
  else
    RMXFirebase::QUEUE.barrier_async do
      ready!
    end
  end
end