<commit_before>//
//  deferred-extras.swift
//  async-deferred
//
//  Created by <NAME> on 2015-07-13.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Dispatch

/*
  Definitions that rely on or extend Deferred, but do not need the fundamental, private stuff.
*/

// MARK: minimum delay until a `Deferred` has a value

extension Deferred
{
  /// Return a `Deferred` whose determination will occur at least a number of seconds from the time of evaluation.
  /// Note that a cancelation or error will result in early determination.
  ///
  /// - parameter seconds: a number of seconds as a `Double` or `NSTimeInterval`
  /// - returns: a `Deferred` reference

  public final func delay(seconds delay: Double) -> Deferred
  {
    return self.delay(until: .now() + delay)
  }

  /// Return a `Deferred` whose determination will occur at the earliest`delay` from the time of evaluation.
  /// Note that a cancelation or error will result in early determination.
  ///
  /// - parameter delay: a time interval, as `DispatchTimeInterval`
  /// - returns: a `Deferred` reference

  public final func delay(_ delay: DispatchTimeInterval) -> Deferred
  {
    return self.delay(until: .now() + delay)
  }

  /// Return a `Deferred` whose determination will occur after a given timestamp.
  /// Note that a cancelation or error will result in early determination.
  ///
  /// - parameter seconds: a number of seconds as a `Double` or `NSTimeInterval`
  /// - returns: a `Deferred` reference

  public final func delay(until time: DispatchTime) -> Deferred
  {
    guard time > .now() || time == .distantFuture else { return self }

    return Delayed(source: self, until: time)
  }
}

// MARK: maximum time until a `Deferred` becomes determined

@_versioned let DefaultTimeoutMessage = "Operation timed out"

extension Deferred
{
  /// Ensure this `Deferred` will be determined by the given deadline.
  /// If `self` has not become determined before the timeout expires, `self` will be canceled.
  ///
  /// - parameter seconds: a number of seconds as a `Double` or `NSTimeInterval`
  /// - parameter reason: the reason for the cancelation if the operation times out. Defaults to "Operation timed out".
  /// - returns: self

  @discardableResult
  public final func timeout(seconds: Double, reason: String = DefaultTimeoutMessage) -> Deferred
  {
    return self.timeout(after: .now() + seconds, reason: reason)
  }

  /// Ensure this `Deferred` will be determined by the given deadline.
  /// If `self` has not become determined before the timeout expires, `self` will be canceled.
  ///
  /// - parameter timeout: a time interval
  /// - parameter reason: the reason for the cancelation if the operation times out. Defaults to "Operation timed out".
  /// - returns: self

  @discardableResult
  public final func timeout(_ timeout: DispatchTimeInterval, reason: String = DefaultTimeoutMessage) -> Deferred
  {
    return self.timeout(after: .now() + timeout, reason: reason)
  }

  /// Ensure this `Deferred` will be determined by the given deadline.
  /// If `self` has not become determined before the timeout expires, `self` will be canceled.
  ///
  /// - parameter deadline: a timestamp used as a deadline
  /// - parameter reason: the reason for the cancelation if the operation times out. Defaults to "Operation timed out".
  /// - returns: self

  @discardableResult
  public final func timeout(after deadline: DispatchTime, reason: String = DefaultTimeoutMessage) -> Deferred
  {
    if deadline == .distantFuture || isDetermined
    {
      return self
    }

    guard deadline > .now() else
    {
      self.cancel(reason)
      return self
    }

    let queue = DispatchQueue.global(qos: self.qos.qosClass)
    queue.asyncAfter(deadline: deadline) { self.cancel(reason) }
    return self
  }
}
<commit_msg>reminders to remove comparisons that special-case `DispatchTime.distantFuture`<commit_after>//
//  deferred-extras.swift
//  async-deferred
//
//  Created by <NAME> on 2015-07-13.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Dispatch

