<commit_before>//
//  TaskService.swift
//  SwiftPlusPlus
//
//  Created by <NAME> on 8/10/16.
//  Copyright © 2016 Drewag LLC. All rights reserved.
//

public enum TaskResult {
    case success
    case error(UserReportableError)
}

public final class TaskService {
    public static let singleton = TaskService()

    private var backgroundOperationQueue = NSOperationQueue()
    private typealias PeriodicTaskSpec = (task: PeriodicTask, period: TaskPeriod)
    private var scheduledPeriodicTasks = [String:PeriodicTaskSpec]()
    private var scheduledSingleTasks = [SingleTask]()

    private init() {}

    static public func performInBackground(block: () -> ()) {
        return self.singleton.performInBackground(block)
    }

    static public func performInForeground(waitForCompletion wait: Bool = false, block: () -> ()) {
        return self.singleton.performInForeground(wait, block: block)
    }

    public func schedule(singleTask task: SingleTask, at date: NSDate) {
        if let existingIndex = self.index(of: task) {
            self.scheduledSingleTasks.removeAtIndex(existingIndex)
        }

        task.scheduledFor = date
        self.scheduledSingleTasks.append(task)

        dispatch_after(date.time, dispatch_get_main_queue(), {
            guard let _ = self.index(of: task) where date == task.scheduledFor else {
                return
            }
            print("Performing \(task.identifier) in foreground")
            task.perform()

            guard let index = self.index(of: task) else {
                return
            }
            self.scheduledSingleTasks.removeAtIndex(index)
        })
        print("Scheduled \(task.identifier) for \(date)")
    }<commit_msg>[BF] Don't allow scheduling single tasks in the past<commit_after>//
//  TaskService.swift
//  SwiftPlusPlus
//
//  Created by <NAME> on 8/10/16.
//  Copyright © 2016 Drewag LLC. All rights reserved.
//

public enum TaskResult {
    case success
    case error(UserReportableError)
}

public final class TaskService {
    public static let singleton = TaskService()

    private var backgroundOperationQueue = NSOperationQueue()
    private typealias PeriodicTaskSpec = (task: PeriodicTask, period: TaskPeriod)
    private var scheduledPeriodicTasks = [String:PeriodicTaskSpec]()
    private var scheduledSingleTasks = [SingleTask]()

    private init() {}

    static public func performInBackground(block: () -> ()) {
        return self.singleton.performInBackground(block)
    }

    static public func performInForeground(waitForCompletion wait: Bool = false, block: () -> ()) {
        return self.singleton.performInForeground(wait, block: block)
    }

    public func schedule(singleTask task: SingleTask, at date: NSDate) {
        guard date.timeIntervalSinceDate(NSDate()) >= 0 else {
            return
        }

        if let existingIndex = self.index(of: task) {
            self.scheduledSingleTasks.removeAtIndex(existingIndex)
        }

        task.scheduledFor = date
        self.scheduledSingleTasks.append(task)

        dispatch_after(date.time, dispatch_get_main_queue(), {
            guard let _ = self.index(of: task) where date == task.scheduledFor else {
                return
            }
            print("Performing \(task.identifier) in foreground")
            task.perform()

            guard let index = self.index(of: task) else {
                return
            }
            self.scheduledSingleTasks.removeAtIndex(index)
        })
        print("Scheduled \(task.identifier) for \(date)")
    }
{'fraction_non_alphanumeric': 0.0982594048287479, 'fraction_numerical': 0.005334081976417743, 'mean_word_length': 2.60262891809909, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, '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': '3604295', 'n_tokens_mistral': 991, 'n_tokens_neox': 937, 'n_words': 245}