Created
May 7, 2016 03:45
-
-
Save mnespor/be0d60bfefc04e54608b3cf67cbf0c43 to your computer and use it in GitHub Desktop.
Exploration of PromiseKit issue 415
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation.NSNotification | |
#if !COCOAPODS | |
import PromiseKit | |
#endif | |
/** | |
To import the `NSNotificationCenter` category: | |
use_frameworks! | |
pod "PromiseKit/Foundation" | |
Or `NSNotificationCenter` is one of the categories imported by the umbrella pod: | |
use_frameworks! | |
pod "PromiseKit" | |
And then in your sources: | |
import PromiseKit | |
*/ | |
extension NSNotificationCenter { | |
public class func once(name: String) -> NotificationPromise<[NSObject: AnyObject]> { | |
return NSNotificationCenter.defaultCenter().once(name) | |
} | |
public func once(name: String) -> NotificationPromise<[NSObject: AnyObject]> { | |
let (promise, fulfill) = NotificationPromise<[NSObject: AnyObject]>.go() | |
let id = addObserverForName(name, object: nil, queue: nil, usingBlock: fulfill) | |
promise.then(on: zalgo) { _ in self.removeObserver(id) } | |
return promise | |
} | |
} | |
public class NotificationPromise<T>: Promise<T> { | |
private let (parentPromise, parentFulfill, _) = Promise<NSNotification>.pendingPromise() | |
public func asNotification() -> Promise<NSNotification> { | |
return parentPromise | |
} | |
private class func go() -> (NotificationPromise<[NSObject: AnyObject]>, (NSNotification) -> Void) { | |
var fulfill: (([NSObject: AnyObject]) -> Void)! | |
let promise = NotificationPromise<[NSObject: AnyObject]> { f, _ in fulfill = f } | |
promise.parentPromise.then { fulfill($0.userInfo ?? [:]) } | |
return (promise, promise.parentFulfill) | |
} | |
private override init(@noescape resolvers: (fulfill: (T) -> Void, reject: (ErrorType) -> Void) throws -> Void) { | |
super.init(resolvers: resolvers) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
here are the differences https://www.diffchecker.com/jgujcajo