<commit_before>//
//  SDWebImageManager+PromiseKit.swift
//  Wikipedia
//
//  Created by <NAME> on 6/22/15.
//  Copyright (c) 2015 Wikimedia Foundation. All rights reserved.
//

import Foundation
import PromiseKit

extension SDImageCacheType: ImageOriginConvertible {
    public func asImageOrigin() -> ImageOrigin {
        switch self {
        case SDImageCacheType.Disk:
            return ImageOrigin.Disk
        case SDImageCacheType.Memory:
            return ImageOrigin.Memory
        case SDImageCacheType.None:
            return ImageOrigin.Network
        }
    }
}

// FIXME: Can't extend the SDWebImageOperation protocol or cast the return value, so we wrap it.
class SDWebImageOperationWrapper: NSObject, Cancellable {
    weak var operation: SDWebImageOperation?
    required init(operation: SDWebImageOperation) {
        super.init()
        self.operation = operation
        // keep wrapper around as long as operation is around
        objc_setAssociatedObject(operation,
                                 "SDWebImageOperationWrapper",
                                 self,
                                 objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
    }

    func cancel() -> Void {
        operation?.cancel()
    }
}

extension SDWebImageManager {
    func promisedImageWithURL(URL: NSURL, options: SDWebImageOptions) -> (Cancellable, Promise<WMFImageDownload>) {
        let (promise, fulfill, reject) = Promise<WMFImageDownload>.pendingPromise()

        let promiseCompatibleOptions = options | SDWebImageOptions.ReportCancellationAsError

        let webImageOperation = self.downloadImageWithURL(URL, options: promiseCompatibleOptions, progress: nil)
        { img, err, cacheType, finished, imageURL in
                if finished && err == nil {
                    fulfill(WMFImageDownload(url: URL, image: img, origin: asImageOrigin(cacheType).rawValue))
                } else {
                    reject(err)
                }
        }<commit_msg>use new Swift 2 syntax for OptionSetType<commit_after>//
//  SDWebImageManager+PromiseKit.swift
//  Wikipedia
//
//  Created by <NAME> on 6/22/15.
//  Copyright (c) 2015 Wikimedia Foundation. All rights reserved.
//

import Foundation
import PromiseKit

extension SDImageCacheType: ImageOriginConvertible {
    public func asImageOrigin() -> ImageOrigin {
        switch self {
        case SDImageCacheType.Disk:
            return ImageOrigin.Disk
        case SDImageCacheType.Memory:
            return ImageOrigin.Memory
        case SDImageCacheType.None:
            return ImageOrigin.Network
        }
    }
}

// FIXME: Can't extend the SDWebImageOperation protocol or cast the return value, so we wrap it.
class SDWebImageOperationWrapper: NSObject, Cancellable {
    weak var operation: SDWebImageOperation?
    required init(operation: SDWebImageOperation) {
        super.init()
        self.operation = operation
        // keep wrapper around as long as operation is around
        objc_setAssociatedObject(operation,
                                 "SDWebImageOperationWrapper",
                                 self,
                                 objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
    }

    func cancel() -> Void {
        operation?.cancel()
    }
}

extension SDWebImageManager {
    func promisedImageWithURL(URL: NSURL, options: SDWebImageOptions) -> (Cancellable, Promise<WMFImageDownload>) {
        let (promise, fulfill, reject) = Promise<WMFImageDownload>.pendingPromise()

        let promiseCompatibleOptions: SDWebImageOptions = [options, SDWebImageOptions.ReportCancellationAsError]

        let webImageOperation = self.downloadImageWithURL(URL, options: promiseCompatibleOptions, progress: nil)
        { img, err, cacheType, finished, imageURL in
                if finished && err == nil {
                    fulfill(WMFImageDownload(url: URL, image: img, origin: asImageOrigin(cacheType).rawValue))
                } else {
                    reject(err)
                }
        }
{'fraction_non_alphanumeric': 0.07627746235497408, 'fraction_numerical': 0.004690199950629474, 'mean_word_length': 2.3851294903926483, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2269009', 'n_tokens_mistral': 1064, 'n_tokens_neox': 1024, 'n_words': 267}