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

fix: typedef default onfulfilled handler for E.when #1233

Merged
merged 1 commit into from
Jul 7, 2022
Merged

Conversation

turadg
Copy link
Member

@turadg turadg commented Jul 7, 2022

E.when return type assumed an onfulfilled handler is provided. That handler is optional and in https://github.com/Agoric/agoric-sdk/blob/21eb21d43390ba51c4d2bc5baecb1b684ee66c52/packages/run-protocol/src/vpool-xyk-amm/addPool.js#L70-L76 it wasn't used, which caused an error in the return type.

This makes the return type default when a handler isn't provided.

@turadg turadg requested review from mhofman and erights July 7, 2022 14:39
@turadg turadg marked this pull request as draft July 7, 2022 15:00
@mhofman
Copy link
Contributor

mhofman commented Jul 7, 2022

I don't understand what U was doing

The return type of a reaction is generally not the same as the input type, which is why Promise(Like)#then() has a similar dual template types. https://github.com/microsoft/TypeScript/blob/59238dbf90f7fb002357851940ced2ae42800e69/lib/lib.es5.d.ts#L1440

Comment on lines +54 to +55
E.when(Promise.all([aPromise, onePromise, remoteString])).then(
([str, num, remote]) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

The problem here is likely in a misusage of E.when, which is expected to be used like the following:

Suggested change
E.when(Promise.all([aPromise, onePromise, remoteString])).then(
([str, num, remote]) => {
E.when(Promise.all([aPromise, onePromise, remoteString]), ([str, num, remote]) => {

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Contributor

@mhofman mhofman Jul 7, 2022

Choose a reason for hiding this comment

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

Right, but I think it's optional in the sense that the expectation is to use either onfulfilled or onrejected. Using .then() with E.when() is somewhat of an anti-pattern.

That said, to enable this case with the types, I do believe that defaulting U to T will work.

Copy link
Member Author

Choose a reason for hiding this comment

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

"expectation" and "anti-pattern" are fuzzy to me wrt the type defs. The type checker has only errors not warnings :)

If you think the type should require at least one handler, I can define the type that way. Though I think you're saying that it should allow them both to be omitted and your solution sounds right. I'll try it.

Copy link
Member

Choose a reason for hiding this comment

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

Speaking only for then, the callback and errback default to exactly value => value and error => { throw error } respectively. They can both be omitted. when should be similar.

@@ -257,11 +257,11 @@ interface EProxy {
* E.when(x, res, rej) is equivalent to
* HandledPromise.resolve(x).then(res, rej)
*/
readonly when: <T, U>(
readonly when: <T>(
Copy link
Contributor

Choose a reason for hiding this comment

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

I think by defaulting U to T (for when neither reactions are used) might solve the problem

Suggested change
readonly when: <T>(
readonly when: <T, U = T>(

@turadg turadg marked this pull request as ready for review July 7, 2022 17:17
@turadg turadg changed the title fix: type of E.when fix: typedef default onfulfilled handler for E.when Jul 7, 2022
@turadg turadg requested a review from mhofman July 7, 2022 17:20
@@ -46,3 +46,15 @@ const foo2 = async (a: FarRef<{ bar(): string; baz: number }>) => {
// @ts-expect-error - calling directly is valid but not yet in the typedef
a.bar;
};

// when
Copy link
Contributor

Choose a reason for hiding this comment

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

If we're adding when type tests, do you mind also adding a test for the "normal" use case with a onfulfilled callback?

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.

3 participants