Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add a possibility to cancel a request. Resolves #16 #17

Merged
merged 14 commits into from
Nov 29, 2024

Conversation

dhuebner
Copy link
Member

@dhuebner dhuebner commented Dec 13, 2023

Manually tested.

TODO:

  • Discuss the API
  • Add tests

@dhuebner dhuebner force-pushed the dhuebner/canelPendingRequest-16 branch from b096532 to cd7681d Compare December 13, 2023 15:36
packages/vscode-messenger-common/src/messages.ts Outdated Show resolved Hide resolved
packages/vscode-messenger-common/src/messages.ts Outdated Show resolved Hide resolved
packages/vscode-messenger-common/src/messages.ts Outdated Show resolved Hide resolved
packages/vscode-messenger-common/src/messages.ts Outdated Show resolved Hide resolved
packages/vscode-messenger-common/src/messages.ts Outdated Show resolved Hide resolved
@dhuebner dhuebner marked this pull request as ready for review December 20, 2023 12:15
@dhuebner dhuebner changed the title WIP: Add a possibility to cancel a request. Resolves #16 Add a possibility to cancel a request. Resolves #16 Jan 8, 2024
@dhuebner dhuebner requested a review from spoenemann January 12, 2024 10:43
Copy link
Member

@spoenemann spoenemann left a comment

Choose a reason for hiding this comment

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

Nice! I'd consider to adapt the naming to other libraries to avoid confusion, see remarks below.

Please remove packages/vscode-messenger-devtools/webview-ui/package-lock.json – there should be only a single package-lock in the root.

packages/vscode-messenger-common/src/messages.ts Outdated Show resolved Hide resolved
packages/vscode-messenger-common/src/messages.ts Outdated Show resolved Hide resolved
packages/vscode-messenger-common/src/messages.ts Outdated Show resolved Hide resolved
packages/vscode-messenger-common/src/messages.ts Outdated Show resolved Hide resolved
packages/vscode-messenger-common/src/messages.ts Outdated Show resolved Hide resolved
packages/vscode-messenger-webview/src/messenger.ts Outdated Show resolved Hide resolved
@dankeboy36
Copy link
Contributor

dankeboy36 commented Nov 17, 2024

Hello 👋 @dhuebner, do you plan to address the review remarks regarding the API? Thank you!

@TypeFox TypeFox deleted a comment from congziqi77 Nov 18, 2024
@dhuebner
Copy link
Member Author

@dankeboy36
Hi! I can have a look at end of this week, I hope.

@spoenemann
Copy link
Member

@dhuebner we should consider using AbortSignal as suggested in #16. It would be nice to do things the standard way if possible.

@dhuebner
Copy link
Member Author

@spoenemann

we should consider using AbortSignal as suggested in #16

Using an own Implementation makes us more flexible in implementing new things and extending existing. I think this is also a reason why vs-code has not re-used AbortSignal but implemented something own.

Other suggestion were applied! The only open conversation is #17 (comment) where I didn't get your concerns. Maybe we can chat in Around about that?

@dhuebner dhuebner force-pushed the dhuebner/canelPendingRequest-16 branch from 2d3babb to e1b5289 Compare November 22, 2024 13:52
Copy link
Member

@spoenemann spoenemann left a comment

Choose a reason for hiding this comment

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

This is in good shape, thank you! A few more remarks before we can merge it.

packages/vscode-messenger-devtools/webview-ui/package-lock.json should be removed – there should be only one lock file in the project root.

Idea: is it possible to add an adapter class that creates a CancellationToken for a given AbortSignal? I think the concepts are very similar. Internally, it's a good idea to stay compatible with the way vscode-jsonrpc handles this.

// Request finished, remove listener
listener.dispose();
}).catch((err) =>
this.log(`Pending request rejected: ${String(err)}`)
Copy link
Member

Choose a reason for hiding this comment

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

I think appending catch to finally has different semantics than the other way around. See also corresponding code in the webview.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think appending catch to finally has different semantics

I thought it is basically the same as:

pendingRequest.result.finally(() => {
    // Request finished, remove listener
    listener.dispose();
});
pendingRequest.result.catch((err) =>
    this.log(`Pending request rejected: ${String(err)}`)
);

Is it not just the execution order of attached callbacks that changes? Which is in this case doesn't matter.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure. My impression after reading https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/finally#return_value is that the promise returned by finally behaves differently from the original promise.

Copy link
Member Author

Choose a reason for hiding this comment

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

It would be strange, if the kind of how you use the API (directly or chained) would change the behavior. But I will not wonder if it is like that in JS... However, I've tried different order and direct/chained here and it behaves the same.

@dhuebner
Copy link
Member Author

packages/vscode-messenger-devtools/webview-ui/package-lock.json should be removed – there should be only one lock file in the project root.

This is because devtools package is not part of the main build. I already prepared a branch to add it to the main build. See: https://github.com/TypeFox/vscode-messenger/tree/dhuebner/add_DevTools_toBuild

I will open a PR after this one is merged.

Idea: is it possible to add an adapter class that creates a CancellationToken for a given AbortSignal? I think the concepts are very similar. Internally, it's a good idea to stay compatible with the way vscode-jsonrpc handles this.

Should it be a part of this PR, on can we do it later?

@spoenemann
Copy link
Member

We can do it later, I was just curious of the new API proposed here is already conceptually compatible with AbortSignal, or we need to change something.

@spoenemann
Copy link
Member

@dhuebner could you try this?

export function createCancellationToken(signal: AbortSignal): CancellationToken {
    return {
        get isCancellationRequested(): boolean {
            return signal.aborted;
        },

        onCancellationRequested: (callback: (reason: string) => void) => {
            const listener = () => callback(String(signal.reason));
            signal.addEventListener('abort', listener);
            return {
                dispose: () => signal.removeEventListener('abort', listener)
            }
        }
    };
}

@dhuebner
Copy link
Member Author

@dhuebner could you try this?

Works, I also added an additional test.

packages/vscode-messenger-common/src/messages.ts Outdated Show resolved Hide resolved
* @param signal An AbortSignal to create a CancellationToken for.
* @returns A CancellationToken that is linked to the given signal.
*/
export function createCancellationToken(signal: AbortSignal): CancellationToken {
Copy link
Member

Choose a reason for hiding this comment

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

Isn't AbortSignal available for Node.js, too? Then we could move this to the common package.

Copy link
Member Author

@dhuebner dhuebner Nov 29, 2024

Choose a reason for hiding this comment

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

I think the node version has a slightly different method signatures. In my case AbortController.abort() has no reason parameter. I also not sure how common AbortSignal in vs-code extension context is.

@dhuebner dhuebner merged commit 0fcdb39 into main Nov 29, 2024
1 check passed
@dhuebner dhuebner deleted the dhuebner/canelPendingRequest-16 branch November 29, 2024 10:41
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants