-
-
Notifications
You must be signed in to change notification settings - Fork 375
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
Add standard convenience functions for creating Promises to resolve groups of Futures. #1844
Open
EliteMasterEric
wants to merge
2
commits into
openfl:develop
Choose a base branch
from
FunkinCrew:feature/promise-handlers-public
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
package lime.app; | ||
|
||
import lime.app.Promise; | ||
import lime.app.Future; | ||
|
||
/** | ||
* A set of static utility functions for working with Promises. | ||
* This includes functions which resolve groups of Futures. | ||
*/ | ||
class Promises { | ||
/** | ||
* Creates a promise which fulfills when all the input promises resolve successfully, | ||
* with an array of the result values. | ||
* Rejects when any of the input promises reject, with the first rejection reason. | ||
* @param futures A list of Futures to resolve. | ||
* @return A Future for a list of result values. | ||
*/ | ||
public static function all<T>(futures:Array<Future<T>>):Future<Array<T>> { | ||
var promise:Promise<Array<T>> = new Promise<Array<T>>(); | ||
var results:Array<T> = []; | ||
|
||
for (future in futures) { | ||
future.onComplete(function(result) { | ||
results.push(result); | ||
if (results.length == futures.length) { | ||
promise.complete(results); | ||
} | ||
}); | ||
future.onError(function(error) { | ||
promise.error(error); | ||
}); | ||
} | ||
|
||
return promise.future; | ||
} | ||
|
||
/** | ||
* Creates a promise which fulfills when all the input promises settle (whether success or failure). | ||
* Returns an array of objects that describe the outcome of each promise. | ||
* @param futures A list of Futures to resolve. | ||
* @return A Future for a list of result values. | ||
*/ | ||
public static function allSettled<T>(futures:Array<Future<T>>):Future<Array<PromiseResult<T>>> { | ||
var promise:Promise<Array<PromiseResult<T>>> = new Promise<Array<PromiseResult<T>>>(); | ||
var results:Array<PromiseResult<T>> = []; | ||
|
||
for (future in futures) { | ||
future.onComplete(function(value) { | ||
results.push(PromiseResult.fulfilled(value)); | ||
if (results.length == futures.length) { | ||
promise.complete(results); | ||
} | ||
}); | ||
future.onError(function(error) { | ||
results.push(PromiseResult.rejected(value)); | ||
if (results.length == futures.length) { | ||
promise.complete(results); | ||
} | ||
}); | ||
} | ||
|
||
return promise.future; | ||
} | ||
|
||
/** | ||
* Creates a promise which fulfills when any of the input promises resolve successfully. | ||
* Returns the first fulfilled promise. If all promises reject, the promise will be rejected with the list of rejection reasons. | ||
* @param futures A list of Futures to resolve. | ||
* @return A Future for a result value. | ||
*/ | ||
public static function any<T>(futures:Array<Future<T>>):Future<T> { | ||
var promise:Promise<T> = new Promise<T>(); | ||
var errors:Array<Dynamic> = []; | ||
|
||
for (future in futures) { | ||
future.onComplete(function(value) { | ||
promise.complete(value); | ||
}); | ||
future.onError(function(error) { | ||
errors.push(error); | ||
if (errors.length == futures.length) { | ||
promise.error(errors); | ||
} | ||
}); | ||
} | ||
|
||
return promise.future; | ||
} | ||
|
||
/** | ||
* Creates a promise which fulfills when any of the input promises settle. | ||
* Returns an object that describes the outcome of the first settled promise (whether success or failure). | ||
* @param futures A list of Futures to resolve. | ||
* @return A Future for a result value. | ||
*/ | ||
public static function race<T>(futures:Array<Future<T>>):Future<PromiseResult<T>> { | ||
var promise:Promise<PromiseResult<T>> = new Promise<PromiseResult<T>>(); | ||
for (future in futures) { | ||
future.onComplete(function(value) { | ||
promise.complete(PromiseResult.fulfilled(value)); | ||
}); | ||
future.onError(function(error) { | ||
promise.complete(PromiseResult.rejected(error)); | ||
}); | ||
} | ||
return promise.future; | ||
} | ||
} | ||
|
||
class PromiseResult<T> { | ||
player-03 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* The current state of the promise. | ||
*/ | ||
public var state:PromiseState; | ||
/** | ||
* The value of the promise, if it resolved as Fulfilled. | ||
*/ | ||
public var value:Null<T>; | ||
/** | ||
* The error of the promise, if it resolved as Rejected. | ||
*/ | ||
public var error:Null<Dynamic>; | ||
|
||
private function new(state:PromiseState, value:Null<T>, error:Null<Dynamic>):Void { | ||
this.state = state; | ||
this.value = value; | ||
this.error = error; | ||
} | ||
|
||
public static function pending<T>():PromiseResult<T> { | ||
return new PromiseResult<T>(PromiseState.Pending, null, null); | ||
} | ||
|
||
public static function fulfilled<T>(value:T):PromiseResult<T> { | ||
return new PromiseResult<T>(PromiseState.Fulfilled, value, null); | ||
} | ||
|
||
public static function rejected<T>(error:Dynamic):PromiseResult<T> { | ||
return new PromiseResult<T>(PromiseState.Rejected, null, error); | ||
} | ||
} | ||
|
||
enum PromiseState { | ||
/** | ||
* This promise has not yet resolved. | ||
*/ | ||
Pending; | ||
/** | ||
* This promise has resolved with a value. | ||
*/ | ||
Fulfilled; | ||
/** | ||
* This promise has resolved with an error. | ||
*/ | ||
Rejected; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not add the functions to
Promise
?