Skip to content

Commit b020a5c

Browse files
committed
2.2.0 — Generated Files
1 parent 64ed18c commit b020a5c

7 files changed

+111
-55
lines changed

Categories/Foundation/NSNotificationCenter+Promise.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extension NSNotificationCenter {
2424
public class func once(name: String) -> Promise<NSNotification> {
2525
return Promise { fulfill, _ in
2626
var id: AnyObject?
27-
id = NSNotificationCenter.defaultCenter().addObserverForName(name, object: nil, queue: PMKOperationQueue){ note in
27+
id = NSNotificationCenter.defaultCenter().addObserverForName(name, object: nil, queue: nil){ note in
2828
fulfill(note)
2929
NSNotificationCenter.defaultCenter().removeObserver(id!)
3030
}

Categories/Foundation/NSURLConnection+AnyPromise.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ + (AnyPromise *)promise:(NSURLRequest *)rq {
5656
static id q = nil;
5757
static dispatch_once_t onceToken;
5858
dispatch_once(&onceToken, ^{
59-
q = [NSOperationQueue new]; //TODO use PMKOperationQueue from swift if you can figure out how
59+
q = [NSOperationQueue new];
6060
});
6161

6262
return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {

Categories/Foundation/NSURLConnection+Promise.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ extension NSURLResponse {
166166
}
167167
}
168168

169+
private let Q = NSOperationQueue()
169170

170171
private func fetch(var request: NSURLRequest) -> Promise<(NSData, NSURLResponse)> {
171172
if request.valueForHTTPHeaderField("User-Agent") == nil {
@@ -175,7 +176,7 @@ private func fetch(var request: NSURLRequest) -> Promise<(NSData, NSURLResponse)
175176
}
176177

177178
return Promise { fulfill, prereject in
178-
NSURLConnection.sendAsynchronousRequest(request, queue: PMKOperationQueue) { rsp, data, err in
179+
NSURLConnection.sendAsynchronousRequest(request, queue: Q) { rsp, data, err in
179180

180181
assert(!NSThread.isMainThread())
181182

Categories/MessagesUI/MFMailComposeViewController+Promise.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extension UIViewController {
1616
proxy.retainCycle = proxy
1717
vc.mailComposeDelegate = proxy
1818
presentViewController(vc, animated: animated, completion: completion)
19-
proxy.promise.finally(on: zalgo) {
19+
proxy.promise.finally {
2020
self.dismissViewControllerAnimated(animated, completion: nil)
2121
}
2222
return proxy.promise

PromiseKit.m

+53-47
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,10 @@ static id PMKCallVariadicBlock(id frock, id result) {
921921
});
922922

923923
id err = PMKUnhandledExceptionHandler(thrown);
924-
if (!err) @throw thrown;
924+
if (!err) {
925+
NSLog(@"PromiseKit no longer catches *all* exceptions. However you can change this behavior by setting a new PMKProcessUnhandledException handler.");
926+
@throw thrown;
927+
}
925928
return err;
926929
}
927930

@@ -1279,14 +1282,14 @@ + (instancetype)promiseWithBooleanAdapter:(void (^)(PMKBooleanAdapter adapter))b
12791282
#import <Foundation/NSError.h>
12801283
#import <Foundation/NSProgress.h>
12811284
#import <Foundation/NSNull.h>
1285+
#import <libkern/OSAtomic.h>
12821286

12831287
// NSProgress resources:
12841288
// * https://robots.thoughtbot.com/asynchronous-nsprogress
12851289
// * http://oleb.net/blog/2014/03/nsprogress/
12861290
// NSProgress! Beware!
12871291
// * https://github.com/AFNetworking/AFNetworking/issues/2261
12881292

1289-
12901293
AnyPromise *PMKWhen(id promises) {
12911294
if (promises == nil)
12921295
return [AnyPromise promiseWithValue:[NSError errorWithDomain:PMKErrorDomain code:PMKInvalidUsageError userInfo:@{NSLocalizedDescriptionKey: @"PMKWhen(nil)"}]];
@@ -1304,54 +1307,57 @@ + (instancetype)promiseWithBooleanAdapter:(void (^)(PMKBooleanAdapter adapter))b
13041307
NSProgress *progress = [NSProgress progressWithTotalUnitCount:[promises count]];
13051308
progress.pausable = NO;
13061309
progress.cancellable = NO;
1310+
#else
1311+
struct PMKProgress {
1312+
int completedUnitCount;
1313+
int totalUnitCount;
1314+
};
1315+
__block struct PMKProgress progress;
13071316
#endif
13081317

1309-
PMKResolver resolve;
1310-
AnyPromise *rootPromise = [[AnyPromise alloc] initWithResolver:&resolve];
1311-
__block void (^fulfill)();
1312-
1313-
__block NSInteger countdown = [promises count];
1314-
void (^yield)(id, id, void(^)(id)) = ^(AnyPromise *promise, id key, void(^set)(id)) {
1315-
if (![promise isKindOfClass:[AnyPromise class]])
1316-
promise = [AnyPromise promiseWithValue:promise];
1317-
[promise pipe:^(id value){
1318-
if (!rootPromise.pending) {
1319-
// suppress “already resolved” log message
1320-
} else if (IsError(value)) {
1321-
#ifndef PMKDisableProgress
1322-
progress.completedUnitCount = progress.totalUnitCount;
1323-
#endif
1324-
resolve(NSErrorSupplement(value, @{PMKFailingPromiseIndexKey: key}));
1325-
} else {
1326-
#ifndef PMKDisableProgress
1327-
progress.completedUnitCount++;
1328-
#endif
1329-
set(promise.value); // we use -value to unwrap PMKManifolds
1330-
if (--countdown == 0)
1331-
fulfill();
1332-
}
1333-
}];
1334-
};
1318+
__block int32_t countdown = (int32_t)[promises count];
1319+
BOOL const isdict = [promises isKindOfClass:[NSDictionary class]];
13351320

1336-
if ([promises isKindOfClass:[NSDictionary class]]) {
1337-
NSMutableDictionary *results = [NSMutableDictionary new];
1338-
fulfill = ^{ resolve(results); };
1321+
return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
1322+
NSInteger index = 0;
13391323

1340-
for (id key in promises) {
1341-
yield(promises[key], key, ^(id value){
1342-
results[key] = value;
1343-
});
1344-
}
1345-
} else {
1346-
NSPointerArray *results = NSPointerArrayMake([promises count]);
1347-
fulfill = ^{ resolve(results.allObjects); };
1324+
for (__strong id key in promises) {
1325+
AnyPromise *promise = isdict ? promises[key] : key;
1326+
if (!isdict) key = @(index);
13481327

1349-
[promises enumerateObjectsUsingBlock:^(id promise, NSUInteger ii, BOOL *stop) {
1350-
yield(promise, @(ii), ^(id value){
1351-
[results replacePointerAtIndex:ii withPointer:(__bridge void *)(value ?: [NSNull null])];
1352-
});
1353-
}];
1354-
}
1355-
1356-
return rootPromise;
1328+
if (![promise isKindOfClass:[AnyPromise class]])
1329+
promise = [AnyPromise promiseWithValue:promise];
1330+
1331+
[promise pipe:^(id value){
1332+
if (progress.fractionCompleted >= 1)
1333+
return;
1334+
1335+
if (IsError(value)) {
1336+
progress.completedUnitCount = progress.totalUnitCount;
1337+
resolve(NSErrorSupplement(value, @{PMKFailingPromiseIndexKey: key}));
1338+
}
1339+
else if (OSAtomicDecrement32(&countdown) == 0) {
1340+
progress.completedUnitCount = progress.totalUnitCount;
1341+
1342+
id results;
1343+
if (isdict) {
1344+
results = [NSMutableDictionary new];
1345+
for (id key in promises) {
1346+
id promise = promises[key];
1347+
results[key] = IsPromise(promise) ? ((AnyPromise *)promise).value : promise;
1348+
}
1349+
} else {
1350+
results = [NSMutableArray new];
1351+
for (AnyPromise *promise in promises) {
1352+
id value = IsPromise(promise) ? (promise.value ?: [NSNull null]) : promise;
1353+
[results addObject:value];
1354+
}
1355+
}
1356+
resolve(results);
1357+
} else {
1358+
progress.completedUnitCount++;
1359+
}
1360+
}];
1361+
}
1362+
}];
13571363
}

PromiseKit.swift

+52-3
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,46 @@ public typealias AnyPromise = PMKPromise
273273
}
274274
}
275275
}
276+
277+
/**
278+
Continue a Promise<T> chain from an AnyPromise.
279+
*/
280+
public func then(on q: dispatch_queue_t = dispatch_get_main_queue(), body: (AnyObject?) -> AnyPromise) -> Promise<AnyObject?> {
281+
return Promise { fulfill, reject in
282+
pipe { object in
283+
if let error = object as? NSError {
284+
reject(error)
285+
} else {
286+
contain_zalgo(q) {
287+
body(object).pipe { object in
288+
if let error = object as? NSError {
289+
reject(error)
290+
} else {
291+
fulfill(object)
292+
}
293+
}
294+
}
295+
}
296+
}
297+
}
298+
}
299+
300+
/**
301+
Continue a Promise<T> chain from an AnyPromise.
302+
*/
303+
public func then<T>(on q: dispatch_queue_t = dispatch_get_main_queue(), body: (AnyObject?) -> Promise<T>) -> Promise<T> {
304+
return Promise(passthru: { resolve in
305+
pipe { object in
306+
if let error = object as? NSError {
307+
resolve(.Rejected(error))
308+
} else {
309+
contain_zalgo(q) {
310+
body(object).pipe(resolve)
311+
}
312+
}
313+
}
314+
})
315+
}
276316
}
277317

278318

@@ -626,7 +666,7 @@ public class Promise<T> {
626666
public designated unsealed initializer! Making this convenience would be
627667
inefficient. Not very inefficient, but still it seems distasteful to me.
628668
*/
629-
init(passthru: ((Resolution) -> Void) -> Void) {
669+
init(@noescape passthru: ((Resolution) -> Void) -> Void) {
630670
var resolve: ((Resolution) -> Void)!
631671
state = UnsealedState(resolver: &resolve)
632672
passthru(resolve)
@@ -986,6 +1026,12 @@ public func firstly<T>(promise: () -> Promise<T>) -> Promise<T> {
9861026
return promise()
9871027
}
9881028

1029+
1030+
public enum ErrorPolicy {
1031+
case AllErrors
1032+
case AllErrorsExceptCancellation
1033+
}
1034+
9891035
public func race<T>(promises: Promise<T>...) -> Promise<T> {
9901036
return Promise(passthru: { resolve in
9911037
for promise in promises {
@@ -1244,15 +1290,18 @@ private func when<T>(promises: [Promise<T>]) -> Promise<Void> {
12441290
var countdown = promises.count
12451291
if countdown == 0 {
12461292
fulfill()
1293+
return rootPromise
12471294
}
1295+
let barrier = dispatch_queue_create("org.promisekit.barrier.when", DISPATCH_QUEUE_CONCURRENT)
12481296

12491297
for (index, promise) in enumerate(promises) {
12501298
promise.pipe { resolution in
1251-
if rootPromise.pending {
1299+
if !rootPromise.pending { return }
1300+
1301+
dispatch_barrier_sync(barrier) {
12521302
switch resolution {
12531303
case .Rejected(let error):
12541304
progress.completedUnitCount = progress.totalUnitCount
1255-
//TODO PMKFailingPromiseIndexKey
12561305
reject(error)
12571306
case .Fulfilled:
12581307
progress.completedUnitCount++

0 commit comments

Comments
 (0)