/*
  Definitions that rely on or extend Deferred, but do not need the fundamental, private stuff.
*/

// MARK: minimum delay until a `Deferred` has a value

extension Deferred
{
  /// Return a `Deferred` whose determination will occur at least a number of seconds from the time of evaluation.
  /// Note that a cancelation or error will result in early determination.
  ///
  /// - parameter seconds: a number of seconds as a `Double` or `NSTimeInterval`
  /// - returns: a `Deferred` reference

  public final func delay(seconds delay: Double) -> Deferred
  {
    return self.delay(until: .now() + delay)
  }

  /// Return a `Deferred` whose determination will occur at the earliest`delay` from the time of evaluation.
  /// Note that a cancelation or error will result in early determination.
  ///
  /// - parameter delay: a time interval, as `DispatchTimeInterval`
  /// - returns: a `Deferred` reference

  public final func delay(_ delay: DispatchTimeInterval) -> Deferred
  {
    return self.delay(until: .now() + delay)
  }

  /// Return a `Deferred` whose determination will occur after a given timestamp.
  /// Note that a cancelation or error will result in early determination.
  ///
  /// - parameter seconds: a number of seconds as a `Double` or `NSTimeInterval`
  /// - returns: a `Deferred` reference

  public final func delay(until time: DispatchTime) -> Deferred
  { // FIXME: don't special-case .distantFuture (https://bugs.swift.org/browse/SR-5706)
    guard time > .now() || time == .distantFuture else { return self }

    return Delayed(source: self, until: time)
  }
}

// MARK: maximum time until a `Deferred` becomes determined

@_versioned let DefaultTimeoutMessage = "Operation timed out"

extension Deferred
{
  /// Ensure this `Deferred` will be determined by the given deadline.
  /// If `self` has not become determined before the timeout expires, `self` will be canceled.
  ///
  /// - parameter seconds: a number of seconds as a `Double` or `NSTimeInterval`
  /// - parameter reason: the reason for the cancelation if the operation times out. Defaults to "Operation timed out".
  /// - returns: self

  @discardableResult
  public final func timeout(seconds: Double, reason: String = DefaultTimeoutMessage) -> Deferred
  {
    return self.timeout(after: .now() + seconds, reason: reason)
  }

  /// Ensure this `Deferred` will be determined by the given deadline.
  /// If `self` has not become determined before the timeout expires, `self` will be canceled.
  ///
  /// - parameter timeout: a time interval
  /// - parameter reason: the reason for the cancelation if the operation times out. Defaults to "Operation timed out".
  /// - returns: self

  @discardableResult
  public final func timeout(_ timeout: DispatchTimeInterval, reason: String = DefaultTimeoutMessage) -> Deferred
  {
    return self.timeout(after: .now() + timeout, reason: reason)
  }

  /// Ensure this `Deferred` will be determined by the given deadline.
  /// If `self` has not become determined before the timeout expires, `self` will be canceled.
  ///
  /// - parameter deadline: a timestamp used as a deadline
  /// - parameter reason: the reason for the cancelation if the operation times out. Defaults to "Operation timed out".
  /// - returns: self

  @discardableResult
  public final func timeout(after deadline: DispatchTime, reason: String = DefaultTimeoutMessage) -> Deferred
  { // FIXME: don't special-case .distantFuture (https://bugs.swift.org/browse/SR-5706)
    if deadline == .distantFuture || isDetermined
    {
      return self
    }

    guard deadline > .now() else
    {
      self.cancel(reason)
      return self
    }

    let queue = DispatchQueue.global(qos: self.qos.qosClass)
    queue.asyncAfter(deadline: deadline) { self.cancel(reason) }
    return self
  }
}

{'fraction_non_alphanumeric': 0.1050628092881614, 'fraction_numerical': 0.004060398426595609, 'mean_word_length': 3.1073475768629493, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3613329', 'n_tokens_mistral': 2233, 'n_tokens_neox': 2149, 'n_words': 892}