<commit_before>//
//  Async.swift
//  TaskKit
//
//  Created by <NAME> on 24.09.17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation
import Dispatch

/// Timed out error.
public struct TimeoutError: Swift.Error {}

// MARK: - Async/Await on Action

public extension LazyAction {

    /// Produce action with `closure` on `queue`.
    /// Callbacks added via `onSuccess/onFailure/onAny/always` methods
    /// will be called on `DispatchQueue.main` with execution result.
    /// - parameter queue: DispatchQueue for execution.
    /// By default `DispatchQueue.global()`.
    /// - parameter work: Closure to be executed on `queue`.
    static func async<T>(
        on queue: DispatchQueue = .global(),
        work: @escaping (Input) throws -> T) -> LazyAction<Input, T> {

        return LazyAction<Input, T> { input, ending in
            queue.async {
                let result = Result<T> { try work(input) }
                DispatchQueue.main.async { ending(result) }
            }
        }
    }
}

public extension LazyAction where Input == Void {

    /// Blocks current execution context and waits for action complete.
    /// - parameter timeout: Timeout for awaiting.
    /// Should be greater than `0`. `0` means no timeout. Default `0`.
    /// `TimeoutError` will be thrown if action finished by timed out.
    func await(timeout: TimeInterval = 0) throws -> Output {
        let group  = DispatchGroup()
        var result = Result<Output>.failure(TimeoutError())

        group.enter()
        onAny { result = $0; group.leave() }.execute()

        if timeout > 0 {
            _ = group.wait(timeout: .now() + timeout)
        } else {
            group.wait()
        }

        return try result.unwrap()
    }
}

// MARK: - Convenience

public extension DispatchQueue {

    /// Produce acion with `closure` on queue.
    /// Callbacks added via `onSuccess/onFailure/onAny/always` methods
    /// will be called on `DispatchQueue.main` with execution result.
    /// - parameter closure: Closure to be executed on queue.
    func asyncValue<T>(_ closure: @escaping () throws -> T) -> Action<T> {
        return Action<T>.async(on: self, work: closure)
    }
}
<commit_msg>use DispatchSemaphore instead of DispatchGroup in `await` method<commit_after>//
//  Async.swift
//  TaskKit
//
//  Created by <NAME> on 24.09.17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation
import Dispatch

/// Timed out error.
public struct TimeoutError: Swift.Error {}

// MARK: - Async/Await on Action

public extension LazyAction {

    /// Produce action with `closure` on `queue`.
    /// Callbacks added via `onSuccess/onFailure/onAny/always` methods
    /// will be called on `DispatchQueue.main` with execution result.
    /// - parameter queue: DispatchQueue for execution.
    /// By default `DispatchQueue.global()`.
    /// - parameter work: Closure to be executed on `queue`.
    static func async<T>(
        on queue: DispatchQueue = .global(),
        work: @escaping (Input) throws -> T) -> LazyAction<Input, T> {

        return LazyAction<Input, T> { input, ending in
            queue.async {
                let result = Result<T> { try work(input) }
                DispatchQueue.main.async { ending(result) }
            }
        }
    }
}

public extension LazyAction where Input == Void {

    /// Blocks current execution context and waits for action complete.
    /// - parameter timeout: Timeout for awaiting.
    /// Should be greater than `0`. `0` means no timeout. Default `0`.
    /// `TimeoutError` will be thrown if action finished by timed out.
    func await(timeout: TimeInterval = 0) throws -> Output {
        let semaphore = DispatchSemaphore(value: 0)
        var result = Result<Output>.failure(TimeoutError())

        onAny { result = $0; semaphore.signal() }.execute()

        if timeout > 0 {
            _ = semaphore.wait(timeout: .now() + timeout)
        } else {
            semaphore.wait()
        }

        return try result.unwrap()
    }
}

// MARK: - Convenience

public extension DispatchQueue {

    /// Produce acion with `closure` on queue.
    /// Callbacks added via `onSuccess/onFailure/onAny/always` methods
    /// will be called on `DispatchQueue.main` with execution result.
    /// - parameter closure: Closure to be executed on queue.
    func asyncValue<T>(_ closure: @escaping () throws -> T) -> Action<T> {
        return Action<T>.async(on: self, work: closure)
    }
}

{'fraction_non_alphanumeric': 0.12274849899933289, 'fraction_numerical': 0.00733822548365577, 'mean_word_length': 2.2928257686676425, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 35, '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': '7086914', 'n_tokens_mistral': 1326, 'n_tokens_neox': 1264, 'n_words': 427}