Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into orthogroup-tree-table
Browse files Browse the repository at this point in the history
  • Loading branch information
bobular committed Jun 6, 2024
2 parents 032c479 + dd14494 commit 9f3d563
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 44 deletions.
1 change: 1 addition & 0 deletions packages/libs/components/src/plots/Barplot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ const Barplot = makePlotlyPlotComponent(
range: data.series.length ? undefined : [0, 10],
tickfont: data.series.length ? {} : { color: 'transparent' },
showticklabels: showIndependentAxisTickLabel,
type: 'category',
};

// truncation
Expand Down
94 changes: 50 additions & 44 deletions packages/libs/wdk-client/src/Service/ServiceBase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import localforage from 'localforage';
import { keyBy, memoize } from 'lodash';
import { keyBy, memoize, once } from 'lodash';
import * as QueryString from 'querystring';
import { v4 as uuid } from 'uuid';
import { expandedRecordClassDecoder } from '../Service/Decoders/RecordClassDecoders';
Expand Down Expand Up @@ -118,13 +118,19 @@ export type ServiceBase = ReturnType<typeof ServiceBase>;
* @param {string} serviceUrl Base url for Wdk REST Service.
*/
export const ServiceBase = (serviceUrl: string) => {
/** Cross-session persistence */
const _store: LocalForage = localforage.createInstance({
name: 'WdkService/' + serviceUrl,
});
/** In-memory persistence */
const _cache: Map<string, Promise<any>> = new Map();
let _initialCheck: Promise<number> | undefined;

/** WdkService "version" number */
let _version: number | undefined;
/** Indicates if the cache is being invalidated */
let _isInvalidating = false;
/** Indicates if a network request has been made */
let _hasRequestBeenMade = false;

/**
* Get the configuration for the Wdk REST Service that resides at the given base url.
Expand Down Expand Up @@ -206,7 +212,7 @@ export const ServiceBase = (serviceUrl: string) => {
console.group('Client error log');
console.error(error);
console.groupEnd();
return _checkStoreVersion().then(() =>
return _initializeStore().then(() =>
sendRequest(Decode.none, {
method: 'post',
path: '/client-errors',
Expand Down Expand Up @@ -246,6 +252,8 @@ export const ServiceBase = (serviceUrl: string) => {
credentials: 'include',
})
.then((response) => {
let hasRequestBeenMade = _hasRequestBeenMade;
_hasRequestBeenMade = true;
if (_isInvalidating) {
return pendingPromise as Promise<T>;
}
Expand Down Expand Up @@ -273,15 +281,23 @@ export const ServiceBase = (serviceUrl: string) => {

return response.text().then((text) => {
if (response.status === 409 && text === CLIENT_OUT_OF_SYNC_TEXT) {
_isInvalidating = true;
Promise.all([
_store.clear(),
alert(
'Reload Page',
'This page is no longer valid and will be reloaded.'
),
]).then(() => location.reload(true));
return pendingPromise as Promise<T>;
if (!_isInvalidating) {
_isInvalidating = true;
_store
.clear()
.then(() =>
hasRequestBeenMade
? alert(
'Reload page',
'This page is no longer valid and will be reloaded.'
)
: undefined
)
.then(() => {
window.location.reload();
});
return pendingPromise as Promise<T>;
}
}

// FIXME Get uuid from response header when available
Expand All @@ -306,7 +322,7 @@ export const ServiceBase = (serviceUrl: string) => {
checkCachedValue = (cachedValue: T) => true
) {
if (!_cache.has(key)) {
let cacheValue$ = _checkStoreVersion()
let cacheValue$ = _initializeStore()
.then(() => _store.getItem<T>(key))
.then((storeItem) => {
if (storeItem != null && checkCachedValue(storeItem))
Expand All @@ -326,42 +342,32 @@ export const ServiceBase = (serviceUrl: string) => {
return <Promise<T>>_cache.get(key);
}

function _checkStoreVersion() {
if (_initialCheck == null) {
let serviceConfig$ = _fetchJson<ServiceConfig>('get', '/');
let storeConfig$ = _store.getItem<ServiceConfig>('config');
_initialCheck = Promise.all([serviceConfig$, storeConfig$] as const)
.then(([serviceConfig, storeConfig]) => {
if (
storeConfig == null ||
storeConfig.startupTime != serviceConfig.startupTime
) {
return _store.clear().then(() => {
return _store.setItem('config', serviceConfig).catch((err) => {
console.error(
'Unable to store WdkService item with key `config`.',
err
);
return serviceConfig;
});
});
}
return serviceConfig;
})
.then((serviceConfig) => {
_version = serviceConfig.startupTime;
return _version;
});
}
return _initialCheck;
}
/**
* Set the store version
*/
const _initializeStore = once(function _initializeStore() {
return _store
.getItem<ServiceConfig>('/__config')
.then((storeConfig) => {
if (storeConfig == null) {
return _fetchJson<ServiceConfig>('GET', '/').then((serviceConfig) => {
return _store.setItem('/__config', serviceConfig);
});
}
return storeConfig;
})
.then((config) => {
_version = config.startupTime;
});
});

function _clearCache() {
return _store.clear();
}

function getVersion() {
return _checkStoreVersion();
async function getVersion() {
const { startupTime } = await getConfig();
return startupTime;
}

function getRecordTypesPath() {
Expand Down

0 comments on commit 9f3d563

Please sign in to comment.