firebase log level

This commit is contained in:
oscarz
2024-08-29 18:25:13 +08:00
parent 8500300d18
commit 27c160beaf
1165 changed files with 122916 additions and 1 deletions

View File

@ -0,0 +1,89 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise+All.h"
#import "FBLPromise+Async.h"
#import "FBLPromisePrivate.h"
@implementation FBLPromise (AllAdditions)
+ (FBLPromise<NSArray *> *)all:(NSArray *)promises {
return [self onQueue:self.defaultDispatchQueue all:promises];
}
+ (FBLPromise<NSArray *> *)onQueue:(dispatch_queue_t)queue all:(NSArray *)allPromises {
NSParameterAssert(queue);
NSParameterAssert(allPromises);
if (allPromises.count == 0) {
return [[self alloc] initWithResolution:@[]];
}
NSMutableArray *promises = [allPromises mutableCopy];
return [self
onQueue:queue
async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock reject) {
for (NSUInteger i = 0; i < promises.count; ++i) {
id promise = promises[i];
if ([promise isKindOfClass:self]) {
continue;
} else if ([promise isKindOfClass:[NSError class]]) {
reject(promise);
return;
} else {
[promises replaceObjectAtIndex:i
withObject:[[self alloc] initWithResolution:promise]];
}
}
for (FBLPromise *promise in promises) {
[promise observeOnQueue:queue
fulfill:^(id __unused _) {
// Wait until all are fulfilled.
for (FBLPromise *promise in promises) {
if (!promise.isFulfilled) {
return;
}
}
// If called multiple times, only the first one affects the result.
fulfill([promises valueForKey:NSStringFromSelector(@selector(value))]);
}
reject:^(NSError *error) {
reject(error);
}];
}
}];
}
@end
@implementation FBLPromise (DotSyntax_AllAdditions)
+ (FBLPromise<NSArray *> * (^)(NSArray *))all {
return ^(NSArray<FBLPromise *> *promises) {
return [self all:promises];
};
}
+ (FBLPromise<NSArray *> * (^)(dispatch_queue_t, NSArray *))allOn {
return ^(dispatch_queue_t queue, NSArray<FBLPromise *> *promises) {
return [self onQueue:queue all:promises];
};
}
@end
/** Stub used to force the linker to include the categories in this file. */
void FBLIncludeAllCategory(void) {}

View File

@ -0,0 +1,61 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise+Always.h"
#import "FBLPromisePrivate.h"
@implementation FBLPromise (AlwaysAdditions)
- (FBLPromise *)always:(FBLPromiseAlwaysWorkBlock)work {
return [self onQueue:FBLPromise.defaultDispatchQueue always:work];
}
- (FBLPromise *)onQueue:(dispatch_queue_t)queue always:(FBLPromiseAlwaysWorkBlock)work {
NSParameterAssert(queue);
NSParameterAssert(work);
return [self chainOnQueue:queue
chainedFulfill:^id(id value) {
work();
return value;
}
chainedReject:^id(NSError *error) {
work();
return error;
}];
}
@end
@implementation FBLPromise (DotSyntax_AlwaysAdditions)
- (FBLPromise * (^)(FBLPromiseAlwaysWorkBlock))always {
return ^(FBLPromiseAlwaysWorkBlock work) {
return [self always:work];
};
}
- (FBLPromise * (^)(dispatch_queue_t, FBLPromiseAlwaysWorkBlock))alwaysOn {
return ^(dispatch_queue_t queue, FBLPromiseAlwaysWorkBlock work) {
return [self onQueue:queue always:work];
};
}
@end
/** Stub used to force the linker to include the categories in this file. */
void FBLIncludeAlwaysCategory(void) {}

View File

@ -0,0 +1,115 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise+Any.h"
#import "FBLPromise+Async.h"
#import "FBLPromisePrivate.h"
static NSArray *FBLPromiseCombineValuesAndErrors(NSArray<FBLPromise *> *promises) {
NSMutableArray *combinedValuesAndErrors = [[NSMutableArray alloc] init];
for (FBLPromise *promise in promises) {
if (promise.isFulfilled) {
[combinedValuesAndErrors addObject:promise.value ?: [NSNull null]];
continue;
}
if (promise.isRejected) {
[combinedValuesAndErrors addObject:promise.error];
continue;
}
assert(!promise.isPending);
};
return combinedValuesAndErrors;
}
@implementation FBLPromise (AnyAdditions)
+ (FBLPromise<NSArray *> *)any:(NSArray *)promises {
return [self onQueue:FBLPromise.defaultDispatchQueue any:promises];
}
+ (FBLPromise<NSArray *> *)onQueue:(dispatch_queue_t)queue any:(NSArray *)anyPromises {
NSParameterAssert(queue);
NSParameterAssert(anyPromises);
if (anyPromises.count == 0) {
return [[self alloc] initWithResolution:@[]];
}
NSMutableArray *promises = [anyPromises mutableCopy];
return [self
onQueue:queue
async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock reject) {
for (NSUInteger i = 0; i < promises.count; ++i) {
id promise = promises[i];
if ([promise isKindOfClass:self]) {
continue;
} else {
[promises replaceObjectAtIndex:i
withObject:[[self alloc] initWithResolution:promise]];
}
}
for (FBLPromise *promise in promises) {
[promise observeOnQueue:queue
fulfill:^(id __unused _) {
// Wait until all are resolved.
for (FBLPromise *promise in promises) {
if (promise.isPending) {
return;
}
}
// If called multiple times, only the first one affects the result.
fulfill(FBLPromiseCombineValuesAndErrors(promises));
}
reject:^(NSError *error) {
BOOL atLeastOneIsFulfilled = NO;
for (FBLPromise *promise in promises) {
if (promise.isPending) {
return;
}
if (promise.isFulfilled) {
atLeastOneIsFulfilled = YES;
}
}
if (atLeastOneIsFulfilled) {
fulfill(FBLPromiseCombineValuesAndErrors(promises));
} else {
reject(error);
}
}];
}
}];
}
@end
@implementation FBLPromise (DotSyntax_AnyAdditions)
+ (FBLPromise<NSArray *> * (^)(NSArray *))any {
return ^(NSArray *promises) {
return [self any:promises];
};
}
+ (FBLPromise<NSArray *> * (^)(dispatch_queue_t, NSArray *))anyOn {
return ^(dispatch_queue_t queue, NSArray *promises) {
return [self onQueue:queue any:promises];
};
}
@end
/** Stub used to force the linker to include the categories in this file. */
void FBLIncludeAnyCategory(void) {}

View File

@ -0,0 +1,73 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise+Async.h"
#import "FBLPromisePrivate.h"
@implementation FBLPromise (AsyncAdditions)
+ (instancetype)async:(FBLPromiseAsyncWorkBlock)work {
return [self onQueue:self.defaultDispatchQueue async:work];
}
+ (instancetype)onQueue:(dispatch_queue_t)queue async:(FBLPromiseAsyncWorkBlock)work {
NSParameterAssert(queue);
NSParameterAssert(work);
FBLPromise *promise = [[self alloc] initPending];
dispatch_group_async(FBLPromise.dispatchGroup, queue, ^{
work(
^(id __nullable value) {
if ([value isKindOfClass:[FBLPromise class]]) {
[(FBLPromise *)value observeOnQueue:queue
fulfill:^(id __nullable value) {
[promise fulfill:value];
}
reject:^(NSError *error) {
[promise reject:error];
}];
} else {
[promise fulfill:value];
}
},
^(NSError *error) {
[promise reject:error];
});
});
return promise;
}
@end
@implementation FBLPromise (DotSyntax_AsyncAdditions)
+ (FBLPromise* (^)(FBLPromiseAsyncWorkBlock))async {
return ^(FBLPromiseAsyncWorkBlock work) {
return [self async:work];
};
}
+ (FBLPromise* (^)(dispatch_queue_t, FBLPromiseAsyncWorkBlock))asyncOn {
return ^(dispatch_queue_t queue, FBLPromiseAsyncWorkBlock work) {
return [self onQueue:queue async:work];
};
}
@end
/** Stub used to force the linker to include the categories in this file. */
void FBLIncludeAsyncCategory(void) {}

View File

