class Rx::SingleAssignmentSubscription

Represents a subscription resource which only allows a single assignment of its underlying subscription resource. If an underlying subscription resource has already been set, future attempts to set the underlying subscription resource will throw an error

Public Class Methods

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

Public Instance Methods

subscription() click to toggle source

Gets the underlying subscription. After unsubscribing, the result of getting this property is undefined.

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

Sets the underlying disposable. If this has already been set, then an error is raised.

# File lib/rx/subscriptions/single_assignment_subscription.rb, line 31
def subscription=(new_subscription)
  raise 'Subscription already set' if @set

  @set = true
  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 underlying subscription

# File lib/rx/subscriptions/single_assignment_subscription.rb, line 50
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/single_assignment_subscription.rb, line 19
def unsubscribed?
  @gate.synchronize do
    return @unsubscribed
  end
end