jcubic / jquery.terminal

jQuery Terminal Emulator - JavaScript library for creating web-based terminals with custom commands
https://terminal.jcubic.pl
MIT License
3.11k stars 570 forks source link

Possible cancel of command promises #940

Closed jcubic closed 4 days ago

jcubic commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

ref: AbortSignal::timeout()

jcubic commented 4 months ago

term::read() also need a signal.

jcubic commented 1 week ago

The new API:

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:

option: errorOnAbort default set to true and two strings:

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);
});