@jbrowse/core
Version:
JBrowse 2 core libraries used by plugins
124 lines (123 loc) • 5 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.readConfObject = readConfObject;
exports.getConf = getConf;
exports.getTypeNamesFromExplicitlyTypedUnion = getTypeNamesFromExplicitlyTypedUnion;
exports.isBareConfigurationSchemaType = isBareConfigurationSchemaType;
exports.isConfigurationSchemaType = isConfigurationSchemaType;
exports.isConfigurationModel = isConfigurationModel;
exports.isConfigurationSlotType = isConfigurationSlotType;
const mobx_state_tree_1 = require("mobx-state-tree");
const mst_reflection_1 = require("../util/mst-reflection");
function readConfObject(confObject, slotPath, args = {}) {
if (!slotPath) {
return structuredClone((0, mobx_state_tree_1.getSnapshot)(confObject));
}
else if (typeof slotPath === 'string') {
let slot = confObject[slotPath];
if (!slot &&
(0, mobx_state_tree_1.isStateTreeNode)(confObject) &&
(0, mobx_state_tree_1.isMapType)((0, mobx_state_tree_1.getType)(confObject))) {
slot = confObject.get(slotPath);
}
if (!slot) {
return undefined;
}
else {
const val = slot.expr ? slot.expr.evalSync(args) : slot;
return (0, mobx_state_tree_1.isStateTreeNode)(val)
? JSON.parse(JSON.stringify((0, mobx_state_tree_1.getSnapshot)(val)))
: val;
}
}
else if (Array.isArray(slotPath)) {
const slotName = slotPath[0];
if (slotPath.length > 1) {
const newPath = slotPath.slice(1);
let subConf = confObject[slotName];
if (!subConf &&
(0, mobx_state_tree_1.isStateTreeNode)(confObject) &&
(0, mobx_state_tree_1.isMapType)((0, mobx_state_tree_1.getType)(confObject))) {
subConf = confObject.get(slotName);
}
return subConf ? readConfObject(subConf, newPath, args) : undefined;
}
return readConfObject(confObject, slotName, args);
}
throw new TypeError('slotPath must be a string or array');
}
function getConf(model, slotPath, args) {
const { configuration } = model;
if (isConfigurationModel(configuration)) {
return readConfObject(configuration, slotPath, args);
}
throw new TypeError('cannot getConf on this model, it has no configuration');
}
function getTypeNamesFromExplicitlyTypedUnion(maybeUnionType) {
if ((0, mobx_state_tree_1.isType)(maybeUnionType)) {
maybeUnionType = (0, mst_reflection_1.resolveLateType)(maybeUnionType);
if ((0, mobx_state_tree_1.isUnionType)(maybeUnionType)) {
const typeNames = [];
for (let type of (0, mst_reflection_1.getUnionSubTypes)(maybeUnionType)) {
type = (0, mst_reflection_1.resolveLateType)(type);
let typeName = getTypeNamesFromExplicitlyTypedUnion(type);
if (!typeName.length) {
const def = (0, mst_reflection_1.getDefaultValue)(type);
typeName = [def.type];
}
if (!typeName[0]) {
throw new Error(`invalid config schema type ${type}`);
}
typeNames.push(...typeName);
}
return typeNames;
}
}
return [];
}
function isBareConfigurationSchemaType(thing) {
if ((0, mobx_state_tree_1.isType)(thing)) {
if ((0, mobx_state_tree_1.isModelType)(thing) &&
('isJBrowseConfigurationSchema' in thing ||
thing.name.includes('ConfigurationSchema'))) {
return true;
}
if ((0, mobx_state_tree_1.isLateType)(thing)) {
return true;
}
}
return false;
}
function isConfigurationSchemaType(thing) {
if (!(0, mobx_state_tree_1.isType)(thing)) {
return false;
}
else if (isBareConfigurationSchemaType(thing)) {
return true;
}
else if ((0, mobx_state_tree_1.isUnionType)(thing)) {
return (0, mst_reflection_1.getUnionSubTypes)(thing).every(t => isConfigurationSchemaType(t) || t.name === 'undefined');
}
else if ((0, mobx_state_tree_1.isOptionalType)(thing) &&
isConfigurationSchemaType((0, mst_reflection_1.getSubType)(thing))) {
return true;
}
else if ((0, mobx_state_tree_1.isArrayType)(thing) &&
isConfigurationSchemaType((0, mst_reflection_1.getSubType)(thing))) {
return true;
}
else if ((0, mobx_state_tree_1.isMapType)(thing) && isConfigurationSchemaType((0, mst_reflection_1.getSubType)(thing))) {
return true;
}
else {
return false;
}
}
function isConfigurationModel(thing) {
return (0, mobx_state_tree_1.isStateTreeNode)(thing) && isConfigurationSchemaType((0, mobx_state_tree_1.getType)(thing));
}
function isConfigurationSlotType(thing) {
return (typeof thing === 'object' &&
thing !== null &&
'isJBrowseConfigurationSlot' in thing);
}
;