@jbrowse/core
Version:
JBrowse 2 core libraries used by plugins
57 lines (56 loc) • 2.39 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 getAdapter(pluginManager, sessionId, adapterConfigSnapshot) {
const cacheKey = (0, util_1.adapterConfigCacheKey)(adapterConfigSnapshot);
if (!adapterCache[cacheKey]) {
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);
adapterCache[cacheKey] = {
dataAdapter,
sessionIds: new Set([sessionId]),
};
}
const cacheEntry = adapterCache[cacheKey];
cacheEntry.sessionIds.add(sessionId);
return cacheEntry;
}
function freeAdapterResources(args) {
const specKeys = Object.keys(args);
if (specKeys.length === 1 && specKeys[0] === 'sessionId') {
const { sessionId } = args;
for (const [cacheKey, cacheEntry] of Object.entries(adapterCache)) {
cacheEntry.sessionIds.delete(sessionId);
if (cacheEntry.sessionIds.size === 0) {
delete adapterCache[cacheKey];
}
}
}
else {
for (const cacheEntry of Object.values(adapterCache)) {
const regions = args.regions || (args.region ? [args.region] : []);
for (const region of regions) {
if (region.refName !== undefined) {
cacheEntry.dataAdapter.freeResources(region);
}
}
}
}
}
function clearAdapterCache() {
adapterCache = {};
}