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

Async/await syntax #2958

Open
OCTAGRAM opened this issue Dec 4, 2020 · 11 comments
Open

Async/await syntax #2958

OCTAGRAM opened this issue Dec 4, 2020 · 11 comments
Milestone

Comments

@OCTAGRAM
Copy link

OCTAGRAM commented Dec 4, 2020

Hello!

With async/await we are finally (almost) able to write clean synchronous-style browser code. But Objective-J did not incorporate it in any way IIUC.

It is not an easy move, but what are the plans about it?

For instance, make all Objective-J methods implementations async, and all Objective-J method invocations await.

Or, like in ordinary JavaScript, move the burden of memorizing who's async, who's not, and forcing to write await by hand. But at least this is already something.

@cappbot cappbot added this to the Someday milestone Dec 4, 2020
@cappbot cappbot added the #new label Dec 4, 2020
@cappbot
Copy link

cappbot commented Dec 4, 2020

Milestone: Someday. Label: #new. What's next? A reviewer should examine this issue.

@mrcarlberg
Copy link
Member

Hello!

I guess this will be done in multiple steps for Objective-J. As it is a superset of Javascript we first need to support ECMAScript 2017. When that is done we can start incorporate it to the Objective-J language.

With that said, do you have any ideas or suggestions that you want in the language?

@OCTAGRAM
Copy link
Author

OCTAGRAM commented Dec 4, 2020

For me would be easier to make everything async. But then quite many assumptions ruin. Handler may semi-execute when handling DOM event, and then next DOM event comes in between. But we cannot let handler fire. The environment becomes "multithreaded" otherwise.

I rely on Cappuccino to do the heavy lifting now, and the same way I would rely on Cappuccino to simulate "single main thread" while making all methods async. DOM elements must collect events or ignore them when "single main thread" is busy. That is, I guess, a serious architecture change.

In the short run I propose explicit async/await. Maybe @async before method. Maybe async before result type.

Third possible solution is based on making everything async, but instead of enabling long-running handlers, denying them to run long. DOM event handlers must enter scheduler, which is to replace window.setTimeout(…, 0);, and this scheduler runs Objective-J handler and expects completion. [CPThread sleepForTimeInterval:] maps to await new Promise, setTimeout, etc. for "non-main thread", but throws exception for "main thread" (bool global variable can be responsible for thinking we are in "main thread"). HTTP request throws exception (or, despite all recommendations, it executes synchronously). To start some long-running process Objective-J handler may use CPThread, which maps to adding jobs to scheduler.

So Objective-J event handler can launch several CPThreads, and all of them may possibly use long-running functions. Objective-J event handler "in the main thread" may possibly try to wait for their completion, and indeed all may end fast, thanks to the scheduler. But if not, if some threshold is passed or if scheduler is out of work (all threads "await" something that is not going to happen soon), Objective-J event handler is woken up with failure or exception.

This approach does not require big UI architecture changes.

@daboe01
Copy link
Contributor

daboe01 commented Dec 6, 2020

maybe we can/should integrate this with CPOperationQueue somehow?

@enquora
Copy link
Contributor

enquora commented Dec 12, 2020

Swift Evolution is now discussing async/await) including interoperability with Objective-C and its runtime (under Related Proposals).

Having a CPOperationQueue which didn't depend on the runloop would be particularly nice.

@mrcarlberg
Copy link
Member

We have to keep things separate. I can at least find these topic in this interesting discussion:

  1. Add async/await support in the Objective-J language. A good reference is the Swift Evolution link above.
  2. The problem with the main thread that sometimes is heavily congested. My thinking here is to move everything in Cappuccino to a Web Worker (https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API). The only thing we will leave in the main thread is a DOMDisplayServer. To make DOM changes the Cappuccino framework (that is running in a Web Worker) will send the changes to the DOMDisplayServer (main thread).
  3. Make CPOperationQueue work better. My thinking here is to allow CPOperationQueue to use a Web Worker to be able to run on their own.

With this said I think this issue should be about 1. Add async/await support in the Objective-J language. Feel free to open new issues for the other issues.

Really interesting topics

@daboe01
Copy link
Contributor

daboe01 commented Mar 2, 2021

-#new
+Objective-J

@cappbot
Copy link

cappbot commented Mar 2, 2021

Milestone: Someday. Label: Objective-J. What's next? A reviewer should examine this issue.

@cappbot cappbot added Objective-J and removed #new labels Mar 2, 2021
@daboe01
Copy link
Contributor

daboe01 commented Feb 11, 2022

@mrcarlberg is this still an issue?

@mrcarlberg
Copy link
Member

Yes, I believe issue 1. above is fixed in the Node version when async/await syntax was added.

@daboe01
Copy link
Contributor

daboe01 commented Feb 15, 2022

@mrcarlberg i think we can close this one. as you said we can open new issues for the Web Worker stuff as needed. do you agree?

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

No branches or pull requests

5 participants