Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions src/dialogs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import restoreTheme from "lib/restoreTheme";

let loaderIsImmortal = false;
let onCancelCallback = null;
let cancelButtonTimeout = null;
let $currentDialog = null;
let $currentMask = null;
const titleLoaderId = "__title-loader";

/**
* @typedef {object} LoaderOptions
* @property {number} timeout Timeout in milliseconds after which the loader will be shown
* @property {function():void} oncancel Callback function to be called when the loader is shown
* @property {number} timeout Delay before the cancel button is shown, in milliseconds
* @property {function():void} oncancel Callback invoked only when the user cancels
*/

/**
Expand Down Expand Up @@ -64,19 +65,21 @@ function create(titleText, message = "", options = {}) {
</div>
);

const { timeout, oncancel } = options;
if (typeof oncancel === "function") {
onCancelCallback = oncancel;
}
clearTimeout(cancelButtonTimeout);
cancelButtonTimeout = null;
onCancelCallback =
typeof options.oncancel === "function" ? options.oncancel : null;

if (typeof timeout === "number") {
setTimeout(() => {
if (typeof options.timeout === "number") {
cancelButtonTimeout = setTimeout(() => {
cancelButtonTimeout = null;
if (!$dialog.isConnected) return;
$dialog.append(
<div className="button-container">
<button onclick={destroy}>{strings.cancel}</button>
<button onclick={cancel}>{strings.cancel}</button>
</div>,
);
}, timeout);
}, options.timeout);
}

if (!$oldLoader) {
Expand All @@ -98,6 +101,13 @@ function create(titleText, message = "", options = {}) {
};
}

function cancel() {
const callback = onCancelCallback;
onCancelCallback = null;
destroy();
callback?.();
}

function createTitleLoader() {
const $titleLoader = tag.get(`#${titleLoaderId}`) || (
<span id={titleLoaderId} innerHTML={createTailSpinSvg()}></span>
Expand All @@ -116,6 +126,9 @@ function createTitleLoader() {
function destroy() {
const loaderDiv = tag.get("#__loader");
const mask = tag.get("#__loader-mask");
clearTimeout(cancelButtonTimeout);
cancelButtonTimeout = null;
onCancelCallback = null;
restoreTheme();

if (!loaderDiv && !mask) {
Expand All @@ -128,7 +141,6 @@ function destroy() {
actionStack.unfreeze();
if (loaderDiv?.isConnected) loaderDiv.remove();
if (mask?.isConnected) mask.remove();
onCancelCallback?.();
}, 300);
}

Expand Down
17 changes: 17 additions & 0 deletions src/fileSystem/sftp.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,23 @@ class SftpClient {
}
}

/**
* Tests a profile once without the normal remote-filesystem retry policy.
* @param {string} requestID Native request ID used for cancellation
*/
testConnection(requestID) {
return new Promise((resolve, reject) => {
sftp.testProfile(this.#profileID, requestID, 10000, resolve, reject);
});
}

/** Cancel an in-flight profile test. */
cancelConnection(requestID) {
return new Promise((resolve) => {
sftp.cancelConnection(requestID, resolve, resolve);
});
}

async #connectWithRetry() {
const attempts = settings.value.retryRemoteFsAfterFail
? this.#MAX_TRY + 1
Expand Down
16 changes: 11 additions & 5 deletions src/lib/remoteStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default {
try {
loader.create(strings["add ftp"], strings["connecting..."], {
timeout: 10000,
callback() {
oncancel() {
stopConnection = true;
},
});
Expand Down Expand Up @@ -176,6 +176,8 @@ export default {
existingProfile = null,
} = {}) {
let stopConnection = false;
let connection;
const connectionRequestID = `add-sftp-${helpers.uuid()}`;
if (existingProfile?.profileId) {
try {
const saved = await getSftpProfileInfo(existingProfile.profileId);
Expand Down Expand Up @@ -242,17 +244,21 @@ export default {
const url = createSftpProfileUrl(profile.profileId);

loader.create(strings["add sftp"], strings["connecting..."], {
timeout: 10000,
callback() {
timeout: 0,
oncancel() {
stopConnection = true;
connection?.cancelConnection(connectionRequestID);
},
});
const connection = Sftp(null, 22, null, {
connection = Sftp(null, 22, null, {
profileID: profile.profileId,
});

try {
const [home] = await Promise.all([connection.pwd(), loadAd()]);
const [home] = await Promise.all([
connection.testConnection(connectionRequestID),
loadAd(),
]);

if (stopConnection) {
stopConnection = false;
Expand Down
3 changes: 1 addition & 2 deletions src/pages/fileBrowser/fileBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1468,8 +1468,7 @@ function FileBrowserInclude(mode, info, doesOpenLast = true) {
const timeout = setTimeout(() => {
loader.create(name, strings.loading + "...", {
timeout: loaderTimeout,
callback() {
loader.destroy();
oncancel() {
navigate("/", "/");
progress[id] = false;
},
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/sftp/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ interface Sftp {
exec(command: String, onSucess: (res: ExecResult)=>void, onFail: (err: any) => void): void;
/** Connects using credentials held by the native profile store. */
connectUsingProfile(profileId: String, onSuccess: () => void, onFail: (err: any) => void): void;
/** Tests a profile once and returns its remote working directory. */
testProfile(profileId: String, requestId: String, timeout: Number, onSuccess: (home: String) => void, onFail: (err: any) => void): void;
/** Cancels an in-flight profile connection or test. */
cancelConnection(requestId: String, onSuccess: () => void, onFail: (err: any) => void): void;
saveProfile(profileId: String | null, host: String, port: Number, username: String, authType: String, password: String, keyFile: String, passphrase: String, onSuccess: (profileId: String) => void, onFail: (err: any) => void): void;
editProfile(profileId: String | null, host: String, port: Number, username: String, authType: String, password: String, keyFile: String, passphrase: String, onSuccess: (profile: SftpProfileInfo & {profileId: string}) => void, onFail: (err: any) => void): void;
getProfileInfo(profileId: String, onSuccess: (profile: SftpProfileInfo & {profileId: string}) => void, onFail: (err: any) => void): void;
Expand Down
Loading