From b856c1f93096b0697a6d763e404f612df2968315 Mon Sep 17 00:00:00 2001 From: Matthias Mohr Date: Fri, 10 Dec 2021 16:37:22 +0100 Subject: [PATCH] Refresh cache after login #55 --- CHANGELOG.md | 10 +++++++++ openeo.d.ts | 9 ++++++++ src/connection.js | 52 ++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 61 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cb34d4e..bc705592 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [2.3.1] - 2021-12-10 + +### Fixed + +- Process cache gets refreshed after the authentication details have changed (e.g. the user has logged in or out). + ## [2.3.0] - 2021-12-10 +### Added + +- New parameter `abortController` to allow cancellation of longer running requests (uploading files, sync. data processing). + ### Changed - Rarely used `multihashes` dependency not included in the bundle by default, see Readme to include it in the browser. No change for node environments. diff --git a/openeo.d.ts b/openeo.d.ts index 4bafbad7..e1ae14d9 100644 --- a/openeo.d.ts +++ b/openeo.d.ts @@ -1856,9 +1856,18 @@ declare module OpenEO { * Initializes the connection by requesting the capabilities. * * @async + * @protected * @returns {Promise} Capabilities */ init(): Promise; + /** + * Refresh the cache for processes. + * + * @async + * @protected + * @returns {Promise} + */ + refreshProcessCache(): Promise; /** * Returns the URL of the versioned back-end instance currently connected to. * diff --git a/src/connection.js b/src/connection.js index 2e8e134c..b75c3fd7 100644 --- a/src/connection.js +++ b/src/connection.js @@ -68,14 +68,6 @@ class Connection { * @type {?Capabilities} */ this.capabilitiesObject = null; - /** - * Process cache - * - * @protected - * @type {ProcessRegistry} - */ - this.processes = new ProcessRegistry([], Boolean(options.addNamespaceToProcess)); - this.processes.listeners.push((...args) => this.emit('processesChanged', ...args)); /** * Listeners for events. * @@ -90,12 +82,21 @@ class Connection { * @type {Options} */ this.options = options; + /** + * Process cache + * + * @protected + * @type {ProcessRegistry} + */ + this.processes = new ProcessRegistry([], Boolean(options.addNamespaceToProcess)); + this.processes.listeners.push((...args) => this.emit('processesChanged', ...args)); } /** * Initializes the connection by requesting the capabilities. * * @async + * @protected * @returns {Promise} Capabilities */ async init() { @@ -104,6 +105,35 @@ class Connection { return this.capabilitiesObject; } + /** + * Refresh the cache for processes. + * + * @async + * @protected + * @returns {Promise} + */ + async refreshProcessCache() { + if (this.processes.count() === 0) { + return; + } + let promises = this.processes.namespaces().map(namespace => { + let fn = () => Promise.resolve(); + if (namespace === 'user') { + if (!this.isAuthenticated()) { + fn = () => this.processes.remove(null, 'user') ? Promise.resolve() : Promise.reject(new Error("Can't clear user processes")); + } + else if (this.capabilities().hasFeature('listUserProcesses')) { + fn = () => this.listUserProcesses(); + } + } + else if (this.capabilities().hasFeature('listProcesses')) { + fn = () => this.listProcesses(namespace); + } + return fn().catch(error => console.warn(`Could not update processes for namespace '${namespace}' due to an error: ${error.message}`)); + }); + return await Promise.all(promises); + } + /** * Returns the URL of the versioned back-end instance currently connected to. * @@ -511,6 +541,8 @@ class Connection { this.authProvider = null; } this.emit('authProviderChanged', this.authProvider); + // Update process cache on auth changes: https://github.com/Open-EO/openeo-js-client/issues/55 + this.refreshProcessCache(); } /** @@ -823,7 +855,7 @@ class Connection { throw new Error("Response did not contain a Job ID. Job has likely been created, but may not show up yet."); } let job = new Job(this, response.headers['openeo-identifier']).setAll(requestBody); - if (this.capabilitiesObject.hasFeature('describeJob')) { + if (this.capabilities().hasFeature('describeJob')) { return await job.describeJob(); } else { @@ -889,7 +921,7 @@ class Connection { throw new Error("Response did not contain a Service ID. Service has likely been created, but may not show up yet."); } let service = new Service(this, response.headers['openeo-identifier']).setAll(requestBody); - if (this.capabilitiesObject.hasFeature('describeService')) { + if (this.capabilities().hasFeature('describeService')) { return service.describeService(); } else {