Skip to content

Commit

Permalink
Refresh cache after login #55
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mohr committed Dec 10, 2021
1 parent cbdd1ed commit b856c1f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions openeo.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1856,9 +1856,18 @@ declare module OpenEO {
* Initializes the connection by requesting the capabilities.
*
* @async
* @protected
* @returns {Promise<Capabilities>} Capabilities
*/
init(): Promise<Capabilities>;
/**
* Refresh the cache for processes.
*
* @async
* @protected
* @returns {Promise}
*/
refreshProcessCache(): Promise;
/**
* Returns the URL of the versioned back-end instance currently connected to.
*
Expand Down
52 changes: 42 additions & 10 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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>} Capabilities
*/
async init() {
Expand All @@ -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.
*
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b856c1f

Please sign in to comment.