@jbrowse/core
Version:
JBrowse 2 core libraries used by plugins
61 lines (60 loc) • 2.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAdapter = getAdapter;
exports.freeAdapterResources = freeAdapterResources;
exports.clearAdapterCache = clearAdapterCache;
const util_1 = require("./util");
let adapterCache = {};
async function getAdapterPre(pluginManager, sessionId, adapterConfigSnapshot) {
const adapterType = adapterConfigSnapshot === null || adapterConfigSnapshot === void 0 ? void 0 : 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]),
};
}
async function getAdapter(pluginManager, sessionId, adapterConfigSnapshot) {
var _a;
const cacheKey = (0, util_1.adapterConfigCacheKey)(adapterConfigSnapshot);
(_a = adapterCache[cacheKey]) !== null && _a !== void 0 ? _a : (adapterCache[cacheKey] = getAdapterPre(pluginManager, sessionId, adapterConfigSnapshot));
const ret = await adapterCache[cacheKey];
ret.sessionIds.add(sessionId);
return ret;
}
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);
}
}
}
}
}
function clearAdapterCache() {
adapterCache = {};
}