@jbrowse/core
Version:
JBrowse 2 core libraries used by plugins
55 lines (54 loc) • 2.27 kB
JavaScript
import { adapterConfigCacheKey } from "./util.js";
let adapterCache = {};
async function getAdapterPre(pluginManager, sessionId, adapterConfigSnapshot) {
const adapterType = adapterConfigSnapshot?.type;
if (!adapterType) {
throw new Error(`could not determine adapter type from adapter config snapshot ${JSON.stringify(adapterConfigSnapshot)}`);
}
const dataAdapterType = pluginManager.getAdapterType(adapterType);
if (!dataAdapterType) {
throw new Error(`unknown data adapter type ${adapterType}`);
}
const adapterConfig = dataAdapterType.configSchema.create(adapterConfigSnapshot, { pluginManager });
const getSubAdapter = getAdapter.bind(null, pluginManager, sessionId);
const CLASS = await dataAdapterType.getAdapterClass();
const dataAdapter = new CLASS(adapterConfig, getSubAdapter, pluginManager);
return {
dataAdapter,
sessionIds: new Set([sessionId]),
};
}
export async function getAdapter(pluginManager, sessionId, adapterConfigSnapshot) {
const cacheKey = adapterConfigCacheKey(adapterConfigSnapshot);
adapterCache[cacheKey] ??= getAdapterPre(pluginManager, sessionId, adapterConfigSnapshot);
const ret = await adapterCache[cacheKey];
ret.sessionIds.add(sessionId);
return ret;
}
export async function freeAdapterResources(args) {
const specKeys = Object.keys(args);
if (specKeys.length === 1 && specKeys[0] === 'sessionId') {
const { sessionId } = args;
for (const [cacheKey, cacheEntryP] of Object.entries(adapterCache)) {
const cacheEntry = await cacheEntryP;
cacheEntry.sessionIds.delete(sessionId);
if (cacheEntry.sessionIds.size === 0) {
delete adapterCache[cacheKey];
}
}
}
else {
for (const cacheEntryP of Object.values(adapterCache)) {
const cacheEntry = await cacheEntryP;
const regions = args.regions || (args.region ? [args.region] : []);
for (const region of regions) {
if (region.refName !== undefined) {
cacheEntry.dataAdapter.freeResources(region);
}
}
}
}
}
export function clearAdapterCache() {
adapterCache = {};
}