Class: IOPromise::CancelContext

Inherits:
Object
  • Object
show all
Defined in:
lib/iopromise/cancel_context.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ CancelContext

Returns a new instance of CancelContext.



34
35
36
# File 'lib/iopromise/cancel_context.rb', line 34

def initialize(parent)
  parent.subscribe(self) unless parent.nil?
end

Class Method Details

.context_stackObject



6
7
8
# File 'lib/iopromise/cancel_context.rb', line 6

def context_stack
  Thread.current[:iopromise_context_stack] ||= []
end

.currentObject



10
11
12
# File 'lib/iopromise/cancel_context.rb', line 10

def current
  context_stack.last
end

.popObject



20
21
22
23
24
# File 'lib/iopromise/cancel_context.rb', line 20

def pop
  ctx = context_stack.pop
  ctx.cancel
  ctx
end

.pushObject



14
15
16
17
18
# File 'lib/iopromise/cancel_context.rb', line 14

def push
  new_ctx = CancelContext.new(current)
  context_stack.push(new_ctx)
  new_ctx
end

.with_new_contextObject



26
27
28
29
30
31
# File 'lib/iopromise/cancel_context.rb', line 26

def with_new_context
  ctx = push
  yield ctx
ensure
  pop
end

Instance Method Details

#cancelObject



43
44
45
46
47
48
49
# File 'lib/iopromise/cancel_context.rb', line 43

def cancel
  return unless defined?(@observers)
  @observers.each do |o|
    o.cancel
  end
  @observers = []
end

#subscribe(observer) ⇒ Object



38
39
40
41
# File 'lib/iopromise/cancel_context.rb', line 38

def subscribe(observer)
  @observers ||= []
  @observers.push observer
end