@ -0,0 +1,51 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise+Await.h"
#import "FBLPromisePrivate.h"
id __nullable FBLPromiseAwait(FBLPromise *promise, NSError **outError) {
assert(promise);
static dispatch_once_t onceToken;
static dispatch_queue_t queue;
dispatch_once(&onceToken, ^{
queue = dispatch_queue_create("com.google.FBLPromises.Await", DISPATCH_QUEUE_CONCURRENT);
});
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
id __block resolution;
NSError __block *blockError;
[promise chainOnQueue:queue
chainedFulfill:^id(id value) {
resolution = value;
dispatch_semaphore_signal(semaphore);
return value;
}
chainedReject:^id(NSError *error) {
blockError = error;
dispatch_semaphore_signal(semaphore);
return error;
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
if (outError) {
*outError = blockError;
}
return resolution;
}
/** Stub used to force the linker to include the categories in this file. */
void FBLIncludeAwaitCategory(void) {}

View File

@ -0,0 +1,58 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise+Catch.h"
#import "FBLPromisePrivate.h"
@implementation FBLPromise (CatchAdditions)
- (FBLPromise *)catch:(FBLPromiseCatchWorkBlock)reject {
return [self onQueue:FBLPromise.defaultDispatchQueue catch:reject];
}
- (FBLPromise *)onQueue:(dispatch_queue_t)queue catch:(FBLPromiseCatchWorkBlock)reject {
NSParameterAssert(queue);
NSParameterAssert(reject);
return [self chainOnQueue:queue
chainedFulfill:nil
chainedReject:^id(NSError *error) {
reject(error);
return error;
}];
}
@end
@implementation FBLPromise (DotSyntax_CatchAdditions)
- (FBLPromise* (^)(FBLPromiseCatchWorkBlock))catch {
return ^(FBLPromiseCatchWorkBlock catch) {
return [self catch:catch];
};
}
- (FBLPromise* (^)(dispatch_queue_t, FBLPromiseCatchWorkBlock))catchOn {
return ^(dispatch_queue_t queue, FBLPromiseCatchWorkBlock catch) {
return [self onQueue:queue catch:catch];
};
}
@end
/** Stub used to force the linker to include the categories in this file. */
void FBLIncludeCatchCategory(void) {}

View File

@ -0,0 +1,62 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise+Delay.h"
#import "FBLPromisePrivate.h"
@implementation FBLPromise (DelayAdditions)
- (FBLPromise *)delay:(NSTimeInterval)interval {
return [self onQueue:FBLPromise.defaultDispatchQueue delay:interval];
}
- (FBLPromise *)onQueue:(dispatch_queue_t)queue delay:(NSTimeInterval)interval {
NSParameterAssert(queue);
FBLPromise *promise = [[[self class] alloc] initPending];
[self observeOnQueue:queue
fulfill:^(id __nullable value) {
dispatch_after(dispatch_time(0, (int64_t)(interval * NSEC_PER_SEC)), queue, ^{
[promise fulfill:value];
});
}
reject:^(NSError *error) {
[promise reject:error];
}];
return promise;
}
@end
@implementation FBLPromise (DotSyntax_DelayAdditions)
- (FBLPromise * (^)(NSTimeInterval))delay {
return ^(NSTimeInterval interval) {
return [self delay:interval];
};
}
- (FBLPromise * (^)(dispatch_queue_t, NSTimeInterval))delayOn {
return ^(dispatch_queue_t queue, NSTimeInterval interval) {
return [self onQueue:queue delay:interval];
};
}
@end
/** Stub used to force the linker to include the categories in this file. */
void FBLIncludeDelayCategory(void) {}

View File

@ -0,0 +1,62 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise+Do.h"
#import "FBLPromisePrivate.h"
@implementation FBLPromise (DoAdditions)
+ (instancetype)do:(FBLPromiseDoWorkBlock)work {
return [self onQueue:self.defaultDispatchQueue do:work];
}
+ (instancetype)onQueue:(dispatch_queue_t)queue do:(FBLPromiseDoWorkBlock)work {
NSParameterAssert(queue);
NSParameterAssert(work);
FBLPromise *promise = [[self alloc] initPending];
dispatch_group_async(FBLPromise.dispatchGroup, queue, ^{
id value = work();
if ([value isKindOfClass:[FBLPromise class]]) {
[(FBLPromise *)value observeOnQueue:queue
fulfill:^(id __nullable value) {
[promise fulfill:value];
}
reject:^(NSError *error) {
[promise reject:error];
}];
} else {
[promise fulfill:value];
}
});
return promise;
}
@end
@implementation FBLPromise (DotSyntax_DoAdditions)
+ (FBLPromise* (^)(dispatch_queue_t, FBLPromiseDoWorkBlock))doOn {
return ^(dispatch_queue_t queue, FBLPromiseDoWorkBlock work) {
return [self onQueue:queue do:work];
};
}
@end
/** Stub used to force the linker to include the categories in this file. */
void FBLIncludeDoCategory(void) {}

View File

@ -0,0 +1,68 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise+Race.h"
#import "FBLPromise+Async.h"
#import "FBLPromisePrivate.h"
@implementation FBLPromise (RaceAdditions)
+ (instancetype)race:(NSArray *)promises {
return [self onQueue:self.defaultDispatchQueue race:promises];
}
+ (instancetype)onQueue:(dispatch_queue_t)queue race:(NSArray *)racePromises {
NSParameterAssert(queue);
NSAssert(racePromises.count > 0, @"No promises to observe");
NSArray *promises = [racePromises copy];
return [self onQueue:queue
async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock reject) {
for (id promise in promises) {
if (![promise isKindOfClass:self]) {
fulfill(promise);
return;
}
}
// Subscribe all, but only the first one to resolve will change
// the resulting promise's state.
for (FBLPromise *promise in promises) {
[promise observeOnQueue:queue fulfill:fulfill reject:reject];
}
}];
}
@end
@implementation FBLPromise (DotSyntax_RaceAdditions)
+ (FBLPromise * (^)(NSArray *))race {
return ^(NSArray *promises) {
return [self race:promises];
};
}
+ (FBLPromise * (^)(dispatch_queue_t, NSArray *))raceOn {
return ^(dispatch_queue_t queue, NSArray *promises) {
return [self onQueue:queue race:promises];
};
}
@end
/** Stub used to force the linker to include the categories in this file. */
void FBLIncludeRaceCategory(void) {}

View File

@ -0,0 +1,57 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise+Recover.h"
#import "FBLPromisePrivate.h"
@implementation FBLPromise (RecoverAdditions)
- (FBLPromise *)recover:(FBLPromiseRecoverWorkBlock)recovery {
return [self onQueue:FBLPromise.defaultDispatchQueue recover:recovery];
}
- (FBLPromise *)onQueue:(dispatch_queue_t)queue recover:(FBLPromiseRecoverWorkBlock)recovery {
NSParameterAssert(queue);
NSParameterAssert(recovery);
return [self chainOnQueue:queue
chainedFulfill:nil
chainedReject:^id(NSError *error) {
return recovery(error);
}];
}
@end
@implementation FBLPromise (DotSyntax_RecoverAdditions)
- (FBLPromise * (^)(FBLPromiseRecoverWorkBlock))recover {
return ^(FBLPromiseRecoverWorkBlock recovery) {
return [self recover:recovery];
};
}
- (FBLPromise * (^)(dispatch_queue_t, FBLPromiseRecoverWorkBlock))recoverOn {
return ^(dispatch_queue_t queue, FBLPromiseRecoverWorkBlock recovery) {
return [self onQueue:queue recover:recovery];
};
}
@end
/** Stub used to force the linker to include the categories in this file. */
void FBLIncludeRecoverCategory(void) {}

View File

@ -0,0 +1,64 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise+Reduce.h"
#import "FBLPromisePrivate.h"
@implementation FBLPromise (ReduceAdditions)
- (FBLPromise *)reduce:(NSArray *)items combine:(FBLPromiseReducerBlock)reducer {
return [self onQueue:FBLPromise.defaultDispatchQueue reduce:items combine:reducer];
}
- (FBLPromise *)onQueue:(dispatch_queue_t)queue
reduce:(NSArray *)items
combine:(FBLPromiseReducerBlock)reducer {
NSParameterAssert(queue);
NSParameterAssert(items);
NSParameterAssert(reducer);
FBLPromise *promise = self;
for (id item in items) {
promise = [promise chainOnQueue:queue
chainedFulfill:^id(id value) {
return reducer(value, item);
}
chainedReject:nil];
}
return promise;
}
@end
@implementation FBLPromise (DotSyntax_ReduceAdditions)
- (FBLPromise * (^)(NSArray *, FBLPromiseReducerBlock))reduce {
return ^(NSArray *items, FBLPromiseReducerBlock reducer) {
return [self reduce:items combine:reducer];
};
}
- (FBLPromise * (^)(dispatch_queue_t, NSArray *, FBLPromiseReducerBlock))reduceOn {
return ^(dispatch_queue_t queue, NSArray *items, FBLPromiseReducerBlock reducer) {
return [self onQueue:queue reduce:items combine:reducer];
};
}
@end
/** Stub used to force the linker to include the categories in this file. */
void FBLIncludeReduceCategory(void) {}

View File

@ -0,0 +1,131 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise+Retry.h"
#import "FBLPromisePrivate.h"
NSInteger const FBLPromiseRetryDefaultAttemptsCount = 1;
NSTimeInterval const FBLPromiseRetryDefaultDelayInterval = 1.0;
static void FBLPromiseRetryAttempt(FBLPromise *promise, dispatch_queue_t queue, NSInteger count,
NSTimeInterval interval, FBLPromiseRetryPredicateBlock predicate,
FBLPromiseRetryWorkBlock work) {
__auto_type retrier = ^(id __nullable value) {
if ([value isKindOfClass:[NSError class]]) {
if (count <= 0 || (predicate && !predicate(count, value))) {
[promise reject:value];
} else {
dispatch_after(dispatch_time(0, (int64_t)(interval * NSEC_PER_SEC)), queue, ^{
FBLPromiseRetryAttempt(promise, queue, count - 1, interval, predicate, work);
});
}
} else {
[promise fulfill:value];
}
};
id value = work();
if ([value isKindOfClass:[FBLPromise class]]) {
[(FBLPromise *)value observeOnQueue:queue fulfill:retrier reject:retrier];
} else {
retrier(value);
}
}
@implementation FBLPromise (RetryAdditions)
+ (instancetype)retry:(FBLPromiseRetryWorkBlock)work {
return [self onQueue:FBLPromise.defaultDispatchQueue retry:work];
}
+ (instancetype)onQueue:(dispatch_queue_t)queue retry:(FBLPromiseRetryWorkBlock)work {
return [self onQueue:queue attempts:FBLPromiseRetryDefaultAttemptsCount retry:work];
}
+ (instancetype)attempts:(NSInteger)count retry:(FBLPromiseRetryWorkBlock)work {
return [self onQueue:FBLPromise.defaultDispatchQueue attempts:count retry:work];
}
+ (instancetype)onQueue:(dispatch_queue_t)queue
attempts:(NSInteger)count
retry:(FBLPromiseRetryWorkBlock)work {
return [self onQueue:queue
attempts:count
delay:FBLPromiseRetryDefaultDelayInterval
condition:nil
retry:work];
}
+ (instancetype)attempts:(NSInteger)count
delay:(NSTimeInterval)interval
condition:(nullable FBLPromiseRetryPredicateBlock)predicate
retry:(FBLPromiseRetryWorkBlock)work {
return [self onQueue:FBLPromise.defaultDispatchQueue
attempts:count
delay:interval
condition:predicate
retry:work];
}
+ (instancetype)onQueue:(dispatch_queue_t)queue
attempts:(NSInteger)count
delay:(NSTimeInterval)interval
condition:(nullable FBLPromiseRetryPredicateBlock)predicate
retry:(FBLPromiseRetryWorkBlock)work {
NSParameterAssert(queue);
NSParameterAssert(work);
FBLPromise *promise = [[self alloc] initPending];
FBLPromiseRetryAttempt(promise, queue, count, interval, predicate, work);
return promise;
}
@end
@implementation FBLPromise (DotSyntax_RetryAdditions)
+ (FBLPromise * (^)(FBLPromiseRetryWorkBlock))retry {
return ^id(FBLPromiseRetryWorkBlock work) {
return [self retry:work];
};
}
+ (FBLPromise * (^)(dispatch_queue_t, FBLPromiseRetryWorkBlock))retryOn {
return ^id(dispatch_queue_t queue, FBLPromiseRetryWorkBlock work) {
return [self onQueue:queue retry:work];
};
}
+ (FBLPromise * (^)(NSInteger, NSTimeInterval, FBLPromiseRetryPredicateBlock,
FBLPromiseRetryWorkBlock))retryAgain {
return ^id(NSInteger count, NSTimeInterval interval, FBLPromiseRetryPredicateBlock predicate,
FBLPromiseRetryWorkBlock work) {
return [self attempts:count delay:interval condition:predicate retry:work];
};
}
+ (FBLPromise * (^)(dispatch_queue_t, NSInteger, NSTimeInterval, FBLPromiseRetryPredicateBlock,
FBLPromiseRetryWorkBlock))retryAgainOn {
return ^id(dispatch_queue_t queue, NSInteger count, NSTimeInterval interval,
FBLPromiseRetryPredicateBlock predicate, FBLPromiseRetryWorkBlock work) {
return [self onQueue:queue attempts:count delay:interval condition:predicate retry:work];
};
}
@end
/** Stub used to force the linker to include the categories in this file. */
void FBLIncludeRetryCategory(void) {}

View File

@ -0,0 +1,58 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise+Testing.h"
BOOL FBLWaitForPromisesWithTimeout(NSTimeInterval timeout) {
BOOL isTimedOut = NO;
NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeout];
static NSTimeInterval const minimalTimeout = 0.01;
static int64_t const minimalTimeToWait = (int64_t)(minimalTimeout * NSEC_PER_SEC);
dispatch_time_t waitTime = dispatch_time(DISPATCH_TIME_NOW, minimalTimeToWait);
dispatch_group_t dispatchGroup = FBLPromise.dispatchGroup;
NSRunLoop *runLoop = NSRunLoop.currentRunLoop;
while (dispatch_group_wait(dispatchGroup, waitTime)) {
isTimedOut = timeoutDate.timeIntervalSinceNow < 0.0;
if (isTimedOut) {
break;
}
[runLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:minimalTimeout]];
}
return !isTimedOut;
}
@implementation FBLPromise (TestingAdditions)
// These properties are implemented in the FBLPromise class itself.
@dynamic isPending;
@dynamic isFulfilled;
@dynamic isRejected;
@dynamic value;
@dynamic error;
+ (dispatch_group_t)dispatchGroup {
static dispatch_group_t gDispatchGroup;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
gDispatchGroup = dispatch_group_create();
});
return gDispatchGroup;
}
@end
/** Stub used to force the linker to include the categories in this file. */
void FBLIncludeTestingCategory(void) {}

View File

@ -0,0 +1,53 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise+Then.h"
#import "FBLPromisePrivate.h"
@implementation FBLPromise (ThenAdditions)
- (FBLPromise *)then:(FBLPromiseThenWorkBlock)work {
return [self onQueue:FBLPromise.defaultDispatchQueue then:work];
}
- (FBLPromise *)onQueue:(dispatch_queue_t)queue then:(FBLPromiseThenWorkBlock)work {
NSParameterAssert(queue);
NSParameterAssert(work);
return [self chainOnQueue:queue chainedFulfill:work chainedReject:nil];
}
@end
@implementation FBLPromise (DotSyntax_ThenAdditions)
- (FBLPromise* (^)(FBLPromiseThenWorkBlock))then {
return ^(FBLPromiseThenWorkBlock work) {
return [self then:work];
};
}
- (FBLPromise* (^)(dispatch_queue_t, FBLPromiseThenWorkBlock))thenOn {
return ^(dispatch_queue_t queue, FBLPromiseThenWorkBlock work) {
return [self onQueue:queue then:work];
};
}
@end
/** Stub used to force the linker to include the categories in this file. */
void FBLIncludeThenCategory(void) {}

View File

@ -0,0 +1,67 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise+Timeout.h"
#import "FBLPromisePrivate.h"
@implementation FBLPromise (TimeoutAdditions)
- (FBLPromise *)timeout:(NSTimeInterval)interval {
return [self onQueue:FBLPromise.defaultDispatchQueue timeout:interval];
}
- (FBLPromise *)onQueue:(dispatch_queue_t)queue timeout:(NSTimeInterval)interval {
NSParameterAssert(queue);
FBLPromise *promise = [[[self class] alloc] initPending];
[self observeOnQueue:queue
fulfill:^(id __nullable value) {
[promise fulfill:value];
}
reject:^(NSError *error) {
[promise reject:error];
}];
FBLPromise* __weak weakPromise = promise;
dispatch_after(dispatch_time(0, (int64_t)(interval * NSEC_PER_SEC)), queue, ^{
NSError *timedOutError = [[NSError alloc] initWithDomain:FBLPromiseErrorDomain
code:FBLPromiseErrorCodeTimedOut
userInfo:nil];
[weakPromise reject:timedOutError];
});
return promise;
}
@end
@implementation FBLPromise (DotSyntax_TimeoutAdditions)
- (FBLPromise* (^)(NSTimeInterval))timeout {
return ^(NSTimeInterval interval) {
return [self timeout:interval];
};
}
- (FBLPromise* (^)(dispatch_queue_t, NSTimeInterval))timeoutOn {
return ^(dispatch_queue_t queue, NSTimeInterval interval) {
return [self onQueue:queue timeout:interval];
};
}
@end
/** Stub used to force the linker to include the categories in this file. */
void FBLIncludeTimeoutCategory(void) {}

