<commit_before>//
//  RetryPromise.swift
//  Sweeft
//
//  Created by <NAME> on 7/17/17.
//

import Foundation

public class RetryPromise<I, V, E: Error>: SelfSettingPromise<V, E> {
    
    public typealias Creator = (I) -> Promise<V, E>
    public typealias RetryHandler = (E?) -> ()
    public typealias RetryBehavior = (E, @escaping RetryHandler) -> ()
    
    let input: I
    let creator: Creator
    var retryBehavior: (E, @escaping RetryHandler) -> ()
    var stopped = false
    var lastError: E?
    
    public init(input: I, creator: @escaping Creator, retryBehavior: @escaping RetryBehavior) {
        self.input = input
        self.creator = creator
        self.retryBehavior = retryBehavior
        super.init()
        self.try()
    }
    
    func `try`() {
        let promise = creator(input)
        promise.onSuccess(call: self.setter.success)
        promise.onError(call: self.handle(error:))
    }
    
    func handle(error: E) {
        guard !stopped else { return }
        self.lastError = error
        retryBehavior(error) { error in
            if let error = error {
                self.setter.error(with: error)
            } else {
                self.try()
            }
        }
    }
    
    public func stop() {
        guard !stopped else { return }
        stopped = true
        if let error = lastError {
            self.setter.error(with: error)
        }
    }
    
}

extension RetryPromise {
    
    static func new(retrying timeinterval: TimeInterval, with input: I, creator: @escaping Creator) -> RetryPromise<I, V, E> {
        return RetryPromise(input: input, creator: creator) { error, completion in
            Timer.scheduledTimer(withTimeInterval: timeinterval, repeats: false) { _ in
                completion(nil)
            }
        }
    }
    
}<commit_msg>Cleaning up retry promise<commit_after>//
//  RetryPromise.swift
//  Sweeft
//
//  Created by <NAME> on 7/17/17.
//

import Foundation

public class RetryPromise<I, V, E: Error>: SelfSettingPromise<V, E> {
    
    public typealias Creator = () -> Promise<V, E>
    public typealias RetryHandler = (E?) -> ()
    public typealias RetryBehavior = (E, @escaping RetryHandler) -> ()
    
    let creator: Creator
    let retryBehavior: RetryBehavior
    var stopped = false
    var lastError: E?
    
    public init(creator: @escaping Creator, retryBehavior: @escaping RetryBehavior) {
        self.creator = creator
        self.retryBehavior = retryBehavior
        super.init()
        self.try()
    }
    
    func `try`() {
        let promise = creator()
        promise.onSuccess(call: self.setter.success)
        promise.onError(call: self.handle(error:))
    }
    
    func handle(error: E) {
        guard !stopped else { return }
        self.lastError = error
        retryBehavior(error) { error in
            if let error = error {
                self.setter.error(with: error)
            } else {
                self.try()
            }
        }
    }
    
    public func stop() {
        guard !stopped else { return }
        stopped = true
        if let error = lastError {
            self.setter.error(with: error)
        }
    }
    
}

extension RetryPromise {
    
    static func new(retrying timeinterval: TimeInterval,
                    creator: @escaping Creator) -> RetryPromise<I, V, E> {
        
        return RetryPromise(creator: creator) { error, completion in
            Timer.scheduledTimer(withTimeInterval: timeinterval, repeats: false) { _ in
                completion(nil)
            }
        }
    }
    
}
{'fraction_non_alphanumeric': 0.10434056761268781, 'fraction_numerical': 0.0027824151363383415, 'mean_word_length': 1.8852327447833066, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 22, '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': '5617172', 'n_tokens_mistral': 1084, 'n_tokens_neox': 1057, 'n_words': 272}