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

Possible cancel of command promises #940

Closed
jcubic opened this issue May 3, 2024 · 4 comments
Closed

Possible cancel of command promises #940

jcubic opened this issue May 3, 2024 · 4 comments
Labels
documentation feature resolved if issue is resolved, it will be open until merge with master

Comments

@jcubic
Copy link
Owner

jcubic commented May 3, 2024

I have an idea for a new feature for jQuery Terminal

In this article are examples of using AbortController to cancel promises (by wrapping them and not resolving when there was an abort signal)

How To Cancel Any Async Task in JavaScript

This could be incorporated with the code for CTRL+D the old code with $.ajax can be removed, and replaced with handler for all promises.

@jcubic jcubic added the feature label May 3, 2024
@jcubic
Copy link
Owner Author

jcubic commented May 3, 2024

The abort signal should be exposed with an API:

terminal::signal()

So the user can use it with fetch:

$('body').terminal(async function(command) {
    const signal = this.signal();
    const res = await fetch('./api?' + command, { signal });
    if (!signal.aborted) {
        const json = await res.json();
        this.echo(json.result);
    }
});

@jcubic
Copy link
Owner Author

jcubic commented May 3, 2024

ref: AbortSignal::timeout()

@jcubic
Copy link
Owner Author

jcubic commented May 6, 2024

term::read() also need a signal.

jcubic added a commit that referenced this issue Sep 5, 2024
@jcubic
Copy link
Owner Author

jcubic commented Sep 5, 2024

The new API:

  • terminal::timeout()
  • terminal::signal()

both return signals that can be aborted with CTRL+D in addition the timeout aborts as name suggest after a timeout.

There is also a method:

  • terminal::abort(string)

option: errorOnAbort default set to true and two strings:

  • abortError: 'Abort with CTRL+D'
  • timeoutError: 'Signal timed out'

Example usage:

$('body').terminal(async () => {
   const signal = this.timeout(500);
   const res = await fetch('./api?command=' + encodeURIComponent(command), { signal });
   const json = await res.json();
   this.echo(json.result);
});

jcubic added a commit that referenced this issue Sep 5, 2024
@jcubic jcubic added the resolved if issue is resolved, it will be open until merge with master label Sep 6, 2024
jcubic added a commit that referenced this issue Sep 6, 2024
@jcubic jcubic closed this as completed Sep 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation feature resolved if issue is resolved, it will be open until merge with master
Projects
None yet
Development

No branches or pull requests

1 participant