View File

@ -0,0 +1,59 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise+Validate.h"
#import "FBLPromisePrivate.h"
@implementation FBLPromise (ValidateAdditions)
- (FBLPromise*)validate:(FBLPromiseValidateWorkBlock)predicate {
return [self onQueue:FBLPromise.defaultDispatchQueue validate:predicate];
}
- (FBLPromise*)onQueue:(dispatch_queue_t)queue validate:(FBLPromiseValidateWorkBlock)predicate {
NSParameterAssert(queue);
NSParameterAssert(predicate);
FBLPromiseChainedFulfillBlock chainedFulfill = ^id(id value) {
return predicate(value) ? value :
[[NSError alloc] initWithDomain:FBLPromiseErrorDomain
code:FBLPromiseErrorCodeValidationFailure
userInfo:nil];
};
return [self chainOnQueue:queue chainedFulfill:chainedFulfill chainedReject:nil];
}
@end
@implementation FBLPromise (DotSyntax_ValidateAdditions)
- (FBLPromise* (^)(FBLPromiseValidateWorkBlock))validate {
return ^(FBLPromiseValidateWorkBlock predicate) {
return [self validate:predicate];
};
}
- (FBLPromise* (^)(dispatch_queue_t, FBLPromiseValidateWorkBlock))validateOn {
return ^(dispatch_queue_t queue, FBLPromiseValidateWorkBlock predicate) {
return [self onQueue:queue validate:predicate];
};
}
@end
/** Stub used to force the linker to include the categories in this file. */
void FBLIncludeValidateCategory(void) {}

View File

@ -0,0 +1,423 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise+Wrap.h"
#import "FBLPromise+Async.h"
@implementation FBLPromise (WrapAdditions)
+ (instancetype)wrapCompletion:(void (^)(FBLPromiseCompletion))work {
return [self onQueue:self.defaultDispatchQueue wrapCompletion:work];
}
+ (instancetype)onQueue:(dispatch_queue_t)queue
wrapCompletion:(void (^)(FBLPromiseCompletion))work {
NSParameterAssert(queue);
NSParameterAssert(work);
return [self onQueue:queue
async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock __unused _) {
work(^{
fulfill(nil);
});
}];
}
+ (instancetype)wrapObjectCompletion:(void (^)(FBLPromiseObjectCompletion))work {
return [self onQueue:self.defaultDispatchQueue wrapObjectCompletion:work];
}
+ (instancetype)onQueue:(dispatch_queue_t)queue
wrapObjectCompletion:(void (^)(FBLPromiseObjectCompletion))work {
NSParameterAssert(queue);
NSParameterAssert(work);
return [self onQueue:queue
async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock __unused _) {
work(^(id __nullable value) {
fulfill(value);
});
}];
}
+ (instancetype)wrapErrorCompletion:(void (^)(FBLPromiseErrorCompletion))work {
return [self onQueue:self.defaultDispatchQueue wrapErrorCompletion:work];
}
+ (instancetype)onQueue:(dispatch_queue_t)queue
wrapErrorCompletion:(void (^)(FBLPromiseErrorCompletion))work {
NSParameterAssert(queue);
NSParameterAssert(work);
return [self onQueue:queue
async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock reject) {
work(^(NSError *__nullable error) {
if (error) {
reject(error);
} else {
fulfill(nil);
}
});
}];
}
+ (instancetype)wrapObjectOrErrorCompletion:(void (^)(FBLPromiseObjectOrErrorCompletion))work {
return [self onQueue:self.defaultDispatchQueue wrapObjectOrErrorCompletion:work];
}
+ (instancetype)onQueue:(dispatch_queue_t)queue
wrapObjectOrErrorCompletion:(void (^)(FBLPromiseObjectOrErrorCompletion))work {
NSParameterAssert(queue);
NSParameterAssert(work);
return [self onQueue:queue
async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock reject) {
work(^(id __nullable value, NSError *__nullable error) {
if (error) {
reject(error);
} else {
fulfill(value);
}
});
}];
}
+ (instancetype)wrapErrorOrObjectCompletion:(void (^)(FBLPromiseErrorOrObjectCompletion))work {
return [self onQueue:self.defaultDispatchQueue wrapErrorOrObjectCompletion:work];
}
+ (instancetype)onQueue:(dispatch_queue_t)queue
wrapErrorOrObjectCompletion:(void (^)(FBLPromiseErrorOrObjectCompletion))work {
NSParameterAssert(queue);
NSParameterAssert(work);
return [self onQueue:queue
async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock reject) {
work(^(NSError *__nullable error, id __nullable value) {
if (error) {
reject(error);
} else {
fulfill(value);
}
});
}];
}
+ (FBLPromise<NSArray *> *)wrap2ObjectsOrErrorCompletion:
(void (^)(FBLPromise2ObjectsOrErrorCompletion))work {
return [self onQueue:self.defaultDispatchQueue wrap2ObjectsOrErrorCompletion:work];
}
+ (FBLPromise<NSArray *> *)onQueue:(dispatch_queue_t)queue
wrap2ObjectsOrErrorCompletion:(void (^)(FBLPromise2ObjectsOrErrorCompletion))work {
NSParameterAssert(queue);
NSParameterAssert(work);
return [self onQueue:queue
async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock reject) {
work(^(id __nullable value1, id __nullable value2, NSError *__nullable error) {
if (error) {
reject(error);
} else {
fulfill(@[ value1 ?: [NSNull null], value2 ?: [NSNull null] ]);
}
});
}];
}
+ (FBLPromise<NSNumber *> *)wrapBoolCompletion:(void (^)(FBLPromiseBoolCompletion))work {
return [self onQueue:self.defaultDispatchQueue wrapBoolCompletion:work];
}
+ (FBLPromise<NSNumber *> *)onQueue:(dispatch_queue_t)queue
wrapBoolCompletion:(void (^)(FBLPromiseBoolCompletion))work {
NSParameterAssert(queue);
NSParameterAssert(work);
return [self onQueue:queue
async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock __unused _) {
work(^(BOOL value) {
fulfill(@(value));
});
}];
}
+ (FBLPromise<NSNumber *> *)wrapBoolOrErrorCompletion:
(void (^)(FBLPromiseBoolOrErrorCompletion))work {
return [self onQueue:self.defaultDispatchQueue wrapBoolOrErrorCompletion:work];
}
+ (FBLPromise<NSNumber *> *)onQueue:(dispatch_queue_t)queue
wrapBoolOrErrorCompletion:(void (^)(FBLPromiseBoolOrErrorCompletion))work {
NSParameterAssert(queue);
NSParameterAssert(work);
return [self onQueue:queue
async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock reject) {
work(^(BOOL value, NSError *__nullable error) {
if (error) {
reject(error);
} else {
fulfill(@(value));
}
});
}];
}
+ (FBLPromise<NSNumber *> *)wrapIntegerCompletion:(void (^)(FBLPromiseIntegerCompletion))work {
return [self onQueue:self.defaultDispatchQueue wrapIntegerCompletion:work];
}
+ (FBLPromise<NSNumber *> *)onQueue:(dispatch_queue_t)queue
wrapIntegerCompletion:(void (^)(FBLPromiseIntegerCompletion))work {
NSParameterAssert(queue);
NSParameterAssert(work);
return [self onQueue:queue
async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock __unused _) {
work(^(NSInteger value) {
fulfill(@(value));
});
}];
}
+ (FBLPromise<NSNumber *> *)wrapIntegerOrErrorCompletion:
(void (^)(FBLPromiseIntegerOrErrorCompletion))work {
return [self onQueue:self.defaultDispatchQueue wrapIntegerOrErrorCompletion:work];
}
+ (FBLPromise<NSNumber *> *)onQueue:(dispatch_queue_t)queue
wrapIntegerOrErrorCompletion:(void (^)(FBLPromiseIntegerOrErrorCompletion))work {
NSParameterAssert(queue);
NSParameterAssert(work);
return [self onQueue:queue
async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock reject) {
work(^(NSInteger value, NSError *__nullable error) {
if (error) {
reject(error);
} else {
fulfill(@(value));
}
});
}];
}
+ (FBLPromise<NSNumber *> *)wrapDoubleCompletion:(void (^)(FBLPromiseDoubleCompletion))work {
return [self onQueue:self.defaultDispatchQueue wrapDoubleCompletion:work];
}
+ (FBLPromise<NSNumber *> *)onQueue:(dispatch_queue_t)queue
wrapDoubleCompletion:(void (^)(FBLPromiseDoubleCompletion))work {
NSParameterAssert(queue);
NSParameterAssert(work);
return [self onQueue:(dispatch_queue_t)queue
async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock __unused _) {
work(^(double value) {
fulfill(@(value));
});
}];
}
+ (FBLPromise<NSNumber *> *)wrapDoubleOrErrorCompletion:
(void (^)(FBLPromiseDoubleOrErrorCompletion))work {
return [self onQueue:self.defaultDispatchQueue wrapDoubleOrErrorCompletion:work];
}
+ (FBLPromise<NSNumber *> *)onQueue:(dispatch_queue_t)queue
wrapDoubleOrErrorCompletion:(void (^)(FBLPromiseDoubleOrErrorCompletion))work {
NSParameterAssert(queue);
NSParameterAssert(work);
return [self onQueue:queue
async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock reject) {
work(^(double value, NSError *__nullable error) {
if (error) {
reject(error);
} else {
fulfill(@(value));
}
});
}];
}
@end
@implementation FBLPromise (DotSyntax_WrapAdditions)
+ (FBLPromise * (^)(void (^)(FBLPromiseCompletion)))wrapCompletion {
return ^(void (^work)(FBLPromiseCompletion)) {
return [self wrapCompletion:work];
};
}
+ (FBLPromise * (^)(dispatch_queue_t, void (^)(FBLPromiseCompletion)))wrapCompletionOn {
return ^(dispatch_queue_t queue, void (^work)(FBLPromiseCompletion)) {
return [self onQueue:queue wrapCompletion:work];
};
}
+ (FBLPromise * (^)(void (^)(FBLPromiseObjectCompletion)))wrapObjectCompletion {
return ^(void (^work)(FBLPromiseObjectCompletion)) {
return [self wrapObjectCompletion:work];
};
}
+ (FBLPromise * (^)(dispatch_queue_t, void (^)(FBLPromiseObjectCompletion)))wrapObjectCompletionOn {
return ^(dispatch_queue_t queue, void (^work)(FBLPromiseObjectCompletion)) {
return [self onQueue:queue wrapObjectCompletion:work];
};
}
+ (FBLPromise * (^)(void (^)(FBLPromiseErrorCompletion)))wrapErrorCompletion {
return ^(void (^work)(FBLPromiseErrorCompletion)) {
return [self wrapErrorCompletion:work];
};
}
+ (FBLPromise * (^)(dispatch_queue_t, void (^)(FBLPromiseErrorCompletion)))wrapErrorCompletionOn {
return ^(dispatch_queue_t queue, void (^work)(FBLPromiseErrorCompletion)) {
return [self onQueue:queue wrapErrorCompletion:work];
};
}
+ (FBLPromise * (^)(void (^)(FBLPromiseObjectOrErrorCompletion)))wrapObjectOrErrorCompletion {
return ^(void (^work)(FBLPromiseObjectOrErrorCompletion)) {
return [self wrapObjectOrErrorCompletion:work];
};
}
+ (FBLPromise * (^)(dispatch_queue_t,
void (^)(FBLPromiseObjectOrErrorCompletion)))wrapObjectOrErrorCompletionOn {
return ^(dispatch_queue_t queue, void (^work)(FBLPromiseObjectOrErrorCompletion)) {
return [self onQueue:queue wrapObjectOrErrorCompletion:work];
};
}
+ (FBLPromise * (^)(void (^)(FBLPromiseErrorOrObjectCompletion)))wrapErrorOrObjectCompletion {
return ^(void (^work)(FBLPromiseErrorOrObjectCompletion)) {
return [self wrapErrorOrObjectCompletion:work];
};
}
+ (FBLPromise * (^)(dispatch_queue_t,
void (^)(FBLPromiseErrorOrObjectCompletion)))wrapErrorOrObjectCompletionOn {
return ^(dispatch_queue_t queue, void (^work)(FBLPromiseErrorOrObjectCompletion)) {
return [self onQueue:queue wrapErrorOrObjectCompletion:work];
};
}
+ (FBLPromise<NSArray *> * (^)(void (^)(FBLPromise2ObjectsOrErrorCompletion)))
wrap2ObjectsOrErrorCompletion {
return ^(void (^work)(FBLPromise2ObjectsOrErrorCompletion)) {
return [self wrap2ObjectsOrErrorCompletion:work];
};
}
+ (FBLPromise<NSArray *> * (^)(dispatch_queue_t, void (^)(FBLPromise2ObjectsOrErrorCompletion)))
wrap2ObjectsOrErrorCompletionOn {
return ^(dispatch_queue_t queue, void (^work)(FBLPromise2ObjectsOrErrorCompletion)) {
return [self onQueue:queue wrap2ObjectsOrErrorCompletion:work];
};
}
+ (FBLPromise<NSNumber *> * (^)(void (^)(FBLPromiseBoolCompletion)))wrapBoolCompletion {
return ^(void (^work)(FBLPromiseBoolCompletion)) {
return [self wrapBoolCompletion:work];
};
}
+ (FBLPromise<NSNumber *> * (^)(dispatch_queue_t,
void (^)(FBLPromiseBoolCompletion)))wrapBoolCompletionOn {
return ^(dispatch_queue_t queue, void (^work)(FBLPromiseBoolCompletion)) {
return [self onQueue:queue wrapBoolCompletion:work];
};
}
+ (FBLPromise<NSNumber *> * (^)(void (^)(FBLPromiseBoolOrErrorCompletion)))
wrapBoolOrErrorCompletion {
return ^(void (^work)(FBLPromiseBoolOrErrorCompletion)) {
return [self wrapBoolOrErrorCompletion:work];
};
}
+ (FBLPromise<NSNumber *> * (^)(dispatch_queue_t, void (^)(FBLPromiseBoolOrErrorCompletion)))
wrapBoolOrErrorCompletionOn {
return ^(dispatch_queue_t queue, void (^work)(FBLPromiseBoolOrErrorCompletion)) {
return [self onQueue:queue wrapBoolOrErrorCompletion:work];
};
}
+ (FBLPromise<NSNumber *> * (^)(void (^)(FBLPromiseIntegerCompletion)))wrapIntegerCompletion {
return ^(void (^work)(FBLPromiseIntegerCompletion)) {
return [self wrapIntegerCompletion:work];
};
}
+ (FBLPromise<NSNumber *> * (^)(dispatch_queue_t,
void (^)(FBLPromiseIntegerCompletion)))wrapIntegerCompletionOn {
return ^(dispatch_queue_t queue, void (^work)(FBLPromiseIntegerCompletion)) {
return [self onQueue:queue wrapIntegerCompletion:work];
};
}
+ (FBLPromise<NSNumber *> * (^)(void (^)(FBLPromiseIntegerOrErrorCompletion)))
wrapIntegerOrErrorCompletion {
return ^(void (^work)(FBLPromiseIntegerOrErrorCompletion)) {
return [self wrapIntegerOrErrorCompletion:work];
};
}
+ (FBLPromise<NSNumber *> * (^)(dispatch_queue_t, void (^)(FBLPromiseIntegerOrErrorCompletion)))
wrapIntegerOrErrorCompletionOn {
return ^(dispatch_queue_t queue, void (^work)(FBLPromiseIntegerOrErrorCompletion)) {
return [self onQueue:queue wrapIntegerOrErrorCompletion:work];
};
}
+ (FBLPromise<NSNumber *> * (^)(void (^)(FBLPromiseDoubleCompletion)))wrapDoubleCompletion {
return ^(void (^work)(FBLPromiseDoubleCompletion)) {
return [self wrapDoubleCompletion:work];
};
}
+ (FBLPromise<NSNumber *> * (^)(dispatch_queue_t,
void (^)(FBLPromiseDoubleCompletion)))wrapDoubleCompletionOn {
return ^(dispatch_queue_t queue, void (^work)(FBLPromiseDoubleCompletion)) {
return [self onQueue:queue wrapDoubleCompletion:work];
};
}
+ (FBLPromise<NSNumber *> * (^)(void (^)(FBLPromiseDoubleOrErrorCompletion)))
wrapDoubleOrErrorCompletion {
return ^(void (^work)(FBLPromiseDoubleOrErrorCompletion)) {
return [self wrapDoubleOrErrorCompletion:work];
};
}
+ (FBLPromise<NSNumber *> * (^)(dispatch_queue_t, void (^)(FBLPromiseDoubleOrErrorCompletion)))
wrapDoubleOrErrorCompletionOn {
return ^(dispatch_queue_t queue, void (^work)(FBLPromiseDoubleOrErrorCompletion)) {
return [self onQueue:queue wrapDoubleOrErrorCompletion:work];
};
}
@end
/** Stub used to force the linker to include the categories in this file. */
void FBLIncludeWrapCategory(void) {}

