Class: Concurrent::Cancellation::Token

Inherits:
Synchronization::Object show all
Defined in:
lib-edge/concurrent/edge/cancellation.rb

Overview

Created through create, passed down to tasks to be able to check if canceled.

Instance Method Summary collapse

Instance Method Details

#canceled?true, false

Is the token cancelled?

Returns:

  • (true, false)


89
90
91
# File 'lib-edge/concurrent/edge/cancellation.rb', line 89

def canceled?
  @Cancel.resolved?
end

#join(*tokens, &block) ⇒ Token

Creates a new token which is cancelled when any of the tokens is.

Parameters:

  • tokens (Token)

    to combine

Returns:



116
117
118
119
# File 'lib-edge/concurrent/edge/cancellation.rb', line 116

def join(*tokens, &block)
  block ||= -> token_list { Promises.any_event(*token_list.map(&:to_event)) }
  self.class.new block.call([@Cancel, *tokens])
end

#loop_until_canceled { ... } ⇒ Object

Repeatedly evaluates block until the token is #canceled?.

Yields:

  • to the block repeatedly.

Yield Returns:

  • (Object)

Returns:

  • (Object)

    last result of the block



97
98
99
100
101
102
# File 'lib-edge/concurrent/edge/cancellation.rb', line 97

def loop_until_canceled(&block)
  until canceled?
    result = block.call
  end
  result
end

#raise_if_canceled(error = CancelledOperationError) ⇒ self

Raise error when cancelled

Parameters:

  • error (#exception) (defaults to: CancelledOperationError)

    to be risen

Returns:

  • (self)

Raises:

  • the error



108
109
110
111
# File 'lib-edge/concurrent/edge/cancellation.rb', line 108

def raise_if_canceled(error = CancelledOperationError)
  raise error if canceled?
  self
end

#to_eventEvent

Returns Event which will be resolved when the token is cancelled.

Returns:

  • (Event)

    Event which will be resolved when the token is cancelled.



77
78
79
# File 'lib-edge/concurrent/edge/cancellation.rb', line 77

def to_event
  @Cancel.to_event
end

#to_futureFuture

Returns Future which will be resolved when the token is cancelled with arguments passed in Concurrent::Cancellation.create .

Returns:



83
84
85
# File 'lib-edge/concurrent/edge/cancellation.rb', line 83

def to_future
  @Cancel.to_future
end

#to_sString Also known as: inspect

Short string representation.

Returns:

  • (String)


123
124
125
# File 'lib-edge/concurrent/edge/cancellation.rb', line 123

def to_s
  format '%s canceled:%s>', super[0..-2], canceled?
end