Skip to content

Commit

Permalink
Random refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Feb 13, 2021
1 parent 5ed2fc2 commit f2b4688
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 26 deletions.
9 changes: 9 additions & 0 deletions packages/core/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { Feature } from './simpleFeature'
import {
TypeTestedByPredicate,
isSessionModel,
isDisplayModel,
isViewModel,
isTrackModel,
Region,
Expand Down Expand Up @@ -257,6 +258,14 @@ export function getContainingTrack(node: IAnyStateTreeNode) {
}
}

export function getContainingDisplay(node: IAnyStateTreeNode) {
try {
return findParentThatIs(node, isDisplayModel)
} catch (e) {
throw new Error('no containing track found')
}
}

/**
* Assemble a 1-based "locString" from an interbase genomic location
* @param region - Region
Expand Down
2 changes: 1 addition & 1 deletion packages/core/util/tracks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function getTrackAssemblyNames(
return [readConfObject(parent, 'name')]
}
}
return trackAssemblyNames
return trackAssemblyNames as string[]
}

/** return the rpcSessionId of the highest parent node in the tree that has an rpcSessionId */
Expand Down
13 changes: 13 additions & 0 deletions packages/core/util/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ export function isTrackModel(thing: unknown): thing is AbstractTrackModel {
)
}

export interface AbstractDisplayModel {
parentTrack: AbstractTrackModel
}
export function isDisplayModel(thing: unknown): thing is AbstractDisplayModel {
return (
typeof thing === 'object' &&
thing !== null &&
'configuration' in thing &&
// @ts-ignore
thing.configuration.displayId
)
}

export interface TrackViewModel extends AbstractViewModel {
showTrack(trackId: string): void
hideTrack(trackId: string): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ test('adapter can fetch sequences', async () => {
})
const featuresArray = await result.pipe(toArray()).toPromise()
expect(featuresArray.length).toBe(2)
expect(featuresArray[0].toJSON()).toEqual(features[1])
})

test('adapter can fetch regions 1', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { types, getParent, isAlive, cast, Instance } from 'mobx-state-tree'
import { Component } from 'react'
import { getConf, readConfObject } from '@jbrowse/core/configuration'
import { readConfObject } from '@jbrowse/core/configuration'
import { Region } from '@jbrowse/core/util/types/mst'

import {
assembleLocString,
makeAbortableReaction,
getSession,
getContainingDisplay,
} from '@jbrowse/core/util'
import {
getTrackAssemblyNames,
Expand Down Expand Up @@ -165,7 +166,7 @@ const blockState = types
JSON.parse(JSON.stringify(renderArgs)),
)
.catch((e: Error) => {
// just console.error if it's something while it's being destroyed
// just warn for errors while being destroyed
console.warn('Error while destroying block', e)
})
}
Expand All @@ -176,33 +177,25 @@ const blockState = types
export default blockState
export type BlockStateModel = typeof blockState
export type BlockModel = Instance<BlockStateModel>
// calls the render worker to render the block content
// not using a flow for this, because the flow doesn't
// work with autorun
// calls the render worker to render the block content. not using a flow for
// this, because the flow doesn't work with autorun
function renderBlockData(self: Instance<BlockStateModel>) {
try {
const { assemblyManager, rpcManager } = getSession(self)
let display = getParent(self)
while (!(display.configuration && getConf(display, 'displayId'))) {
display = getParent(display)
}
const display = getContainingDisplay(self) as any
const assemblyNames = getTrackAssemblyNames(display.parentTrack)
let cannotBeRenderedReason
if (!assemblyNames.includes(self.region.assemblyName)) {
let matchFound = false
assemblyNames.forEach((assemblyName: string) => {
const assembly = assemblyManager.get(assemblyName)
if (assembly && assembly.hasName(assemblyName)) {
matchFound = true
}
})
if (!matchFound) {
cannotBeRenderedReason = `region assembly (${self.region.assemblyName}) does not match track assemblies (${assemblyNames})`
const regionAsm = self.region.assemblyName
if (
!assemblyNames.includes(regionAsm) &&
!assemblyNames.find(assemblyName =>
assemblyManager.get(assemblyName)?.hasName(regionAsm),
)
) {
return {
displayError: `region assembly (${regionAsm}) does not match track assemblies (${assemblyNames})`,
}
}
if (!cannotBeRenderedReason) {
cannotBeRenderedReason = display.regionCannotBeRendered(self.region)
}

const { renderProps } = display
const { rendererType } = display
const { config } = renderProps
Expand All @@ -218,7 +211,7 @@ function renderBlockData(self: Instance<BlockStateModel>) {
rendererType,
rpcManager,
renderProps,
cannotBeRenderedReason,
cannotBeRenderedReason: display.regionCannotBeRendered(self.region),
displayError: display.error,
renderArgs: {
statusCallback: (message: string) => {
Expand Down

0 comments on commit f2b4688

Please sign in to comment.