Class: Concurrent::Cancellation::Token — Concurrent

Class List Method List File List

# Class: Concurrent::Cancellation::Token

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

## Overview

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

## Instance Method Summary (collapse)

- - (true, false) canceled?

  Is the token cancelled?.

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

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

- - (Object) loop_until_canceled { ... }

  Repeatedly evaluates block until the token is #canceled?.

- - (self) raise_if_canceled(error = CancelledOperationError)

  Raise error when cancelled.

- - (Event) to_event

  Event which will be resolved when the token is cancelled.

- - (Future) to_future

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

- - (String) to_s (also: #inspect)

  Short string representation.

## Instance Method Details

### - (`true`, `false`) canceled?

Is the token cancelled?

Returns:

- (`true`, `false`)

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

    def canceled?
      @Cancel.resolved?
    end

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

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

Parameters:

- tokens (`Token`) —

  to combine

Returns:

- (`Token`) —

  new token

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

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

### - (`Object`) loop_until_canceled { ... }

Repeatedly evaluates block until the token is #canceled?.

Yields:

- to the block repeatedly.

Yield Returns:

- (`Object`)

Returns:

- (`Object`) —

  last result of the block

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

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

### - (`self`) raise_if_canceled(error = CancelledOperationError)

Raise error when cancelled

Parameters:

- error (`#exception`) (defaults to: `CancelledOperationError`) —

  to be risen

Returns:

- (`self`)

Raises:

- the error

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

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

### - (`Event`) to_event

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

Returns:

- (`Event`) —

  Event which will be resolved when the token is cancelled.

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

    def to_event
      @Cancel.to_event
    end

### - (`Future`) to_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 .

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

    def to_future
      @Cancel.to_future
    end

### - (`String`) to_s Also known as: inspect

Short string representation.

Returns:

- (`String`)

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

    def to_s
      format '<#%s:0x%x canceled:%s>', self.class, object_id << 1, canceled?
    end