Class: Rx::CompositeSubscription

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rx/subscriptions/composite_subscription.rb

Overview

Represents a group of subscription resources that are unsubscribed together.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#subscribe, #to_observable

Constructor Details

#initialize(subscriptions = []) ⇒ CompositeSubscription

Returns a new instance of CompositeSubscription.



14
15
16
17
18
19
# File 'lib/rx/subscriptions/composite_subscription.rb', line 14

def initialize(subscriptions = [])
  @subscriptions = subscriptions
  @length = subscriptions.length
  @unsubscribed = false
  @gate = Mutex.new
end

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



12
13
14
# File 'lib/rx/subscriptions/composite_subscription.rb', line 12

def length
  @length
end

Instance Method Details

#clearObject

Removes and unsubscribes all subscriptions from the CompositeSubscription, but does not dispose the CompositeSubscription.



66
67
68
69
70
71
72
73
74
75
# File 'lib/rx/subscriptions/composite_subscription.rb', line 66

def clear
  currentSubscriptions = nil

  @gate.synchronize do
    currentSubscriptions = @subscriptions
    @subscriptions = []
    @length = 0
  end
  currentSubscriptions.each {|subscription| subscription.unsubscribe}
end

#delete(subscription) ⇒ Object

Removes and unsubscribes the first occurrence of a subscription from the CompositeSubscription.



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rx/subscriptions/composite_subscription.rb', line 78

def delete(subscription)
  should_unsubscribe = nil

  @gate.synchronize do
    should_unsubscribe = @subscriptions.delete(subscription)
    @length -= 1 if should_unsubscribe
  end

  subscription.unsubscribe if should_unsubscribe

  should_unsubscribe
end

#each(&block) ⇒ Object



26
27
28
# File 'lib/rx/subscriptions/composite_subscription.rb', line 26

def each(&block)
  @subscriptions.each(&block)
end

#push(subscription) ⇒ Object Also known as: <<

Adds a subscription to the CompositeSubscription or unsubscribes the subscription if the CompositeSubscription is unsubscribed.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rx/subscriptions/composite_subscription.rb', line 47

def push(subscription)
  should_unsubscribe = false

  @gate.synchronize do
    should_unsubscribe = @unsubscribed
  
    unless @unsubscribed
      @subscriptions.push(subscription)
      @length += 1
    end
  end

  subscription.unsubscribe if should_unsubscribe

  return self
end

#unsubscribeObject

Unsubscribes all subscriptions in the group and removes them from the group.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rx/subscriptions/composite_subscription.rb', line 31

def unsubscribe
  currentSubscriptions = nil

  @gate.synchronize do
    unless @unsubscribed
      @unsubscribed = true
      currentSubscriptions = @subscriptions
      @subscriptions = []
      @length = 0
    end
  end

  currentSubscriptions.each {|subscription| subscription.unsubscribe} if currentSubscriptions
end

#unsubscribed?Boolean

Gets a value that indicates whether the object is unsubscribed.

Returns:

  • (Boolean)


22
23
24
# File 'lib/rx/subscriptions/composite_subscription.rb', line 22

def unsubscribed?
  @unsubscribed
end