Class: Reacto::Subscriptions::SimpleSubscription

Inherits:
Object
  • Object
show all
Includes:
Subscription
Defined in:
lib/reacto/subscriptions/simple_subscription.rb

Direct Known Subclasses

BufferedSubscription, InnerSubscription

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(open: NO_ACTION, value: NO_ACTION, error: DEFAULT_ON_ERROR, close: NO_ACTION) ⇒ SimpleSubscription

Returns a new instance of SimpleSubscription.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/reacto/subscriptions/simple_subscription.rb', line 11

def initialize(
  open: NO_ACTION,
  value: NO_ACTION,
  error: DEFAULT_ON_ERROR,
  close: NO_ACTION
)
  @open = open
  @value = value
  @error = error
  @close = close

  @subscribed = true
  @subscriptions = []
  @resources = []
end

Instance Attribute Details

#subscriptionsObject (readonly)

Returns the value of attribute subscriptions.



9
10
11
# File 'lib/reacto/subscriptions/simple_subscription.rb', line 9

def subscriptions
  @subscriptions
end

Instance Method Details

#add(subscription) ⇒ Object



38
39
40
41
42
# File 'lib/reacto/subscriptions/simple_subscription.rb', line 38

def add(subscription)
  return unless subscribed?

  @subscriptions << subscription
end

#add_resource(resource) ⇒ Object



44
45
46
47
48
# File 'lib/reacto/subscriptions/simple_subscription.rb', line 44

def add_resource(resource)
  return unless subscribed?

  @resources << resource
end

#on_closeObject



72
73
74
75
76
77
78
# File 'lib/reacto/subscriptions/simple_subscription.rb', line 72

def on_close
  return unless subscribed?

  @close.call
  @subscriptions.each(&:on_close)
  unsubscribe
end

#on_error(e) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/reacto/subscriptions/simple_subscription.rb', line 64

def on_error(e)
  return unless subscribed?

  @error.call(e)
  @subscriptions.each { |s| s.on_error(e) }
  unsubscribe
end

#on_openObject



50
51
52
53
54
55
# File 'lib/reacto/subscriptions/simple_subscription.rb', line 50

def on_open
  return unless subscribed?

  @open.call
  @subscriptions.each(&:on_open)
end

#on_value(v) ⇒ Object



57
58
59
60
61
62
# File 'lib/reacto/subscriptions/simple_subscription.rb', line 57

def on_value(v)
  return unless subscribed?

  @value.call(v)
  @subscriptions.each { |s| s.on_value(v) }
end

#subscribed?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/reacto/subscriptions/simple_subscription.rb', line 27

def subscribed?
  @subscribed
end

#unsubscribeObject



31
32
33
34
35
36
# File 'lib/reacto/subscriptions/simple_subscription.rb', line 31

def unsubscribe
  @subscriptions.each(&:unsubscribe)
  @subscribed = false
  @resources.each(&:cleanup)
  @resources = []
end