refactor(js): send 'autosave:end' when there are no more pending promises

This commit is contained in:
simon lehericey 2022-10-24 20:03:44 +02:00
parent 00d1552060
commit f4f40ded6c

View file

@ -37,6 +37,7 @@ export class AutosaveController extends ApplicationController {
#abortController?: AbortController;
#latestPromise = Promise.resolve();
#needsRetry = false;
#pendingPromiseCount = 0;
connect() {
this.#latestPromise = Promise.resolve();
@ -119,11 +120,15 @@ export class AutosaveController extends ApplicationController {
}
private didSucceed() {
this.globalDispatch('autosave:end');
this.#pendingPromiseCount -= 1;
if (this.#pendingPromiseCount == 0) {
this.globalDispatch('autosave:end');
}
}
private didFail(error: ResponseError) {
this.#needsRetry = true;
this.#pendingPromiseCount -= 1;
this.globalDispatch('autosave:error', { error });
}
@ -179,6 +184,8 @@ export class AutosaveController extends ApplicationController {
}
}
this.#pendingPromiseCount++;
return httpRequest(form.action, {
method: 'patch',
body: formData,