Class: Google::Cloud::Firestore::DocumentListener

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

Overview

DocumentListener

An ongoing listen operation on a document reference. This is returned by calling Google::Cloud::Firestore::DocumentReference#listen.

Examples:

require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new

# Get a document reference
nyc_ref = firestore.doc "cities/NYC"

listener = nyc_ref.listen do |snapshot|
  puts "The population of #{snapshot[:name]} is #{snapshot[:population]}."
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

# Get a document reference
nyc_ref = firestore.doc "cities/NYC"

listener = nyc_ref.listen do |snapshot|
  puts "The population of #{snapshot[:name]} is #{snapshot[:population]}."
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.



189
190
191
# File 'lib/google/cloud/firestore/document_listener.rb', line 189

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

# Get a document reference
nyc_ref = firestore.doc "cities/NYC"

listener = nyc_ref.listen do |snapshot|
  puts "The population of #{snapshot[:name]} is #{snapshot[:population]}."
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)


158
159
160
161
# File 'lib/google/cloud/firestore/document_listener.rb', line 158

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

# Get a document reference
nyc_ref = firestore.doc "cities/NYC"

listener = nyc_ref.listen do |snapshot|
  puts "The population of #{snapshot[:name]} is #{snapshot[:population]}."
end

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


95
96
97
# File 'lib/google/cloud/firestore/document_listener.rb', line 95

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

# Get a document reference
nyc_ref = firestore.doc "cities/NYC"

listener = nyc_ref.listen do |snapshot|
  puts "The population of #{snapshot[:name]} is #{snapshot[:population]}."
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)


123
124
125
# File 'lib/google/cloud/firestore/document_listener.rb', line 123

def stopped?
  @listener.stopped?
end