View File

@ -0,0 +1,346 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromisePrivate.h"
/** All states a promise can be in. */
typedef NS_ENUM(NSInteger, FBLPromiseState) {
FBLPromiseStatePending = 0,
FBLPromiseStateFulfilled,
FBLPromiseStateRejected,
};
typedef void (^FBLPromiseObserver)(FBLPromiseState state, id __nullable resolution);
static dispatch_queue_t gFBLPromiseDefaultDispatchQueue;
@implementation FBLPromise {
/** Current state of the promise. */
FBLPromiseState _state;
/**
Set of arbitrary objects to keep strongly while the promise is pending.
Becomes nil after the promise has been resolved.
*/
NSMutableSet *__nullable _pendingObjects;
/**
Value to fulfill the promise with.
Can be nil if the promise is still pending, was resolved with nil or after it has been rejected.
*/
id __nullable _value;
/**
Error to reject the promise with.
Can be nil if the promise is still pending or after it has been fulfilled.
*/
NSError *__nullable _error;
/** List of observers to notify when the promise gets resolved. */
NSMutableArray<FBLPromiseObserver> *_observers;
}
+ (void)initialize {
if (self == [FBLPromise class]) {
gFBLPromiseDefaultDispatchQueue = dispatch_get_main_queue();
}
}
+ (dispatch_queue_t)defaultDispatchQueue {
@synchronized(self) {
return gFBLPromiseDefaultDispatchQueue;
}
}
+ (void)setDefaultDispatchQueue:(dispatch_queue_t)queue {
NSParameterAssert(queue);
@synchronized(self) {
gFBLPromiseDefaultDispatchQueue = queue;
}
}
+ (instancetype)pendingPromise {
return [[self alloc] initPending];
}
+ (instancetype)resolvedWith:(nullable id)resolution {
return [[self alloc] initWithResolution:resolution];
}
- (void)fulfill:(nullable id)value {
if ([value isKindOfClass:[NSError class]]) {
[self reject:(NSError *)value];
} else {
@synchronized(self) {
if (_state == FBLPromiseStatePending) {
_state = FBLPromiseStateFulfilled;
_value = value;
_pendingObjects = nil;
for (FBLPromiseObserver observer in _observers) {
observer(_state, _value);
}
_observers = nil;
dispatch_group_leave(FBLPromise.dispatchGroup);
}
}
}
}
- (void)reject:(NSError *)error {
NSAssert([error isKindOfClass:[NSError class]], @"Invalid error type.");
if (![error isKindOfClass:[NSError class]]) {
// Give up on invalid error type in Release mode.
@throw error; // NOLINT
}
@synchronized(self) {
if (_state == FBLPromiseStatePending) {
_state = FBLPromiseStateRejected;
_error = error;
_pendingObjects = nil;
for (FBLPromiseObserver observer in _observers) {
observer(_state, _error);
}
_observers = nil;
dispatch_group_leave(FBLPromise.dispatchGroup);
}
}
}
#pragma mark - NSObject
- (NSString *)description {
if (self.isFulfilled) {
return [NSString stringWithFormat:@"<%@ %p> Fulfilled: %@", NSStringFromClass([self class]),
self, self.value];
}
if (self.isRejected) {
return [NSString stringWithFormat:@"<%@ %p> Rejected: %@", NSStringFromClass([self class]),
self, self.error];
}
return [NSString stringWithFormat:@"<%@ %p> Pending", NSStringFromClass([self class]), self];
}
#pragma mark - Private
- (instancetype)initPending {
self = [super init];
if (self) {
dispatch_group_enter(FBLPromise.dispatchGroup);
}
return self;
}
- (instancetype)initWithResolution:(nullable id)resolution {
self = [super init];
if (self) {
if ([resolution isKindOfClass:[NSError class]]) {
_state = FBLPromiseStateRejected;
_error = (NSError *)resolution;
} else {
_state = FBLPromiseStateFulfilled;
_value = resolution;
}
}
return self;
}
- (void)dealloc {
if (_state == FBLPromiseStatePending) {
dispatch_group_leave(FBLPromise.dispatchGroup);
}
}
- (BOOL)isPending {
@synchronized(self) {
return _state == FBLPromiseStatePending;
}
}
- (BOOL)isFulfilled {
@synchronized(self) {
return _state == FBLPromiseStateFulfilled;
}
}
- (BOOL)isRejected {
@synchronized(self) {
return _state == FBLPromiseStateRejected;
}
}
- (nullable id)value {
@synchronized(self) {
return _value;
}
}
- (NSError *__nullable)error {
@synchronized(self) {
return _error;
}
}
- (void)addPendingObject:(id)object {
NSParameterAssert(object);
@synchronized(self) {
if (_state == FBLPromiseStatePending) {
if (!_pendingObjects) {
_pendingObjects = [[NSMutableSet alloc] init];
}
[_pendingObjects addObject:object];
}
}
}
- (void)observeOnQueue:(dispatch_queue_t)queue
fulfill:(FBLPromiseOnFulfillBlock)onFulfill
reject:(FBLPromiseOnRejectBlock)onReject {
NSParameterAssert(queue);
NSParameterAssert(onFulfill);
NSParameterAssert(onReject);
@synchronized(self) {
switch (_state) {
case FBLPromiseStatePending: {
if (!_observers) {
_observers = [[NSMutableArray alloc] init];
}
[_observers addObject:^(FBLPromiseState state, id __nullable resolution) {
dispatch_group_async(FBLPromise.dispatchGroup, queue, ^{
switch (state) {
case FBLPromiseStatePending:
break;
case FBLPromiseStateFulfilled:
onFulfill(resolution);
break;
case FBLPromiseStateRejected:
onReject(resolution);
break;
}
});
}];
break;
}
case FBLPromiseStateFulfilled: {
dispatch_group_async(FBLPromise.dispatchGroup, queue, ^{
onFulfill(self->_value);
});
break;
}
case FBLPromiseStateRejected: {
dispatch_group_async(FBLPromise.dispatchGroup, queue, ^{
onReject(self->_error);
});
break;
}
}
}
}
- (FBLPromise *)chainOnQueue:(dispatch_queue_t)queue
chainedFulfill:(FBLPromiseChainedFulfillBlock)chainedFulfill
chainedReject:(FBLPromiseChainedRejectBlock)chainedReject {
NSParameterAssert(queue);
FBLPromise *promise = [[[self class] alloc] initPending];
__auto_type resolver = ^(id __nullable value) {
if ([value isKindOfClass:[FBLPromise class]]) {
[(FBLPromise *)value observeOnQueue:queue
fulfill:^(id __nullable value) {
[promise fulfill:value];
}
reject:^(NSError *error) {
[promise reject:error];
}];
} else {
[promise fulfill:value];
}
};
[self observeOnQueue:queue
fulfill:^(id __nullable value) {
value = chainedFulfill ? chainedFulfill(value) : value;
resolver(value);
}
reject:^(NSError *error) {
id value = chainedReject ? chainedReject(error) : error;
resolver(value);
}];
return promise;
}
#pragma mark - Category linking workaround
extern void FBLIncludeAllCategory(void);
extern void FBLIncludeAlwaysCategory(void);
extern void FBLIncludeAnyCategory(void);
extern void FBLIncludeAsyncCategory(void);
extern void FBLIncludeAwaitCategory(void);
extern void FBLIncludeCatchCategory(void);
extern void FBLIncludeDelayCategory(void);
extern void FBLIncludeDoCategory(void);
extern void FBLIncludeRaceCategory(void);
extern void FBLIncludeRecoverCategory(void);
extern void FBLIncludeReduceCategory(void);
extern void FBLIncludeRetryCategory(void);
extern void FBLIncludeTestingCategory(void);
extern void FBLIncludeThenCategory(void);
extern void FBLIncludeTimeoutCategory(void);
extern void FBLIncludeValidateCategory(void);
extern void FBLIncludeWrapCategory(void);
/**
Does nothing when called, and not meant to be called.
This method forces the linker to include all FBLPromise categories even if
users do not include the '-ObjC' linker flag in their projects.
*/
+ (void)noop {
FBLIncludeAllCategory();
FBLIncludeAllCategory();
FBLIncludeAlwaysCategory();
FBLIncludeAnyCategory();
FBLIncludeAsyncCategory();
FBLIncludeAwaitCategory();
FBLIncludeCatchCategory();
FBLIncludeDelayCategory();
FBLIncludeDoCategory();
FBLIncludeRaceCategory();
FBLIncludeRecoverCategory();
FBLIncludeReduceCategory();
FBLIncludeRetryCategory();
FBLIncludeTestingCategory();
FBLIncludeThenCategory();
FBLIncludeTimeoutCategory();
FBLIncludeValidateCategory();
FBLIncludeWrapCategory();
}
@end
@implementation FBLPromise (DotSyntaxAdditions)
+ (FBLPromise * (^)(void))pending {
return ^(void) {
return [self pendingPromise];
};
}
+ (FBLPromise * (^)(id __nullable))resolved {
return ^(id resolution) {
return [self resolvedWith:resolution];
};
}
@end

