class Rx::SerialSubscription

Represents a subscription resource whose underlying subscription resource can be replaced by another subscription resource, causing automatic disposal of the previous underlying subscription resource.

Public Class Methods

new() click to toggle source
# File lib/rx/subscriptions/serial_subscription.rb, line 11
def initialize
  @gate = Mutex.new
  @current = nil
  @unsubscribed = false
end

Public Instance Methods

subscription() click to toggle source

Gets the underlying subscription.

# File lib/rx/subscriptions/serial_subscription.rb, line 25
def subscription
  @current
end
subscription=(new_subscription) click to toggle source

Sets the underlying subscription.

# File lib/rx/subscriptions/serial_subscription.rb, line 30
def subscription=(new_subscription)
  should_unsubscribe = false
  old = nil
  @gate.synchronize do
    should_unsubscribe = @unsubscribed
    unless should_unsubscribe
      old = @current
      @current = new_subscription
    end
  end

  old.unsubscribe if old
  new_subscription.unsubscribe if should_unsubscribe && !new_subscription.nil?
end
unsubscribe() click to toggle source

Unsubscribes the current underlying subscription and all future subscriptions.

# File lib/rx/subscriptions/serial_subscription.rb, line 46
def unsubscribe
  old = nil
  @gate.synchronize do
    unless @unsubscribed
      @unsubscribed = true
      old = @current
      @current = nil
    end
  end

  old.unsubscribe if old
end
unsubscribed?() click to toggle source

Gets a value that indicates whether the object is unsubscribed.

# File lib/rx/subscriptions/serial_subscription.rb, line 18
def unsubscribed?
  @gate.synchronize do
    return @unsubscribed
  end
end