Class: IOPromise::CancelContext
- Inherits:
-
Object
- Object
- IOPromise::CancelContext
- Defined in:
- lib/iopromise/cancel_context.rb
Class Method Summary collapse
Instance Method Summary collapse
- #cancel ⇒ Object
-
#initialize(parent) ⇒ CancelContext
constructor
A new instance of CancelContext.
- #subscribe(observer) ⇒ Object
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_stack ⇒ Object
6 7 8 |
# File 'lib/iopromise/cancel_context.rb', line 6 def context_stack Thread.current[:iopromise_context_stack] ||= [] end |
.current ⇒ Object
10 11 12 |
# File 'lib/iopromise/cancel_context.rb', line 10 def current context_stack.last end |
.pop ⇒ Object
20 21 22 23 24 |
# File 'lib/iopromise/cancel_context.rb', line 20 def pop ctx = context_stack.pop ctx.cancel ctx end |
.push ⇒ Object
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_context ⇒ Object
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
#cancel ⇒ Object
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 |