View File

@ -0,0 +1,19 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromiseError.h"
NSErrorDomain const FBLPromiseErrorDomain = @"com.google.FBLPromises.Error";

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSPrivacyAccessedAPITypes</key>
<array/>
<key>NSPrivacyCollectedDataTypes</key>
<array/>
<key>NSPrivacyTracking</key>
<false/>
<key>NSPrivacyTrackingDomains</key>
<array/>
</dict>
</plist>

View File

@ -0,0 +1,63 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise.h"
NS_ASSUME_NONNULL_BEGIN
@interface FBLPromise<Value>(AllAdditions)
/**
Wait until all of the given promises are fulfilled.
If one of the given promises is rejected, then the returned promise is rejected with same error.
If any other arbitrary value or `NSError` appears in the array instead of `FBLPromise`,
it's implicitly considered a pre-fulfilled or pre-rejected `FBLPromise` correspondingly.
Promises resolved with `nil` become `NSNull` instances in the resulting array.
@param promises Promises to wait for.
@return Promise of an array containing the values of input promises in the same order.
*/
+ (FBLPromise<NSArray *> *)all:(NSArray *)promises NS_SWIFT_UNAVAILABLE("");
/**
Wait until all of the given promises are fulfilled.
If one of the given promises is rejected, then the returned promise is rejected with same error.
If any other arbitrary value or `NSError` appears in the array instead of `FBLPromise`,
it's implicitly considered a pre-fulfilled or pre-rejected FBLPromise correspondingly.
Promises resolved with `nil` become `NSNull` instances in the resulting array.
@param queue A queue to dispatch on.
@param promises Promises to wait for.
@return Promise of an array containing the values of input promises in the same order.
*/
+ (FBLPromise<NSArray *> *)onQueue:(dispatch_queue_t)queue
all:(NSArray *)promises NS_REFINED_FOR_SWIFT;
@end
/**
Convenience dot-syntax wrappers for `FBLPromise` `all` operators.
Usage: FBLPromise.all(@[ ... ])
*/
@interface FBLPromise<Value>(DotSyntax_AllAdditions)
+ (FBLPromise<NSArray *> * (^)(NSArray *))all FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise<NSArray *> * (^)(dispatch_queue_t, NSArray *))allOn FBL_PROMISES_DOT_SYNTAX
NS_SWIFT_UNAVAILABLE("");
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,54 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise.h"
NS_ASSUME_NONNULL_BEGIN
@interface FBLPromise<Value>(AlwaysAdditions)
typedef void (^FBLPromiseAlwaysWorkBlock)(void) NS_SWIFT_UNAVAILABLE("");
/**
@param work A block that always executes, no matter if the receiver is rejected or fulfilled.
@return A new pending promise to be resolved with same resolution as the receiver.
*/
- (FBLPromise *)always:(FBLPromiseAlwaysWorkBlock)work NS_SWIFT_UNAVAILABLE("");
/**
@param queue A queue to dispatch on.
@param work A block that always executes, no matter if the receiver is rejected or fulfilled.
@return A new pending promise to be resolved with same resolution as the receiver.
*/
- (FBLPromise *)onQueue:(dispatch_queue_t)queue
always:(FBLPromiseAlwaysWorkBlock)work NS_REFINED_FOR_SWIFT;
@end
/**
Convenience dot-syntax wrappers for `FBLPromise` `always` operators.
Usage: promise.always(^{...})
*/
@interface FBLPromise<Value>(DotSyntax_AlwaysAdditions)
- (FBLPromise* (^)(FBLPromiseAlwaysWorkBlock))always FBL_PROMISES_DOT_SYNTAX
NS_SWIFT_UNAVAILABLE("");
- (FBLPromise* (^)(dispatch_queue_t, FBLPromiseAlwaysWorkBlock))alwaysOn FBL_PROMISES_DOT_SYNTAX
NS_SWIFT_UNAVAILABLE("");
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,69 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise.h"
NS_ASSUME_NONNULL_BEGIN
@interface FBLPromise<Value>(AnyAdditions)
/**
Waits until all of the given promises are either fulfilled or rejected.
If all promises are rejected, then the returned promise is rejected with same error
as the last one rejected.
If at least one of the promises is fulfilled, the resulting promise is fulfilled with an array of
values or `NSErrors`, matching the original order of fulfilled or rejected promises respectively.
If any other arbitrary value or `NSError` appears in the array instead of `FBLPromise`,
it's implicitly considered a pre-fulfilled or pre-rejected `FBLPromise` correspondingly.
Promises resolved with `nil` become `NSNull` instances in the resulting array.
@param promises Promises to wait for.
@return Promise of array containing the values or `NSError`s of input promises in the same order.
*/
+ (FBLPromise<NSArray *> *)any:(NSArray *)promises NS_SWIFT_UNAVAILABLE("");
/**
Waits until all of the given promises are either fulfilled or rejected.
If all promises are rejected, then the returned promise is rejected with same error
as the last one rejected.
If at least one of the promises is fulfilled, the resulting promise is fulfilled with an array of
values or `NSError`s, matching the original order of fulfilled or rejected promises respectively.
If any other arbitrary value or `NSError` appears in the array instead of `FBLPromise`,
it's implicitly considered a pre-fulfilled or pre-rejected `FBLPromise` correspondingly.
Promises resolved with `nil` become `NSNull` instances in the resulting array.
@param queue A queue to dispatch on.
@param promises Promises to wait for.
@return Promise of array containing the values or `NSError`s of input promises in the same order.
*/
+ (FBLPromise<NSArray *> *)onQueue:(dispatch_queue_t)queue
any:(NSArray *)promises NS_REFINED_FOR_SWIFT;
@end
/**
Convenience dot-syntax wrappers for `FBLPromise` `any` operators.
Usage: FBLPromise.any(@[ ... ])
*/
@interface FBLPromise<Value>(DotSyntax_AnyAdditions)
+ (FBLPromise<NSArray *> * (^)(NSArray *))any FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise<NSArray *> * (^)(dispatch_queue_t, NSArray *))anyOn FBL_PROMISES_DOT_SYNTAX
NS_SWIFT_UNAVAILABLE("");
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,60 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise.h"
NS_ASSUME_NONNULL_BEGIN
@interface FBLPromise<Value>(AsyncAdditions)
typedef void (^FBLPromiseFulfillBlock)(Value __nullable value) NS_SWIFT_UNAVAILABLE("");
typedef void (^FBLPromiseRejectBlock)(NSError *error) NS_SWIFT_UNAVAILABLE("");
typedef void (^FBLPromiseAsyncWorkBlock)(FBLPromiseFulfillBlock fulfill,
FBLPromiseRejectBlock reject) NS_SWIFT_UNAVAILABLE("");
/**
Creates a pending promise and executes `work` block asynchronously.
@param work A block to perform any operations needed to resolve the promise.
@return A new pending promise.
*/
+ (instancetype)async:(FBLPromiseAsyncWorkBlock)work NS_SWIFT_UNAVAILABLE("");
/**
Creates a pending promise and executes `work` block asynchronously on the given queue.
@param queue A queue to invoke the `work` block on.
@param work A block to perform any operations needed to resolve the promise.
@return A new pending promise.
*/
+ (instancetype)onQueue:(dispatch_queue_t)queue
async:(FBLPromiseAsyncWorkBlock)work NS_REFINED_FOR_SWIFT;
@end
/**
Convenience dot-syntax wrappers for `FBLPromise` `async` operators.
Usage: FBLPromise.async(^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock reject) { ... })
*/
@interface FBLPromise<Value>(DotSyntax_AsyncAdditions)
+ (FBLPromise* (^)(FBLPromiseAsyncWorkBlock))async FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise* (^)(dispatch_queue_t, FBLPromiseAsyncWorkBlock))asyncOn FBL_PROMISES_DOT_SYNTAX
NS_SWIFT_UNAVAILABLE("");
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,32 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise.h"
NS_ASSUME_NONNULL_BEGIN
/**
Waits for promise resolution. The current thread blocks until the promise is resolved.
@param promise Promise to wait for.
@param error Error the promise was rejected with, or `nil` if the promise was fulfilled.
@return Value the promise was fulfilled with. If the promise was rejected, the return value
is always `nil`, but the error out arg is not.
*/
FOUNDATION_EXTERN id __nullable FBLPromiseAwait(FBLPromise *promise,
NSError **error) NS_REFINED_FOR_SWIFT;
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,59 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise.h"
NS_ASSUME_NONNULL_BEGIN
@interface FBLPromise<Value>(CatchAdditions)
typedef void (^FBLPromiseCatchWorkBlock)(NSError *error) NS_SWIFT_UNAVAILABLE("");
/**
Creates a pending promise which eventually gets resolved with same resolution as the receiver.
If receiver is rejected, then `reject` block is executed asynchronously.
@param reject A block to handle the error that receiver was rejected with.
@return A new pending promise.
*/
- (FBLPromise *)catch:(FBLPromiseCatchWorkBlock)reject NS_SWIFT_UNAVAILABLE("");
/**
Creates a pending promise which eventually gets resolved with same resolution as the receiver.
If receiver is rejected, then `reject` block is executed asynchronously on the given queue.
@param queue A queue to invoke the `reject` block on.
@param reject A block to handle the error that receiver was rejected with.
@return A new pending promise.
*/
- (FBLPromise *)onQueue:(dispatch_queue_t)queue
catch:(FBLPromiseCatchWorkBlock)reject NS_REFINED_FOR_SWIFT;
@end
/**
Convenience dot-syntax wrappers for `FBLPromise` `catch` operators.
Usage: promise.catch(^(NSError *error) { ... })
*/
@interface FBLPromise<Value>(DotSyntax_CatchAdditions)
- (FBLPromise* (^)(FBLPromiseCatchWorkBlock))catch FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
- (FBLPromise* (^)(dispatch_queue_t, FBLPromiseCatchWorkBlock))catchOn FBL_PROMISES_DOT_SYNTAX
NS_SWIFT_UNAVAILABLE("");
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,59 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise.h"
NS_ASSUME_NONNULL_BEGIN
@interface FBLPromise<Value>(DelayAdditions)
/**
Creates a new pending promise that fulfills with the same value as `self` after the `delay`, or
rejects with the same error immediately.
@param interval Time to wait in seconds.
@return A new pending promise that fulfills at least `delay` seconds later than `self`, or rejects
with the same error immediately.
*/
- (FBLPromise *)delay:(NSTimeInterval)interval NS_SWIFT_UNAVAILABLE("");
/**
Creates a new pending promise that fulfills with the same value as `self` after the `delay`, or
rejects with the same error immediately.
@param queue A queue to dispatch on.
@param interval Time to wait in seconds.
@return A new pending promise that fulfills at least `delay` seconds later than `self`, or rejects
with the same error immediately.
*/
- (FBLPromise *)onQueue:(dispatch_queue_t)queue
delay:(NSTimeInterval)interval NS_REFINED_FOR_SWIFT;
@end
/**
Convenience dot-syntax wrappers for `FBLPromise` `delay` operators.
Usage: promise.delay(...)
*/
@interface FBLPromise<Value>(DotSyntax_DelayAdditions)
- (FBLPromise * (^)(NSTimeInterval))delay FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
- (FBLPromise * (^)(dispatch_queue_t, NSTimeInterval))delayOn FBL_PROMISES_DOT_SYNTAX
NS_SWIFT_UNAVAILABLE("");
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,55 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise.h"
NS_ASSUME_NONNULL_BEGIN
@interface FBLPromise<Value>(DoAdditions)
typedef id __nullable (^FBLPromiseDoWorkBlock)(void) NS_SWIFT_UNAVAILABLE("");
/**
Creates a pending promise and executes `work` block asynchronously.
@param work A block that returns a value or an error used to resolve the promise.
@return A new pending promise.
*/
+ (instancetype)do:(FBLPromiseDoWorkBlock)work NS_SWIFT_UNAVAILABLE("");
/**
Creates a pending promise and executes `work` block asynchronously on the given queue.
@param queue A queue to invoke the `work` block on.
@param work A block that returns a value or an error used to resolve the promise.
@return A new pending promise.
*/
+ (instancetype)onQueue:(dispatch_queue_t)queue do:(FBLPromiseDoWorkBlock)work NS_REFINED_FOR_SWIFT;
@end
/**
Convenience dot-syntax wrappers for `FBLPromise` `do` operators.
Usage: FBLPromise.doOn(queue, ^(NSError *error) { ... })
*/
@interface FBLPromise<Value>(DotSyntax_DoAdditions)
+ (FBLPromise * (^)(dispatch_queue_t, FBLPromiseDoWorkBlock))doOn FBL_PROMISES_DOT_SYNTAX
NS_SWIFT_UNAVAILABLE("");
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,62 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise.h"
NS_ASSUME_NONNULL_BEGIN
@interface FBLPromise<Value>(RaceAdditions)
/**
Wait until any of the given promises are fulfilled.
If one of the promises is rejected, then the returned promise is rejected with same error.
If any other arbitrary value or `NSError` appears in the array instead of `FBLPromise`,
it's implicitly considered a pre-fulfilled or pre-rejected `FBLPromise` correspondingly.
@param promises Promises to wait for.
@return A new pending promise to be resolved with the same resolution as the first promise, among
the given ones, which was resolved.
*/
+ (instancetype)race:(NSArray *)promises NS_SWIFT_UNAVAILABLE("");
/**
Wait until any of the given promises are fulfilled.
If one of the promises is rejected, then the returned promise is rejected with same error.
If any other arbitrary value or `NSError` appears in the array instead of `FBLPromise`,
it's implicitly considered a pre-fulfilled or pre-rejected `FBLPromise` correspondingly.
@param queue A queue to dispatch on.
@param promises Promises to wait for.
@return A new pending promise to be resolved with the same resolution as the first promise, among
the given ones, which was resolved.
*/
+ (instancetype)onQueue:(dispatch_queue_t)queue race:(NSArray *)promises NS_REFINED_FOR_SWIFT;
@end
/**
Convenience dot-syntax wrappers for `FBLPromise` `race` operators.
Usage: FBLPromise.race(@[ ... ])
*/
@interface FBLPromise<Value>(DotSyntax_RaceAdditions)
+ (FBLPromise * (^)(NSArray *))race FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise * (^)(dispatch_queue_t, NSArray *))raceOn FBL_PROMISES_DOT_SYNTAX
NS_SWIFT_UNAVAILABLE("");
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,60 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise.h"
NS_ASSUME_NONNULL_BEGIN
@interface FBLPromise<Value>(RecoverAdditions)
typedef id __nullable (^FBLPromiseRecoverWorkBlock)(NSError *error) NS_SWIFT_UNAVAILABLE("");
/**
Provides a new promise to recover in case the receiver gets rejected.
@param recovery A block to handle the error that the receiver was rejected with.
@return A new pending promise to use instead of the rejected one that gets resolved with resolution
returned from `recovery` block.
*/
- (FBLPromise *)recover:(FBLPromiseRecoverWorkBlock)recovery NS_SWIFT_UNAVAILABLE("");
/**
Provides a new promise to recover in case the receiver gets rejected.
@param queue A queue to dispatch on.
@param recovery A block to handle the error that the receiver was rejected with.
@return A new pending promise to use instead of the rejected one that gets resolved with resolution
returned from `recovery` block.
*/
- (FBLPromise *)onQueue:(dispatch_queue_t)queue
recover:(FBLPromiseRecoverWorkBlock)recovery NS_REFINED_FOR_SWIFT;
@end
/**
Convenience dot-syntax wrappers for `FBLPromise` `recover` operators.
Usage: promise.recover(^id(NSError *error) {...})
*/
@interface FBLPromise<Value>(DotSyntax_RecoverAdditions)
- (FBLPromise * (^)(FBLPromiseRecoverWorkBlock))recover FBL_PROMISES_DOT_SYNTAX
NS_SWIFT_UNAVAILABLE("");
- (FBLPromise * (^)(dispatch_queue_t, FBLPromiseRecoverWorkBlock))recoverOn FBL_PROMISES_DOT_SYNTAX
NS_SWIFT_UNAVAILABLE("");
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,71 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise.h"
NS_ASSUME_NONNULL_BEGIN
@interface FBLPromise<Value>(ReduceAdditions)
typedef id __nullable (^FBLPromiseReducerBlock)(Value __nullable partial, id next)
NS_SWIFT_UNAVAILABLE("");
/**
Sequentially reduces a collection of values to a single promise using a given combining block
and the value `self` resolves with as initial value.
@param items An array of values to process in order.
@param reducer A block to combine an accumulating value and an element of the sequence into
the new accumulating value or a promise resolved with it, to be used in the next
call of the `reducer` or returned to the caller.
@return A new pending promise returned from the last `reducer` invocation.
Or `self` if `items` is empty.
*/
- (FBLPromise *)reduce:(NSArray *)items
combine:(FBLPromiseReducerBlock)reducer NS_SWIFT_UNAVAILABLE("");
/**
Sequentially reduces a collection of values to a single promise using a given combining block
and the value `self` resolves with as initial value.
@param queue A queue to dispatch on.
@param items An array of values to process in order.
@param reducer A block to combine an accumulating value and an element of the sequence into
the new accumulating value or a promise resolved with it, to be used in the next
call of the `reducer` or returned to the caller.
@return A new pending promise returned from the last `reducer` invocation.
Or `self` if `items` is empty.
*/
- (FBLPromise *)onQueue:(dispatch_queue_t)queue
reduce:(NSArray *)items
combine:(FBLPromiseReducerBlock)reducer NS_SWIFT_UNAVAILABLE("");
@end
/**
Convenience dot-syntax wrappers for `FBLPromise` `reduce` operators.
Usage: promise.reduce(values, ^id(id partial, id next) { ... })
*/
@interface FBLPromise<Value>(DotSyntax_ReduceAdditions)
- (FBLPromise * (^)(NSArray *, FBLPromiseReducerBlock))reduce FBL_PROMISES_DOT_SYNTAX
NS_SWIFT_UNAVAILABLE("");
- (FBLPromise * (^)(dispatch_queue_t, NSArray *, FBLPromiseReducerBlock))reduceOn
FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,165 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise.h"
NS_ASSUME_NONNULL_BEGIN
/** The default number of retry attempts is 1. */
FOUNDATION_EXTERN NSInteger const FBLPromiseRetryDefaultAttemptsCount NS_REFINED_FOR_SWIFT;
/** The default delay interval before making a retry attempt is 1.0 second. */
FOUNDATION_EXTERN NSTimeInterval const FBLPromiseRetryDefaultDelayInterval NS_REFINED_FOR_SWIFT;
@interface FBLPromise<Value>(RetryAdditions)
typedef id __nullable (^FBLPromiseRetryWorkBlock)(void) NS_SWIFT_UNAVAILABLE("");
typedef BOOL (^FBLPromiseRetryPredicateBlock)(NSInteger, NSError *) NS_SWIFT_UNAVAILABLE("");
/**
Creates a pending promise that fulfills with the same value as the promise returned from `work`
block, which executes asynchronously, or rejects with the same error after all retry attempts have
been exhausted. Defaults to `FBLPromiseRetryDefaultAttemptsCount` attempt(s) on rejection where the
`work` block is retried after a delay of `FBLPromiseRetryDefaultDelayInterval` second(s).
@param work A block that executes asynchronously on the default queue and returns a value or an
error used to resolve the promise.
@return A new pending promise that fulfills with the same value as the promise returned from `work`
block, or rejects with the same error after all retry attempts have been exhausted.
*/
+ (instancetype)retry:(FBLPromiseRetryWorkBlock)work NS_SWIFT_UNAVAILABLE("");
/**
Creates a pending promise that fulfills with the same value as the promise returned from `work`
block, which executes asynchronously on the given `queue`, or rejects with the same error after all
retry attempts have been exhausted. Defaults to `FBLPromiseRetryDefaultAttemptsCount` attempt(s) on
rejection where the `work` block is retried on the given `queue` after a delay of
`FBLPromiseRetryDefaultDelayInterval` second(s).
@param queue A queue to invoke the `work` block on.
@param work A block that executes asynchronously on the given `queue` and returns a value or an
error used to resolve the promise.
@return A new pending promise that fulfills with the same value as the promise returned from `work`
block, or rejects with the same error after all retry attempts have been exhausted.
*/
+ (instancetype)onQueue:(dispatch_queue_t)queue
retry:(FBLPromiseRetryWorkBlock)work NS_SWIFT_UNAVAILABLE("");
/**
Creates a pending promise that fulfills with the same value as the promise returned from `work`
block, which executes asynchronously, or rejects with the same error after all retry attempts have
been exhausted.
@param count Max number of retry attempts. The `work` block will be executed once if the specified
count is less than or equal to zero.
@param work A block that executes asynchronously on the default queue and returns a value or an
error used to resolve the promise.
@return A new pending promise that fulfills with the same value as the promise returned from `work`
block, or rejects with the same error after all retry attempts have been exhausted.
*/
+ (instancetype)attempts:(NSInteger)count
retry:(FBLPromiseRetryWorkBlock)work NS_SWIFT_UNAVAILABLE("");
/**
Creates a pending promise that fulfills with the same value as the promise returned from `work`
block, which executes asynchronously on the given `queue`, or rejects with the same error after all
retry attempts have been exhausted.
@param queue A queue to invoke the `work` block on.
@param count Max number of retry attempts. The `work` block will be executed once if the specified
count is less than or equal to zero.
@param work A block that executes asynchronously on the given `queue` and returns a value or an
error used to resolve the promise.
@return A new pending promise that fulfills with the same value as the promise returned from `work`
block, or rejects with the same error after all retry attempts have been exhausted.
*/
+ (instancetype)onQueue:(dispatch_queue_t)queue
attempts:(NSInteger)count
retry:(FBLPromiseRetryWorkBlock)work NS_SWIFT_UNAVAILABLE("");
/**
Creates a pending promise that fulfills with the same value as the promise returned from `work`
block, which executes asynchronously, or rejects with the same error after all retry attempts have
been exhausted. On rejection, the `work` block is retried after the given delay `interval` and will
continue to retry until the number of specified attempts have been exhausted or will bail early if
the given condition is not met.
@param count Max number of retry attempts. The `work` block will be executed once if the specified
count is less than or equal to zero.
@param interval Time to wait before the next retry attempt.
@param predicate Condition to check before the next retry attempt. The predicate block provides the
the number of remaining retry attempts and the error that the promise was rejected
with.
@param work A block that executes asynchronously on the default queue and returns a value or an
error used to resolve the promise.
@return A new pending promise that fulfills with the same value as the promise returned from `work`
block, or rejects with the same error after all retry attempts have been exhausted or if
the given condition is not met.
*/
+ (instancetype)attempts:(NSInteger)count
delay:(NSTimeInterval)interval
condition:(nullable FBLPromiseRetryPredicateBlock)predicate
retry:(FBLPromiseRetryWorkBlock)work NS_SWIFT_UNAVAILABLE("");
/**
Creates a pending promise that fulfills with the same value as the promise returned from `work`
block, which executes asynchronously on the given `queue`, or rejects with the same error after all
retry attempts have been exhausted. On rejection, the `work` block is retried after the given
delay `interval` and will continue to retry until the number of specified attempts have been
exhausted or will bail early if the given condition is not met.
@param queue A queue to invoke the `work` block on.
@param count Max number of retry attempts. The `work` block will be executed once if the specified
count is less than or equal to zero.
@param interval Time to wait before the next retry attempt.
@param predicate Condition to check before the next retry attempt. The predicate block provides the
the number of remaining retry attempts and the error that the promise was rejected
with.
@param work A block that executes asynchronously on the given `queue` and returns a value or an
error used to resolve the promise.
@return A new pending promise that fulfills with the same value as the promise returned from `work`
block, or rejects with the same error after all retry attempts have been exhausted or if
the given condition is not met.
*/
+ (instancetype)onQueue:(dispatch_queue_t)queue
attempts:(NSInteger)count
delay:(NSTimeInterval)interval
condition:(nullable FBLPromiseRetryPredicateBlock)predicate
retry:(FBLPromiseRetryWorkBlock)work NS_REFINED_FOR_SWIFT;
@end
/**
Convenience dot-syntax wrappers for `FBLPromise+Retry` operators.
Usage: FBLPromise.retry(^id { ... })
*/
@interface FBLPromise<Value>(DotSyntax_RetryAdditions)
+ (FBLPromise * (^)(FBLPromiseRetryWorkBlock))retry FBL_PROMISES_DOT_SYNTAX
NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise * (^)(dispatch_queue_t, FBLPromiseRetryWorkBlock))retryOn FBL_PROMISES_DOT_SYNTAX
NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise * (^)(NSInteger, NSTimeInterval, FBLPromiseRetryPredicateBlock __nullable,
FBLPromiseRetryWorkBlock))retryAgain FBL_PROMISES_DOT_SYNTAX
NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise * (^)(dispatch_queue_t, NSInteger, NSTimeInterval,
FBLPromiseRetryPredicateBlock __nullable,
FBLPromiseRetryWorkBlock))retryAgainOn FBL_PROMISES_DOT_SYNTAX
NS_SWIFT_UNAVAILABLE("");
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,57 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise.h"
NS_ASSUME_NONNULL_BEGIN
/**
Waits for all scheduled promises blocks.
@param timeout Maximum time to wait.
@return YES if all promises blocks have completed before the timeout and NO otherwise.
*/
FOUNDATION_EXTERN BOOL FBLWaitForPromisesWithTimeout(NSTimeInterval timeout) NS_REFINED_FOR_SWIFT;
@interface FBLPromise<Value>(TestingAdditions)
/**
Dispatch group for promises that is typically used to wait for all scheduled blocks.
*/
@property(class, nonatomic, readonly) dispatch_group_t dispatchGroup NS_REFINED_FOR_SWIFT;
/**
Properties to get the current state of the promise.
*/
@property(nonatomic, readonly) BOOL isPending NS_REFINED_FOR_SWIFT;
@property(nonatomic, readonly) BOOL isFulfilled NS_REFINED_FOR_SWIFT;
@property(nonatomic, readonly) BOOL isRejected NS_REFINED_FOR_SWIFT;
/**
Value the promise was fulfilled with.
Can be nil if the promise is still pending, was resolved with nil or after it has been rejected.
*/
@property(nonatomic, readonly, nullable) Value value NS_REFINED_FOR_SWIFT;
/**
Error the promise was rejected with.
Can be nil if the promise is still pending or after it has been fulfilled.
*/
@property(nonatomic, readonly, nullable) NSError *error NS_REFINED_FOR_SWIFT;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,63 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise.h"
NS_ASSUME_NONNULL_BEGIN
@interface FBLPromise<Value>(ThenAdditions)
typedef id __nullable (^FBLPromiseThenWorkBlock)(Value __nullable value) NS_SWIFT_UNAVAILABLE("");
/**
Creates a pending promise which eventually gets resolved with resolution returned from `work`
block: either value, error or another promise. The `work` block is executed asynchronously only
when the receiver is fulfilled. If receiver is rejected, the returned promise is also rejected with
the same error.
@param work A block to handle the value that receiver was fulfilled with.
@return A new pending promise to be resolved with resolution returned from the `work` block.
*/
- (FBLPromise *)then:(FBLPromiseThenWorkBlock)work NS_SWIFT_UNAVAILABLE("");
/**
Creates a pending promise which eventually gets resolved with resolution returned from `work`
block: either value, error or another promise. The `work` block is executed asynchronously when the
receiver is fulfilled. If receiver is rejected, the returned promise is also rejected with the same
error.
@param queue A queue to invoke the `work` block on.
@param work A block to handle the value that receiver was fulfilled with.
@return A new pending promise to be resolved with resolution returned from the `work` block.
*/
- (FBLPromise *)onQueue:(dispatch_queue_t)queue
then:(FBLPromiseThenWorkBlock)work NS_REFINED_FOR_SWIFT;
@end
/**
Convenience dot-syntax wrappers for `FBLPromise` `then` operators.
Usage: promise.then(^id(id value) { ... })
*/
@interface FBLPromise<Value>(DotSyntax_ThenAdditions)
- (FBLPromise* (^)(FBLPromiseThenWorkBlock))then FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
- (FBLPromise* (^)(dispatch_queue_t, FBLPromiseThenWorkBlock))thenOn FBL_PROMISES_DOT_SYNTAX
NS_SWIFT_UNAVAILABLE("");
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,57 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise.h"
NS_ASSUME_NONNULL_BEGIN
@interface FBLPromise<Value>(TimeoutAdditions)
/**
Waits for a promise with the specified `timeout`.
@param interval Time to wait in seconds.
@return A new pending promise that gets either resolved with same resolution as the receiver or
rejected with `FBLPromiseErrorCodeTimedOut` error code in `FBLPromiseErrorDomain`.
*/
- (FBLPromise *)timeout:(NSTimeInterval)interval NS_SWIFT_UNAVAILABLE("");
/**
Waits for a promise with the specified `timeout`.
@param queue A queue to dispatch on.
@param interval Time to wait in seconds.
@return A new pending promise that gets either resolved with same resolution as the receiver or
rejected with `FBLPromiseErrorCodeTimedOut` error code in `FBLPromiseErrorDomain`.
*/
- (FBLPromise *)onQueue:(dispatch_queue_t)queue
timeout:(NSTimeInterval)interval NS_REFINED_FOR_SWIFT;
@end
/**
Convenience dot-syntax wrappers for `FBLPromise` `timeout` operators.
Usage: promise.timeout(...)
*/
@interface FBLPromise<Value>(DotSyntax_TimeoutAdditions)
- (FBLPromise* (^)(NSTimeInterval))timeout FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
- (FBLPromise* (^)(dispatch_queue_t, NSTimeInterval))timeoutOn FBL_PROMISES_DOT_SYNTAX
NS_SWIFT_UNAVAILABLE("");
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,60 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise.h"
NS_ASSUME_NONNULL_BEGIN
@interface FBLPromise<Value>(ValidateAdditions)
typedef BOOL (^FBLPromiseValidateWorkBlock)(Value __nullable value) NS_SWIFT_UNAVAILABLE("");
/**
Validates a fulfilled value or rejects the value if it can not be validated.
@param predicate An expression to validate.
@return A new pending promise that gets either resolved with same resolution as the receiver or
rejected with `FBLPromiseErrorCodeValidationFailure` error code in `FBLPromiseErrorDomain`.
*/
- (FBLPromise *)validate:(FBLPromiseValidateWorkBlock)predicate NS_SWIFT_UNAVAILABLE("");
/**
Validates a fulfilled value or rejects the value if it can not be validated.
@param queue A queue to dispatch on.
@param predicate An expression to validate.
@return A new pending promise that gets either resolved with same resolution as the receiver or
rejected with `FBLPromiseErrorCodeValidationFailure` error code in `FBLPromiseErrorDomain`.
*/
- (FBLPromise *)onQueue:(dispatch_queue_t)queue
validate:(FBLPromiseValidateWorkBlock)predicate NS_REFINED_FOR_SWIFT;
@end
/**
Convenience dot-syntax wrappers for `FBLPromise` `validate` operators.
Usage: promise.validate(^BOOL(id value) { ... })
*/
@interface FBLPromise<Value>(DotSyntax_ValidateAdditions)
- (FBLPromise * (^)(FBLPromiseValidateWorkBlock))validate FBL_PROMISES_DOT_SYNTAX
NS_SWIFT_UNAVAILABLE("");
- (FBLPromise * (^)(dispatch_queue_t, FBLPromiseValidateWorkBlock))validateOn
FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,316 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise.h"
NS_ASSUME_NONNULL_BEGIN
/**
Different types of completion handlers available to be wrapped with promise.
*/
typedef void (^FBLPromiseCompletion)(void) NS_SWIFT_UNAVAILABLE("");
typedef void (^FBLPromiseObjectCompletion)(id __nullable) NS_SWIFT_UNAVAILABLE("");
typedef void (^FBLPromiseErrorCompletion)(NSError* __nullable) NS_SWIFT_UNAVAILABLE("");
typedef void (^FBLPromiseObjectOrErrorCompletion)(id __nullable, NSError* __nullable)
NS_SWIFT_UNAVAILABLE("");
typedef void (^FBLPromiseErrorOrObjectCompletion)(NSError* __nullable, id __nullable)
NS_SWIFT_UNAVAILABLE("");
typedef void (^FBLPromise2ObjectsOrErrorCompletion)(id __nullable, id __nullable,
NSError* __nullable) NS_SWIFT_UNAVAILABLE("");
typedef void (^FBLPromiseBoolCompletion)(BOOL) NS_SWIFT_UNAVAILABLE("");
typedef void (^FBLPromiseBoolOrErrorCompletion)(BOOL, NSError* __nullable) NS_SWIFT_UNAVAILABLE("");
typedef void (^FBLPromiseIntegerCompletion)(NSInteger) NS_SWIFT_UNAVAILABLE("");
typedef void (^FBLPromiseIntegerOrErrorCompletion)(NSInteger, NSError* __nullable)
NS_SWIFT_UNAVAILABLE("");
typedef void (^FBLPromiseDoubleCompletion)(double) NS_SWIFT_UNAVAILABLE("");
typedef void (^FBLPromiseDoubleOrErrorCompletion)(double, NSError* __nullable)
NS_SWIFT_UNAVAILABLE("");
/**
Provides an easy way to convert methods that use common callback patterns into promises.
*/
@interface FBLPromise<Value>(WrapAdditions)
/**
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with `nil` when completion handler is invoked.
*/
+ (instancetype)wrapCompletion:(void (^)(FBLPromiseCompletion handler))work
NS_SWIFT_UNAVAILABLE("");
/**
@param queue A queue to invoke the `work` block on.
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with `nil` when completion handler is invoked.
*/
+ (instancetype)onQueue:(dispatch_queue_t)queue
wrapCompletion:(void (^)(FBLPromiseCompletion handler))work NS_SWIFT_UNAVAILABLE("");
/**
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with an object provided by completion handler.
*/
+ (instancetype)wrapObjectCompletion:(void (^)(FBLPromiseObjectCompletion handler))work
NS_SWIFT_UNAVAILABLE("");
/**
@param queue A queue to invoke the `work` block on.
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with an object provided by completion handler.
*/
+ (instancetype)onQueue:(dispatch_queue_t)queue
wrapObjectCompletion:(void (^)(FBLPromiseObjectCompletion handler))work
NS_SWIFT_UNAVAILABLE("");
/**
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with an error provided by completion handler.
If error is `nil`, fulfills with `nil`, otherwise rejects with the error.
*/
+ (instancetype)wrapErrorCompletion:(void (^)(FBLPromiseErrorCompletion handler))work
NS_SWIFT_UNAVAILABLE("");
/**
@param queue A queue to invoke the `work` block on.
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with an error provided by completion handler.
If error is `nil`, fulfills with `nil`, otherwise rejects with the error.
*/
+ (instancetype)onQueue:(dispatch_queue_t)queue
wrapErrorCompletion:(void (^)(FBLPromiseErrorCompletion handler))work NS_SWIFT_UNAVAILABLE("");
/**
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with an object provided by completion handler if error is `nil`.
Otherwise, rejects with the error.
*/
+ (instancetype)wrapObjectOrErrorCompletion:
(void (^)(FBLPromiseObjectOrErrorCompletion handler))work NS_SWIFT_UNAVAILABLE("");
/**
@param queue A queue to invoke the `work` block on.
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with an object provided by completion handler if error is `nil`.
Otherwise, rejects with the error.
*/
+ (instancetype)onQueue:(dispatch_queue_t)queue
wrapObjectOrErrorCompletion:(void (^)(FBLPromiseObjectOrErrorCompletion handler))work
NS_SWIFT_UNAVAILABLE("");
/**
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with an error or object provided by completion handler. If error
is not `nil`, rejects with the error.
*/
+ (instancetype)wrapErrorOrObjectCompletion:
(void (^)(FBLPromiseErrorOrObjectCompletion handler))work NS_SWIFT_UNAVAILABLE("");
/**
@param queue A queue to invoke the `work` block on.
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with an error or object provided by completion handler. If error
is not `nil`, rejects with the error.
*/
+ (instancetype)onQueue:(dispatch_queue_t)queue
wrapErrorOrObjectCompletion:(void (^)(FBLPromiseErrorOrObjectCompletion handler))work
NS_SWIFT_UNAVAILABLE("");
/**
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with an array of objects provided by completion handler in order
if error is `nil`. Otherwise, rejects with the error.
*/
+ (FBLPromise<NSArray*>*)wrap2ObjectsOrErrorCompletion:
(void (^)(FBLPromise2ObjectsOrErrorCompletion handler))work NS_SWIFT_UNAVAILABLE("");
/**
@param queue A queue to invoke the `work` block on.
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with an array of objects provided by completion handler in order
if error is `nil`. Otherwise, rejects with the error.
*/
+ (FBLPromise<NSArray*>*)onQueue:(dispatch_queue_t)queue
wrap2ObjectsOrErrorCompletion:(void (^)(FBLPromise2ObjectsOrErrorCompletion handler))work
NS_SWIFT_UNAVAILABLE("");
/**
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with an `NSNumber` wrapping YES/NO.
*/
+ (FBLPromise<NSNumber*>*)wrapBoolCompletion:(void (^)(FBLPromiseBoolCompletion handler))work
NS_SWIFT_UNAVAILABLE("");
/**
@param queue A queue to invoke the `work` block on.
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with an `NSNumber` wrapping YES/NO.
*/
+ (FBLPromise<NSNumber*>*)onQueue:(dispatch_queue_t)queue
wrapBoolCompletion:(void (^)(FBLPromiseBoolCompletion handler))work
NS_SWIFT_UNAVAILABLE("");
/**
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with an `NSNumber` wrapping YES/NO when error is `nil`.
Otherwise rejects with the error.
*/
+ (FBLPromise<NSNumber*>*)wrapBoolOrErrorCompletion:
(void (^)(FBLPromiseBoolOrErrorCompletion handler))work NS_SWIFT_UNAVAILABLE("");
/**
@param queue A queue to invoke the `work` block on.
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with an `NSNumber` wrapping YES/NO when error is `nil`.
Otherwise rejects with the error.
*/
+ (FBLPromise<NSNumber*>*)onQueue:(dispatch_queue_t)queue
wrapBoolOrErrorCompletion:(void (^)(FBLPromiseBoolOrErrorCompletion handler))work
NS_SWIFT_UNAVAILABLE("");
/**
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with an `NSNumber` wrapping an integer.
*/
+ (FBLPromise<NSNumber*>*)wrapIntegerCompletion:(void (^)(FBLPromiseIntegerCompletion handler))work
NS_SWIFT_UNAVAILABLE("");
/**
@param queue A queue to invoke the `work` block on.
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with an `NSNumber` wrapping an integer.
*/
+ (FBLPromise<NSNumber*>*)onQueue:(dispatch_queue_t)queue
wrapIntegerCompletion:(void (^)(FBLPromiseIntegerCompletion handler))work
NS_SWIFT_UNAVAILABLE("");
/**
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with an `NSNumber` wrapping an integer when error is `nil`.
Otherwise rejects with the error.
*/
+ (FBLPromise<NSNumber*>*)wrapIntegerOrErrorCompletion:
(void (^)(FBLPromiseIntegerOrErrorCompletion handler))work NS_SWIFT_UNAVAILABLE("");
/**
@param queue A queue to invoke the `work` block on.
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with an `NSNumber` wrapping an integer when error is `nil`.
Otherwise rejects with the error.
*/
+ (FBLPromise<NSNumber*>*)onQueue:(dispatch_queue_t)queue
wrapIntegerOrErrorCompletion:(void (^)(FBLPromiseIntegerOrErrorCompletion handler))work
NS_SWIFT_UNAVAILABLE("");
/**
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with an `NSNumber` wrapping a double.
*/
+ (FBLPromise<NSNumber*>*)wrapDoubleCompletion:(void (^)(FBLPromiseDoubleCompletion handler))work
NS_SWIFT_UNAVAILABLE("");
/**
@param queue A queue to invoke the `work` block on.
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with an `NSNumber` wrapping a double.
*/
+ (FBLPromise<NSNumber*>*)onQueue:(dispatch_queue_t)queue
wrapDoubleCompletion:(void (^)(FBLPromiseDoubleCompletion handler))work
NS_SWIFT_UNAVAILABLE("");
/**
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with an `NSNumber` wrapping a double when error is `nil`.
Otherwise rejects with the error.
*/
+ (FBLPromise<NSNumber*>*)wrapDoubleOrErrorCompletion:
(void (^)(FBLPromiseDoubleOrErrorCompletion handler))work NS_SWIFT_UNAVAILABLE("");
/**
@param queue A queue to invoke the `work` block on.
@param work A block to perform any operations needed to resolve the promise.
@returns A promise that resolves with an `NSNumber` wrapping a double when error is `nil`.
Otherwise rejects with the error.
*/
+ (FBLPromise<NSNumber*>*)onQueue:(dispatch_queue_t)queue
wrapDoubleOrErrorCompletion:(void (^)(FBLPromiseDoubleOrErrorCompletion handler))work
NS_SWIFT_UNAVAILABLE("");
@end
/**
Convenience dot-syntax wrappers for `FBLPromise` `wrap` operators.
Usage: FBLPromise.wrapCompletion(^(FBLPromiseCompletion handler) {...})
*/
@interface FBLPromise<Value>(DotSyntax_WrapAdditions)
+ (FBLPromise* (^)(void (^)(FBLPromiseCompletion)))wrapCompletion FBL_PROMISES_DOT_SYNTAX
NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise* (^)(dispatch_queue_t, void (^)(FBLPromiseCompletion)))wrapCompletionOn
FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise* (^)(void (^)(FBLPromiseObjectCompletion)))wrapObjectCompletion
FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise* (^)(dispatch_queue_t, void (^)(FBLPromiseObjectCompletion)))wrapObjectCompletionOn
FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise* (^)(void (^)(FBLPromiseErrorCompletion)))wrapErrorCompletion FBL_PROMISES_DOT_SYNTAX
NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise* (^)(dispatch_queue_t, void (^)(FBLPromiseErrorCompletion)))wrapErrorCompletionOn
FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise* (^)(void (^)(FBLPromiseObjectOrErrorCompletion)))wrapObjectOrErrorCompletion
FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise* (^)(dispatch_queue_t,
void (^)(FBLPromiseObjectOrErrorCompletion)))wrapObjectOrErrorCompletionOn
FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise* (^)(void (^)(FBLPromiseErrorOrObjectCompletion)))wrapErrorOrObjectCompletion
FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise* (^)(dispatch_queue_t,
void (^)(FBLPromiseErrorOrObjectCompletion)))wrapErrorOrObjectCompletionOn
FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise<NSArray*>* (^)(void (^)(FBLPromise2ObjectsOrErrorCompletion)))
wrap2ObjectsOrErrorCompletion FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise<NSArray*>* (^)(dispatch_queue_t, void (^)(FBLPromise2ObjectsOrErrorCompletion)))
wrap2ObjectsOrErrorCompletionOn FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise<NSNumber*>* (^)(void (^)(FBLPromiseBoolCompletion)))wrapBoolCompletion
FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise<NSNumber*>* (^)(dispatch_queue_t,
void (^)(FBLPromiseBoolCompletion)))wrapBoolCompletionOn
FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise<NSNumber*>* (^)(void (^)(FBLPromiseBoolOrErrorCompletion)))wrapBoolOrErrorCompletion
FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise<NSNumber*>* (^)(dispatch_queue_t,
void (^)(FBLPromiseBoolOrErrorCompletion)))wrapBoolOrErrorCompletionOn
FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise<NSNumber*>* (^)(void (^)(FBLPromiseIntegerCompletion)))wrapIntegerCompletion
FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise<NSNumber*>* (^)(dispatch_queue_t,
void (^)(FBLPromiseIntegerCompletion)))wrapIntegerCompletionOn
FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise<NSNumber*>* (^)(void (^)(FBLPromiseIntegerOrErrorCompletion)))
wrapIntegerOrErrorCompletion FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise<NSNumber*>* (^)(dispatch_queue_t, void (^)(FBLPromiseIntegerOrErrorCompletion)))
wrapIntegerOrErrorCompletionOn FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise<NSNumber*>* (^)(void (^)(FBLPromiseDoubleCompletion)))wrapDoubleCompletion
FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise<NSNumber*>* (^)(dispatch_queue_t,
void (^)(FBLPromiseDoubleCompletion)))wrapDoubleCompletionOn
FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise<NSNumber*>* (^)(void (^)(FBLPromiseDoubleOrErrorCompletion)))
wrapDoubleOrErrorCompletion FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise<NSNumber*>* (^)(dispatch_queue_t, void (^)(FBLPromiseDoubleOrErrorCompletion)))
wrapDoubleOrErrorCompletionOn FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,93 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromiseError.h"
NS_ASSUME_NONNULL_BEGIN
/**
Promises synchronization construct in Objective-C.
*/
@interface FBLPromise<__covariant Value> : NSObject
/**
Default dispatch queue used for `FBLPromise`, which is `main` if a queue is not specified.
*/
@property(class) dispatch_queue_t defaultDispatchQueue NS_REFINED_FOR_SWIFT;
/**
Creates a pending promise.
*/
+ (instancetype)pendingPromise NS_REFINED_FOR_SWIFT;
/**
Creates a resolved promise.
@param resolution An object to resolve the promise with: either a value or an error.
@return A new resolved promise.
*/
+ (instancetype)resolvedWith:(nullable id)resolution NS_REFINED_FOR_SWIFT;
/**
Synchronously fulfills the promise with a value.
@param value An arbitrary value to fulfill the promise with, including `nil`.
*/
- (void)fulfill:(nullable Value)value NS_REFINED_FOR_SWIFT;
/**
Synchronously rejects the promise with an error.
@param error An error to reject the promise with.
*/
- (void)reject:(NSError *)error NS_REFINED_FOR_SWIFT;
+ (instancetype)new NS_UNAVAILABLE;
- (instancetype)init NS_UNAVAILABLE;
@end
@interface FBLPromise<Value>()
/**
Adds an object to the set of pending objects to keep strongly while the promise is pending.
Used by the Swift wrappers to keep them alive until the underlying ObjC promise is resolved.
@param object An object to add.
*/
- (void)addPendingObject:(id)object NS_REFINED_FOR_SWIFT;
@end
#ifdef FBL_PROMISES_DOT_SYNTAX_IS_DEPRECATED
#define FBL_PROMISES_DOT_SYNTAX __attribute__((deprecated))
#else
#define FBL_PROMISES_DOT_SYNTAX
#endif
@interface FBLPromise<Value>(DotSyntaxAdditions)
/**
Convenience dot-syntax wrappers for FBLPromise.
Usage: FBLPromise.pending()
FBLPromise.resolved(value)
*/
+ (FBLPromise * (^)(void))pending FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
+ (FBLPromise * (^)(id __nullable))resolved FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,43 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
FOUNDATION_EXTERN NSErrorDomain const FBLPromiseErrorDomain NS_REFINED_FOR_SWIFT;
/**
Possible error codes in `FBLPromiseErrorDomain`.
*/
typedef NS_ENUM(NSInteger, FBLPromiseErrorCode) {
/** Promise failed to resolve in time. */
FBLPromiseErrorCodeTimedOut = 1,
/** Validation predicate returned false. */
FBLPromiseErrorCodeValidationFailure = 2,
} NS_REFINED_FOR_SWIFT;
NS_INLINE BOOL FBLPromiseErrorIsTimedOut(NSError *error) NS_SWIFT_UNAVAILABLE("") {
return error.domain == FBLPromiseErrorDomain &&
error.code == FBLPromiseErrorCodeTimedOut;
}
NS_INLINE BOOL FBLPromiseErrorIsValidationFailure(NSError *error) NS_SWIFT_UNAVAILABLE("") {
return error.domain == FBLPromiseErrorDomain &&
error.code == FBLPromiseErrorCodeValidationFailure;
}
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,66 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise+Testing.h"
NS_ASSUME_NONNULL_BEGIN
/**
Miscellaneous low-level private interfaces available to extend standard FBLPromise functionality.
*/
@interface FBLPromise<Value>()
typedef void (^FBLPromiseOnFulfillBlock)(Value __nullable value) NS_SWIFT_UNAVAILABLE("");
typedef void (^FBLPromiseOnRejectBlock)(NSError *error) NS_SWIFT_UNAVAILABLE("");
typedef id __nullable (^__nullable FBLPromiseChainedFulfillBlock)(Value __nullable value)
NS_SWIFT_UNAVAILABLE("");
typedef id __nullable (^__nullable FBLPromiseChainedRejectBlock)(NSError *error)
NS_SWIFT_UNAVAILABLE("");
/**
Creates a pending promise.
*/
- (instancetype)initPending NS_SWIFT_UNAVAILABLE("");
/**
Creates a resolved promise.
@param resolution An object to resolve the promise with: either a value or an error.
@return A new resolved promise.
*/
- (instancetype)initWithResolution:(nullable id)resolution NS_SWIFT_UNAVAILABLE("");
/**
Invokes `fulfill` and `reject` blocks on `queue` when the receiver gets either fulfilled or
rejected respectively.
*/
- (void)observeOnQueue:(dispatch_queue_t)queue
fulfill:(FBLPromiseOnFulfillBlock)onFulfill
reject:(FBLPromiseOnRejectBlock)onReject NS_SWIFT_UNAVAILABLE("");
/**
Returns a new promise which gets resolved with the return value of `chainedFulfill` or
`chainedReject` blocks respectively. The blocks are invoked when the receiver gets either
fulfilled or rejected. If `nil` is passed to either block arg, the returned promise is resolved
with the same resolution as the receiver.
*/
- (FBLPromise *)chainOnQueue:(dispatch_queue_t)queue
chainedFulfill:(FBLPromiseChainedFulfillBlock)chainedFulfill
chainedReject:(FBLPromiseChainedRejectBlock)chainedReject NS_SWIFT_UNAVAILABLE("");
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,32 @@
/**
Copyright 2018 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "FBLPromise+All.h"
#import "FBLPromise+Always.h"
#import "FBLPromise+Any.h"
#import "FBLPromise+Async.h"
#import "FBLPromise+Await.h"
#import "FBLPromise+Catch.h"
#import "FBLPromise+Delay.h"
#import "FBLPromise+Do.h"
#import "FBLPromise+Race.h"
#import "FBLPromise+Recover.h"
#import "FBLPromise+Reduce.h"
#import "FBLPromise+Retry.h"
#import "FBLPromise+Then.h"
#import "FBLPromise+Timeout.h"
#import "FBLPromise+Validate.h"
#import "FBLPromise+Wrap.h"