<commit_before>//
//  TimeoutObserver.swift
//  Operations
//
//  Created by <NAME> on 27/06/2015.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation

/**
An operation observer which will automatically cancels (with an error)
if it doesn't finish before a time interval is expired.
*/
public struct TimeoutObserver: OperationObserver {

    private let timeout: NSTimeInterval

    public init(timeout: NSTimeInterval) {
        self.timeout = timeout
    }

    public func operationDidStart(operation: Operation) {
        let when = dispatch_time(DISPATCH_TIME_NOW, Int64(timeout * Double(NSEC_PER_SEC)))

        dispatch_after(when, Queue.Default.queue) {
            if !operation.finished && !operation.cancelled {
                let error = OperationError.OperationTimedOut(self.timeout)
                operation.cancelWithError(error)
            }
        }
    }

    public func operation(operation: Operation, didProduceOperation newOperation: NSOperation) {
        // no-op
    }

    public func operationDidFinish(operation: Operation, errors: [ErrorType]) {
        // no-op
    }
}


<commit_msg>[OPR-100]: + docs to TimeoutObserver<commit_after>//
//  TimeoutObserver.swift
//  Operations
//
//  Created by <NAME> on 27/06/2015.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation

/**
An operation observer which will automatically cancels (with an error)
if it doesn't finish before a time interval is expired.
*/
public struct TimeoutObserver: OperationObserver {

    private let timeout: NSTimeInterval

    /**
    Initialize the operation observer with a timeout, which will start when 
    the operation to which it is attached starts.

    - parameter timeout: a `NSTimeInterval` value.
    */
    public init(timeout: NSTimeInterval) {
        self.timeout = timeout
    }

    /**
    Conforms to `OperationObserver`, when the operation starts, it triggers
    a block using dispatch_after and the time interval. When the block runs,
    if the operation has not finished and is not cancelled, then it will
    cancel it with an error of `OperationError.OperationTimedOut`
    
    - parameter operation: the `Operation` which will be cancelled if the timeout is reached.
    */
    public func operationDidStart(operation: Operation) {
        let when = dispatch_time(DISPATCH_TIME_NOW, Int64(timeout * Double(NSEC_PER_SEC)))

        dispatch_after(when, Queue.Default.queue) {
            if !operation.finished && !operation.cancelled {
                let error = OperationError.OperationTimedOut(self.timeout)
                operation.cancelWithError(error)
            }
        }
    }

    /// Conforms to `OperationObserver`, has no opertion for when another operation is produced.
    public func operation(operation: Operation, didProduceOperation newOperation: NSOperation) {}

    /// Conforms to `OperationObserver`, has no opertion for when the operation finishes
    public func operationDidFinish(operation: Operation, errors: [ErrorType]) {}
}



{'fraction_non_alphanumeric': 0.08538587848932677, 'fraction_numerical': 0.010180623973727421, 'mean_word_length': 2.976501305483029, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, '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': '5926515', 'n_tokens_mistral': 850, 'n_tokens_neox': 789, 'n_words': 268}