<commit_before>//
//  Animation.swift
//  AnimationKit
//
//  Created by <NAME> on 8/22/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation

public typealias Closure = () -> ()
public typealias SubscribeClosure = (completed: Bool, next: AnimationTask?) -> ()

class AnimationClosure: NSObject {

    let duration: NSTimeInterval
    let closure: Closure
    init(duration: NSTimeInterval, closure: Closure) {
        self.duration = duration
        self.closure = closure
    }

}

public class AnimationTask {

    var closures: [AnimationClosure] = []
    var completion: () -> () = {}
    var duration: NSTimeInterval {
        if let closure = closures.sorted({$0.duration < $1.duration}).last {
            return closure.duration
        }
        return 0
    }

    public init(duration: NSTimeInterval, closure: Closure) {
        union(duration: duration, closure: closure)
    }

    public func union(#duration: NSTimeInterval, closure: Closure) {
        closures.append(AnimationClosure(duration: duration, closure: closure))
    }

    public func start(completion: () -> ()) {
        self.completion = completion
        for c in closures {
            UIView.animateWithDuration(c.duration, animations: c.closure, completion: { _ in self._completion(c) })
        }
    }

    func _completion(closure: AnimationClosure) {
        if let index = find(closures, closure) {
            closures.removeAtIndex(index)
        }
        if closures.isEmpty {
            completion()
        }
    }
}

public enum State {
    case Ready
    case Running
}

public class Animation {

    public var tasks: [AnimationTask] = []
    public let label: String
    public var duration: NSTimeInterval {
        return tasks.reduce(0, combine: { $0 + $1.duration })
    }
    public var state: State = .Ready

    public init(_ label: String) {
        self.label = label
    }

    public func start() {
        Animation.singleton[label] = self
        state = .Running
        next()
    }

    public func union(#duration: NSTimeInterval, closure: Closure) {
        if let task = tasks.last {
            task.union(duration: duration, closure: closure)
            return
        }

        fatalError("Animation dosen't have tasks, use `append` or `-->` at first")
    }

    public func append(#duration: NSTimeInterval, closure: Closure) {
        tasks.append(AnimationTask(duration: duration, closure: closure))
    }<commit_msg>Add delay in start function<commit_after>//
//  Animation.swift
//  AnimationKit
//
//  Created by <NAME> on 8/22/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation

public typealias Closure = () -> ()
public typealias SubscribeClosure = (completed: Bool, next: AnimationTask?) -> ()

class AnimationClosure: NSObject {

    let duration: NSTimeInterval
    let closure: Closure
    init(duration: NSTimeInterval, closure: Closure) {
        self.duration = duration
        self.closure = closure
    }

}

public class AnimationTask {

    var closures: [AnimationClosure] = []
    var completion: () -> () = {}
    var duration: NSTimeInterval {
        if let closure = closures.sorted({$0.duration < $1.duration}).last {
            return closure.duration
        }
        return 0
    }

    public init(duration: NSTimeInterval, closure: Closure) {
        union(duration: duration, closure: closure)
    }

    public func union(#duration: NSTimeInterval, closure: Closure) {
        closures.append(AnimationClosure(duration: duration, closure: closure))
    }

    public func start(completion: () -> ()) {
        self.completion = completion
        for c in closures {
            UIView.animateWithDuration(c.duration, animations: c.closure, completion: { _ in self._completion(c) })
        }
    }

    func _completion(closure: AnimationClosure) {
        if let index = find(closures, closure) {
            closures.removeAtIndex(index)
        }
        if closures.isEmpty {
            completion()
        }
    }
}

public enum State {
    case Ready
    case Running
}

public class Animation {

    public var tasks: [AnimationTask] = []
    public let label: String
    public var duration: NSTimeInterval {
        return tasks.reduce(0, combine: { $0 + $1.duration })
    }
    public var state: State = .Ready

    public init(_ label: String) {
        self.label = label
    }

    public func start() {
        start(delay: 0)
    }

    public func start(#delay: NSTimeInterval) {
        Animation.singleton[label] = self
        state = .Running

        if delay == 0 {
            next()
            return
        }

        let after = delay * Double(NSEC_PER_SEC)
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(after)), dispatch_get_main_queue()) {
            self.next()
        }
    }

    public func union(#duration: NSTimeInterval, closure: Closure) {
        if let task = tasks.last {
            task.union(duration: duration, closure: closure)
            return
        }

        fatalError("Animation dosen't have tasks, use `append` or `-->` at first")
    }

    public func append(#duration: NSTimeInterval, closure: Closure) {
        tasks.append(AnimationTask(duration: duration, closure: closure))
    }
{'fraction_non_alphanumeric': 0.10751466969524891, 'fraction_numerical': 0.006435737270490252, 'mean_word_length': 2.3698979591836733, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, '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': '94069', 'n_tokens_mistral': 1582, 'n_tokens_neox': 1454, 'n_words': 402}