Class: RxRuby::SingleAssignmentSubscription

Inherits:
Object
  • Object
show all
Defined in:
lib/rx_ruby/subscriptions/single_assignment_subscription.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initializeSingleAssignmentSubscription

Returns a new instance of SingleAssignmentSubscription.



11
12
13
14
15
16
# File 'lib/rx_ruby/subscriptions/single_assignment_subscription.rb', line 11

def initialize
  @gate = Mutex.new() 
  @current = nil
  @unsubscribed = false
  @set = false
end

Instance Method Details

#subscriptionObject

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



26
27
28
# File 'lib/rx_ruby/subscriptions/single_assignment_subscription.rb', line 26

def subscription
  @current
end

#subscription=(new_subscription) ⇒ Object

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rx_ruby/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

#unsubscribeObject

Unsubscribes the underlying subscription



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rx_ruby/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?Boolean

Gets a value that indicates whether the object is unsubscribed.

Returns:

  • (Boolean)


19
20
21
22
23
# File 'lib/rx_ruby/subscriptions/single_assignment_subscription.rb', line 19

def unsubscribed?
  @gate.synchronize do
    return @unsubscribed
  end
end