# Class: Concurrent::Cancellation::Token

Inherits:
  Synchronization::Object
  - Object
  - Synchronization::Object
  - Concurrent::Cancellation::Token
  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

- #canceled? ⇒ true, false

  Is the token cancelled?.

- #join(*tokens, &block) ⇒ Token

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

- #loop_until_canceled { ... } ⇒ Object

  Repeatedly evaluates block until the token is #canceled?.

- #raise_if_canceled(error = CancelledOperationError) ⇒ self

  Raise error when cancelled.

- #to_event ⇒ Event

  Event which will be resolved when the token is cancelled.

- #to_future ⇒ Future

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

- #to_s ⇒ String (also: #inspect)

  Short string representation.

## 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:

- (`Token`) —

  new token

    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_event ⇒ `Event`

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_future ⇒ `Future`

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

Returns:

- (`Future`) —

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

    83
    84
    85

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

    def to_future
      @Cancel.to_future
    end

### #to_s ⇒ `String` 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