Class: Google::Cloud::Firestore::QueryListener

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/google/cloud/firestore/query_listener.rb

Overview

QueryListener

An ongoing listen operation on a query. This is returned by calling Google::Cloud::Firestore::Query#listen.

Examples:

require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new

# Create a query
query = firestore.col(:cities).order(:population, :desc)

listener = query.listen do |snapshot|
  puts "The query snapshot has #{snapshot.docs.count} documents "
  puts "and has #{snapshot.changes.count} changes."
end

# When ready, stop the listen operation and close the stream.
listener.stop

Instance Method Summary collapse

Instance Method Details

#last_errorException?

The most recent unhandled error to occur while listening for changes.

If an unhandled error has occurred the listener will attempt to recover from the error and resume listening.

Examples:

require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new

# Create a query
query = firestore.col(:cities).order(:population, :desc)

listener = query.listen do |snapshot|
  puts "The query snapshot has #{snapshot.docs.count} documents "
  puts "and has #{snapshot.changes.count} changes."
end

# If an error was raised, it can be retrieved here:
listener.last_error #=> nil

# When ready, stop the listen operation and close the stream.
listener.stop

Returns:

  • (Exception, nil)

    error The most recent error raised.



184
185
186
# File 'lib/google/cloud/firestore/query_listener.rb', line 184

def last_error
  synchronize { @last_error }
end

#on_error {|callback| ... } ⇒ Object

Register to be notified of errors when raised.

If an unhandled error has occurred the listener will attempt to recover from the error and resume listening.

Multiple error handlers can be added.

Examples:

require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new

# Create a query
query = firestore.col(:cities).order(:population, :desc)

listener = query.listen do |snapshot|
  puts "The query snapshot has #{snapshot.docs.count} documents "
  puts "and has #{snapshot.changes.count} changes."
end

# Register to be notified when unhandled errors occur.
listener.on_error do |error|
  puts error
end

# When ready, stop the listen operation and close the stream.
listener.stop

Yields:

  • (callback)

    The block to be called when an error is raised.

Yield Parameters:

  • error (Exception)

    The error raised.

Raises:

  • (ArgumentError)


152
153
154
155
# File 'lib/google/cloud/firestore/query_listener.rb', line 152

def on_error &block
  raise ArgumentError, "on_error must be called with a block" unless block_given?
  synchronize { @error_callbacks << block }
end

#stopObject

Stops the client listening for changes.

Examples:

require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new

# Create a query
query = firestore.col(:cities).order(:population, :desc)

listener = query.listen do |snapshot|
  puts "The query snapshot has #{snapshot.docs.count} documents "
  puts "and has #{snapshot.changes.count} changes."
end

# When ready, stop the listen operation and close the stream.
listener.stop


87
88
89
# File 'lib/google/cloud/firestore/query_listener.rb', line 87

def stop
  @listener.stop
end

#stopped?Boolean

Whether the client has stopped listening for changes.

Examples:

require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new

# Create a query
query = firestore.col(:cities).order(:population, :desc)

listener = query.listen do |snapshot|
  puts "The query snapshot has #{snapshot.docs.count} documents "
  puts "and has #{snapshot.changes.count} changes."
end

# Checks if the listener is stopped.
listener.stopped? #=> false

# When ready, stop the listen operation and close the stream.
listener.stop

# Checks if the listener is stopped.
listener.stopped? #=> true

Returns:

  • (Boolean)


116
117
118
# File 'lib/google/cloud/firestore/query_listener.rb', line 116

def stopped?
  @listener.stopped?
end