Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

func race(fulfilled:) for Objective-C #1269

Merged
merged 3 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Sources/fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extern NSString * __nonnull const PMKErrorDomain;
#define PMKOperationFailed 8l
#define PMKTaskError 9l
#define PMKJoinError 10l
#define PMKNoWinnerError 11l


#ifdef __cplusplus
Expand Down Expand Up @@ -160,6 +161,11 @@ extern AnyPromise * __nonnull dispatch_promise_on(dispatch_queue_t __nonnull que
*/
extern AnyPromise * __nonnull PMKRace(NSArray * __nonnull promises) NS_REFINED_FOR_SWIFT;

/**
Returns a new promise that resolves with the value of the first fulfilled promise in the provided array of promises.
*/
extern AnyPromise * __nonnull PMKRaceFulfilled(NSArray * __nonnull promises) NS_REFINED_FOR_SWIFT;

#ifdef __cplusplus
} // Extern C
#endif
26 changes: 26 additions & 0 deletions Sources/race.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,29 @@
}
}];
}

/**
Waits for one promise to fulfill

@note If there are no fulfilled promises, the returned promise is rejected with `PMKNoWinnerError`.
@param promises The promises to fulfill.
@return The promise that was fulfilled first.
*/
AnyPromise *PMKRaceFulfilled(NSArray *promises) {
__block int32_t countdown = (int32_t)[promises count];

return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
for (__strong AnyPromise* promise in promises) {
[promise __pipe:^(id value) {
if (IsError(value)) {
if (OSAtomicDecrement32(&countdown) == 0) {
id err = [NSError errorWithDomain:PMKErrorDomain code:PMKNoWinnerError userInfo:@{NSLocalizedDescriptionKey: @"PMKRaceFulfilled(nil)"}];
resolve(err);
}
} else {
resolve(value);
}
}];
}
}];
}
1 change: 0 additions & 1 deletion Sources/race.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ public func race<U: Thenable>(fulfilled thenables: [U]) -> Promise<U.T> {
}
case .fulfilled(let value):
guard rp.isPending else { return }
countdown = 0
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An unnecessary line.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, but there is no reason to remove it. It is possible this is required. Please leave it.

rp.box.seal(.fulfilled(value))
}
}
Expand Down
38 changes: 38 additions & 0 deletions Tests/CoreObjC/AnyPromiseTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,44 @@ - (void)test_race {
[self waitForExpectationsWithTimeout:1 handler:nil];
}

- (void)test_race_fullfilled {
id ex = [self expectationWithDescription:@""];
NSArray* promises = @[
PMKAfter(1).then(^{ return dummyWithCode(1); }),
PMKAfter(2).then(^{ return dummyWithCode(2); }),
PMKAfter(5).then(^{ return @1; }),
PMKAfter(4).then(^{ return @2; }),
PMKAfter(3).then(^{ return dummyWithCode(3); })
];
PMKRaceFulfilled(promises).then(^(id obj){
XCTAssertEqual(2, [obj integerValue]);
[ex fulfill];
}).catch(^{
XCTFail();
[ex fulfill];
});
[self waitForExpectationsWithTimeout:10 handler:nil];
}

- (void)test_race_fullfilled_with_no_winner {
id ex = [self expectationWithDescription:@""];
NSArray* promises = @[
PMKAfter(1).then(^{ return dummyWithCode(1); }),
PMKAfter(2).then(^{ return dummyWithCode(2); }),
PMKAfter(3).then(^{ return dummyWithCode(3); })
];
PMKRaceFulfilled(promises).then(^(id obj){
XCTFail();
[ex fulfill];
}).catch(^(NSError *e){
XCTAssertEqual(e.domain, PMKErrorDomain);
XCTAssertEqual(e.code, PMKNoWinnerError);
XCTAssertEqualObjects(e.userInfo[NSLocalizedDescriptionKey], @"PMKRaceFulfilled(nil)");
[ex fulfill];
});
[self waitForExpectationsWithTimeout:10 handler:nil];
}

- (void)testInBackground {
id ex = [self expectationWithDescription:@""];
PMKAfter(0.1).thenInBackground(^{ [ex fulfill]; });
Expand Down