From 3ad3eab9d441859deeb597e9547e22bc961aa18f Mon Sep 17 00:00:00 2001 From: "Stephane Lacoin (aka nxmatic)" Date: Mon, 18 Mar 2024 11:08:32 +0100 Subject: [PATCH] BDE-226 enhanced repository indexing notification --- src/main/repository-indexer.js | 44 ++++++++++++++++++++++++---------- src/main/server-connector.js | 13 +++++----- src/popup/index.js | 44 +++++++++++----------------------- 3 files changed, 52 insertions(+), 49 deletions(-) diff --git a/src/main/repository-indexer.js b/src/main/repository-indexer.js index ecddcd93..b0fdc1b3 100644 --- a/src/main/repository-indexer.js +++ b/src/main/repository-indexer.js @@ -17,6 +17,7 @@ limitations under the License. class RepositoryIndexer { constructor(worker) { this.worker = worker; + this.waiting = undefined; // Bind methods Object.getOwnPropertyNames(Object.getPrototypeOf(this)) @@ -26,30 +27,47 @@ class RepositoryIndexer { }); } - reindex(input) { + reindex(input = undefined) { return this.worker.serverConnector - .withNuxeo() - .then((nuxeo) => { - const operation = nuxeo.operation('Elasticsearch.Index'); - if (input) { - operation.input(input); - } - return operation.execute(); - }) + .executeOperation('Elasticsearch.Index', {}, input) .then(() => { - this.worker.desktopNotifier.notify('success', { - title: 'Success!', - message: `Your repository index is rebuilding for ${input ? JSON.stringify(input) : 'all documents'}.`, + this.worker.desktopNotifier.notify('repository-reindexing', { + title: 'Indexing!', + message: `Your repository index is rebuilding (${input ? JSON.stringify(input) : 'all documents'}).`, iconUrl: '../images/nuxeo-128.png', }); }) + .then(() => { + this.waiting = this.waitForIndexing(); + return true; + }) + .catch((cause) => { + this.worker.desktopNotifier.notify('repository-reindexing', { + title: 'Something went wrong...', + message: `${cause.message}\nPlease try again later.`, + iconUrl: '../images/access_denied.png', + }); + return false; + }); + } + + waitForIndexing() { + if (this.waiting) return this.waiting; + this.waiting = this.worker.serverConnector + .executeOperation('Elasticsearch.WaitForIndexing') + // eslint-disable-next-line arrow-body-style, no-unused-vars + .then((response) => { + return this.worker.desktopNotifier.cancel('repository-reindexing'); + }) + .then(() => { this.waiting = undefined; }) .catch((cause) => { - this.worker.desktopNotifier.notify('error', { + this.worker.desktopNotifier.notify('repository-reindexing', { title: 'Something went wrong...', message: `${cause.message}\nPlease try again later.`, iconUrl: '../images/access_denied.png', }); }); + return this.waiting; } } diff --git a/src/main/server-connector.js b/src/main/server-connector.js index cc454c2f..e61faada 100644 --- a/src/main/server-connector.js +++ b/src/main/server-connector.js @@ -134,6 +134,7 @@ class ServerConnector { requireInteraction: false }); } + return error.response; }); } @@ -155,17 +156,17 @@ class ServerConnector { .then((res) => res.text())); } - executeOperation(operationId, params) { + executeOperation(operationId, params = {}, input = undefined) { return this.withNuxeo().then((nuxeo) => nuxeo .operation(operationId) .params(params) + .input(input) .execute() - .catch((e) => { - if (e.response) { - this.handleErrors(e, this.defaultServerError); - } else { - throw e; + .catch((cause) => { + if (!cause.response) { + throw cause; } + return this.handleErrors(cause, this.defaultServerError); })); } diff --git a/src/popup/index.js b/src/popup/index.js index f8c642ac..8a011b57 100644 --- a/src/popup/index.js +++ b/src/popup/index.js @@ -686,36 +686,20 @@ function loadPage(serviceWorker) { } }); - $('#traces-button').click(() => { - serviceWorker.serverConnector - .executeOperation('Traces.ToggleRecording', { readOnly: false }) - .then((response) => { - if (response.value) { - $('#traces-button').addClass('enabled'); - if ($('#traces-button').hasClass('disabled')) { - $('#traces-button').removeClass('disabled'); - } - } else { - $('#traces-button').addClass('disabled'); - if ($('#traces-button').hasClass('enabled')) { - $('#traces-button').removeClass('enabled'); - } - } - }) - .catch(() => { - serviceWorker.serverConnector - .executeOperation('Traces.ToggleRecording', { readOnly: true }) - .then((response) => { - if (response.value) { - $('#traces-button').addClass('enabled'); - $('#traces-button').removeClass('disabled'); - } else { - $('#traces-button').addClass('disabled'); - $('#traces-button').removeClass('enabled'); - } - }); - }); - }); + $('#traces-button').click(() => serviceWorker.serverConnector + .executeOperation('Traces.ToggleRecording', { readOnly: false }) + .then((response) => Boolean(response.value)) + .then((isEnabled) => { + $('#traces-button').toggleClass('enabled', isEnabled); + $('#traces-button').toggleClass('disabled', !isEnabled); + }) + .catch((cause) => { + console.error("Can't toggle automation traces", cause); + return serviceWorker.serverConnector + .executeOperation('Traces.ToggleRecording', { readOnly: true }) + .then((response) => Boolean(response.value)) + .catch(() => false); + })); $('#search').keydown(() => { $('#loading-gif').css({ display: 'inline' });