Closed jcubic closed 4 days 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);
}
});
term::read()
also need a signal.
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);
});
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.