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

Implement Promise.completeAsync(). #1838

Open
wants to merge 4 commits into
base: 8.3.0-Dev
Choose a base branch
from

Conversation

player-03
Copy link
Contributor

@player-03 player-03 commented Sep 2, 2024

Like #1828, but designed for the Promise class because I think that's more appropriate. Compared to new Future(), completeAsync() offers:

  • The option to run on the main thread.
  • The option to send progress events.
  • The ability to interrupt the job by calling promise.error().

This PR includes #1837, on the assumption that we'll merge that first.

Since we're storing more and more in `JobData`, and the background thread only needs three bits of that data, I added those to `ThreadEvent` so we don't have to pass the full object. This may improve performance in HTML5 specifically, where passing a class instance incurs an overhead.
Compared to `new Future()`, this offers:

- The option to run on the main thread.
- The option to send progress events.
- The ability to interrupt the job by calling `promise.error()`.
@player-03
Copy link
Contributor Author

For convenience, here's the new function's documentation and signature:

/**
	Runs the given function asynchronously, and resolves this `Promise` with
	the complete, error, and/or progress events sent by that function.
	Sample usage:
	```haxe
	function examplePromise():Future<String>
	{
		var promise = new Promise<String>();
		promise.completeAsync(function(state:State, output:WorkOutput):Void
			{
				output.sendProgress({progress:state.progress, total:10});
				state.progress++;
				if (state.progress == 10)
				{
					output.sendComplete("Done!");
				}
			},
			{progress: 0}, MULTI_THREADED);
		return promise.future;
	}
	var future = examplePromise();
	future.onComplete(function(message) { trace(message); });
	future.onProgress(function(loaded, total) { trace("Progress: " + loaded + ", " + total); });
	```
	@param doWork A function to perform work asynchronously. For best results,
	see the guidelines in the `ThreadPool` class overview.
	@param state The value to pass to `doWork`.
	@param mode Which mode to run the job in: `SINGLE_THREADED` or `MULTI_THREADED`.
**/
public function completeAsync(doWork:WorkFunction<State->WorkOutput->Void>, ?state:State, ?mode:ThreadMode = MULTI_THREADED):Void

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant