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

Major API change on JSClosure #113

Merged
merged 11 commits into from
Jan 4, 2021
Merged

Major API change on JSClosure #113

merged 11 commits into from
Jan 4, 2021

Conversation

kateinoigakukun
Copy link
Member

@kateinoigakukun kateinoigakukun commented Jan 2, 2021

Motivation

While integrating async/await feature, I found JSPromise has too complex API. (e.g. too many overloads, unsafe generic parameters, and lifetime management)
So I want to refine the API before async/await integration. As a first step, I changed JSClosure API.

Changes

  • Add JSOneshotClosure which provides an ability to invoke a closure at most once. This will be suitable to use with functions like Promise.then
  • Remove JSClosure.init(_ body: @escaping ([JSValue]) -> Void) overload, which forces users to write result type in closure.
  • Add JSClosure lifetime test suites

- Add `JSOneshotClosure` which provide an ability to invoke a closure
  at most once.
- Remove `JSClosure.init(_ body: @escaping ([JSValue]) -> Void)`
  overload, which forces users to write result type in closure.
- Add JSClosure lifetime test suites
@github-actions
Copy link

github-actions bot commented Jan 2, 2021

Time Change: -525.25ms (6%) ✅

Total Time: 8,162ms

Test name Duration Change
Serialization/Write JavaScript number directly 145.75ms -18.5ms (12%) 👏
Serialization/Write JavaScript string directly 154.5ms -10ms (6%)
Serialization/Swift Int to JavaScript 2,602.5ms -188.25ms (7%)
Serialization/Swift String to JavaScript 2,665.25ms -179ms (6%)
ℹ️ View Unchanged
Test name Duration Change
Object heap/Increment and decrement RC 2,594ms -129.5ms (4%)

performance-action

@kateinoigakukun kateinoigakukun requested review from MaxDesiatov and a team January 2, 2021 16:45
Copy link
Contributor

@MaxDesiatov MaxDesiatov left a comment

Choose a reason for hiding this comment

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

Overall this seems legit with a few suggestions in comments.

Copy link
Contributor

@MaxDesiatov MaxDesiatov left a comment

Choose a reason for hiding this comment

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

I wasn't sure about that JSClosure.init overload myself when I introduced, but you're right it brings more harm than good. Also, great idea to simplify a lot of things with JSOneshotClosure! 👍

@MaxDesiatov MaxDesiatov requested a review from a team January 2, 2021 17:07
@kateinoigakukun
Copy link
Member Author

@swiftwasm/jskit-team Could anyone review this change? I want two or more approvals for this kind of API change before merging 🙏

@MaxDesiatov MaxDesiatov requested a review from j-f1 January 3, 2021 13:57
Copy link
Member

@j-f1 j-f1 left a comment

Choose a reason for hiding this comment

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

at a desktop now. LGTM except for these things

Sources/JavaScriptKit/BasicObjects/JSTimer.swift Outdated Show resolved Hide resolved
Sources/JavaScriptKit/BasicObjects/JSPromise.swift Outdated Show resolved Hide resolved
Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift Outdated Show resolved Hide resolved
Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift Outdated Show resolved Hide resolved
Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift Outdated Show resolved Hide resolved
@kateinoigakukun
Copy link
Member Author

@j-f1 I addressed reviewed points. Could you take a look again?

Sources/JavaScriptKit/JSValue.swift Outdated Show resolved Hide resolved
Comment on lines +506 to +509
timeout = JSTimer(millisecondsDelay: timeoutMilliseconds, isRepeating: false) {
fatalError("timer should be cancelled")
}
timeout = nil
Copy link
Member

Choose a reason for hiding this comment

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

Totally happy to punt this to another issue, but since there isn’t necessarily a need to cancel a setTimeout, could there be an API that doesn’t require holding the JSTimer instance? I guess users could just do

JSObject.global.setTimeout(JSOneshotClosure {
  // ...
  return undefined
}, 1000)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we can provide such API in this signature for example.

JSTimer.timeout(millisecondsDelay: 100) {
    // ...
}

This PR is already too large, so let's implement it in another PR 👍

Co-authored-by: Jed Fox <git@jedfox.com>
Copy link
Member

@j-f1 j-f1 left a comment

Choose a reason for hiding this comment

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

lol oops accounts are hard

@kateinoigakukun kateinoigakukun merged commit 55a4bfa into main Jan 4, 2021
@kateinoigakukun kateinoigakukun deleted the katei/update-jsclosure branch January 4, 2021 00:55
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.

4 participants