Class: FQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/motion/FQuery+RMXFirebase.rb

Defined Under Namespace

Classes: OutstandingHandlers

Constant Summary collapse

EVENT_TYPES_MAP =
{
  :child_added => FEventTypeChildAdded,
  :added => FEventTypeChildAdded,
  :child_moved => FEventTypeChildMoved,
  :moved => FEventTypeChildMoved,
  :child_changed => FEventTypeChildChanged,
  :changed => FEventTypeChildChanged,
  :child_removed => FEventTypeChildRemoved,
  :removed => FEventTypeChildRemoved,
  :value => FEventTypeValue
}

Instance Method Summary collapse

Instance Method Details

#_off(handle) ⇒ Object



160
161
162
163
164
165
# File 'lib/motion/FQuery+RMXFirebase.rb', line 160

def _off(handle)
  if @_outstanding_handlers
    @_outstanding_handlers.off(handle)
  end
  self
end

#descriptionObject



174
175
176
# File 'lib/motion/FQuery+RMXFirebase.rb', line 174

def description
  AFHTTPRequestSerializer.serializer.requestWithMethod("GET", URLString: "https://#{repo.repoInfo.host}#{path.toString}", parameters:queryParams.queryObject).URL.absoluteString
end

#ending_at(priority) ⇒ Object



27
28
29
# File 'lib/motion/FQuery+RMXFirebase.rb', line 27

def ending_at(priority)
  queryEndingAtPriority(priority)
end

#limited(limit) ⇒ Object



19
20
21
# File 'lib/motion/FQuery+RMXFirebase.rb', line 19

def limited(limit)
  queryLimitedToNumberOfChildren(limit)
end

#off(handle) ⇒ Object



167
168
169
170
171
172
# File 'lib/motion/FQuery+RMXFirebase.rb', line 167

def off(handle)
  RMXFirebase::QUEUE.barrier_async do
    _off(handle)
  end
  self
end

#on(_event_type, options = {}, &and_then) ⇒ Object



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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/motion/FQuery+RMXFirebase.rb', line 31

def on(_event_type, options={}, &and_then)
  and_then ||= options[:completion]
  raise "event handler is required" unless and_then
  raise "event handler must accept one or two arguments" unless and_then.arity == 1 || and_then.arity == 2
  completion = RMX.safe_block(and_then)

  event_type = EVENT_TYPES_MAP[_event_type]
  raise "event handler is unknown: #{_event_type.inspect}" unless event_type

  _disconnect_block = options[:disconnect]
  raise ":disconnect handler must not accept any arguments" if _disconnect_block && _disconnect_block.arity != 1
  disconnect_block = nil
  if _disconnect_block
    disconnect_inner_block = RMX.safe_block(_disconnect_block)
    disconnect_block = lambda do |err|
      disconnect_inner_block.call(err)
    end.weak!
  end
  handler = nil
  if and_then.arity == 1
    inner_block = RMX.safe_block(lambda do |snap|
      datasnap = RMXFirebaseDataSnapshot.new(snap)
      completion.call(datasnap)
    end)
    wrapped_block = lambda do |snap|
      inner_block.call(snap)
    end.weak!
    if disconnect_block
      if options[:once]
        RMXFirebase::INTERNAL_QUEUE.sync do
          handler = observeSingleEventOfType(event_type, withBlock:wrapped_block, withCancelBlock:disconnect_block)
        end
      else
        RMXFirebase::INTERNAL_QUEUE.sync do
          handler = observeEventType(event_type, withBlock:wrapped_block, withCancelBlock:disconnect_block)
        end
      end
    else
      if options[:once]
        RMXFirebase::INTERNAL_QUEUE.sync do
          handler = observeSingleEventOfType(event_type, withBlock:wrapped_block)
        end
      else
        RMXFirebase::INTERNAL_QUEUE.sync do
          handler = observeEventType(event_type, withBlock:wrapped_block)
        end
      end
    end
  else
    inner_block = RMX.safe_block(lambda do |snap, prev|
      datasnap = RMXFirebaseDataSnapshot.new(snap)
      completion.call(datasnap, prev)
    end)
    wrapped_block = lambda do |snap, prev|
      inner_block.call(snap, prev)
    end.weak!
    if disconnect_block
      if options[:once]
        RMXFirebase::INTERNAL_QUEUE.sync do
          handler = observeSingleEventOfType(event_type, andPreviousSiblingNameWithBlock:wrapped_block, withCancelBlock:disconnect_block)
        end
      else
        RMXFirebase::INTERNAL_QUEUE.sync do
          handler = observeEventType(event_type, andPreviousSiblingNameWithBlock:wrapped_block, withCancelBlock:disconnect_block)
        end
      end
    else
      if options[:once]
        RMXFirebase::INTERNAL_QUEUE.sync do
          handler = observeSingleEventOfType(event_type, andPreviousSiblingNameWithBlock:wrapped_block)
        end
      else
        RMXFirebase::INTERNAL_QUEUE.sync do
          handler = observeEventType(event_type, andPreviousSiblingNameWithBlock:wrapped_block)
        end
      end
    end
  end
  unless options[:once]
    @_outstanding_handlers ||= OutstandingHandlers.new(self)
    @_outstanding_handlers << handler
  end
  handler
end

#once(event_type, options = {}, &and_then) ⇒ Object



156
157
158
# File 'lib/motion/FQuery+RMXFirebase.rb', line 156

def once(event_type, options={}, &and_then)
  on(event_type, options.merge(:once => true), &and_then)
end

#rmx_object_descObject



15
16
17
# File 'lib/motion/FQuery+RMXFirebase.rb', line 15

def rmx_object_desc
  "#{super}:#{description}"
end

#starting_at(priority) ⇒ Object



23
24
25
# File 'lib/motion/FQuery+RMXFirebase.rb', line 23

def starting_at(priority)
  queryStartingAtPriority(priority)
end