Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions js/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,9 +609,12 @@ class Browser {

cleanHouseForSession() {

this.clearViewportInteraction()

for (let trackView of this.trackViews) {
// empty axis column, viewport columns, sampleName column, scroll column, drag column, gear column
trackView.removeDOMFromColumnContainer()
// empty axis column, viewport columns, sampleName column, scroll column, drag column, gear column, and
// release the tracks, which may have listeners on the browser
trackView.dispose()
}

// discard all columns TODO - why do we do this?
Expand Down Expand Up @@ -1544,8 +1547,6 @@ class Browser {

async updateViews() {

const trackViews = this.trackViews

this.updateLocusSearchWidget()

for (const {bpPerPixel, chr, start} of this.referenceFrameList) {
Expand All @@ -1554,6 +1555,8 @@ class Browser {
}
}

const trackViews = this.trackViews

for (const centerGuide of this.centerLineList) {
centerGuide.repaint()
}
Expand Down Expand Up @@ -1584,9 +1587,18 @@ class Browser {
// Calculate group autoscale dataRange
if (Object.entries(groupAutoscaleTrackViews).length > 0) {
for (const [group, trackViews] of Object.entries(groupAutoscaleTrackViews)) {
const viewports = trackViews.map(trackView => [...trackView.viewports])
const inViewFeatures = await Promise.all(trackViews.map(trackView => trackView.getInViewFeatures()))

// Viewports replaced during the load (a locus change, a closed multi-locus panel) make this
// update obsolete. Stop, so a range computed without their features is not applied; the
// change that replaced them does its own update. A track removed during the load is
// different: nothing updates the rest of its group, so carry on without it.
if (trackViews.some((trackView, i) => !trackView.disposed && viewports[i].some(viewport => viewport.disposed))) return

const dataRange = doAutoscale(inViewFeatures.flat())
for (const trackView of trackViews) {
if (trackView.disposed) continue // Track was removed during the load
trackView.track.dataRange = Object.assign({}, dataRange)
trackView.track.autoscale = false
}
Expand Down Expand Up @@ -1907,9 +1919,10 @@ class Browser {
// create reference frame list based on search loci
this.referenceFrameList = createReferenceFrameList(loci, this.genome, this.flanking, this.minimumBases(), this.calculateViewportWidth(loci.length), this.isSoftclipped())

// discard track viewport DOM elements
// discard track viewports
this.clearViewportInteraction()
for (let trackView of this.trackViews) {
trackView.removeViewportsFromColumnContainer()
trackView.disposeViewports()
}

// discard ONLY viewport columns
Expand Down Expand Up @@ -2387,6 +2400,16 @@ class Browser {
}


/**
* Forget a pan or vertical scroll that is in progress. Called before viewports are disposed of, as the
* interaction state refers to one of them.
*/
clearViewportInteraction() {
this.dragObject = undefined
this.isScrolling = false
this.vpMouseDown = undefined
}

cancelTrackPan() {

const dragObject = this.dragObject
Expand All @@ -2397,6 +2420,11 @@ class Browser {
if (dragObject && dragObject.viewport.referenceFrame.start !== dragObject.start) {
this.updateViews()
this.fireEvent('trackdragend', [dragObject.viewport])
for (const trackView of this.trackViews) {
if (typeof trackView.track.trackDragEnd === 'function') {
trackView.track.trackDragEnd()
}
}
}
}

Expand Down Expand Up @@ -2563,8 +2591,7 @@ class Browser {
}

removeKeyboardHandler() {
console.log("Remove handler")
document.addEventListener("keyup", this.keyUpHandler)
document.removeEventListener("keyup", this.keyUpHandler)
}


Expand Down
32 changes: 9 additions & 23 deletions js/feature/featureTrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ class FeatureTrack extends TrackBase {
} else {
this.render = config.render || renderFeature
this.arrowSpacing = 30
// adjust label positions to make sure they're always visible
monitorTrackDrag(this)
this.squishedRowHeight = config.squishedRowHeight || 15
this.expandedRowHeight = config.expandedRowHeight || 30
this.height = config.height || this.margin + 2 * this.expandedRowHeight
Expand Down Expand Up @@ -546,32 +544,20 @@ class FeatureTrack extends TrackBase {


/**
* Called when the track is removed. Do any needed cleanup here
* Called by the browser when a pan ends. Repaint to move feature names whose center went out of view.
*/
dispose() {
this.trackView = undefined
}
}

/**
* Monitors track drag events, updates label position to ensure that they're always visible.
* @param track
*/
function monitorTrackDrag(track) {

function onDragEnd() {
if (track.trackView && track.displayMode !== "SQUISHED") {
// Repaint views to adjust feature name if center is moved out of view
track.trackView.repaintViews()
trackDragEnd() {
if (this.trackView && this.displayMode !== "SQUISHED") {
this.trackView.repaintViews()
}
}

if (track.browser.on) {
track.browser.on('trackdragend', onDragEnd)
/**
* Called when the track is removed. Do any needed cleanup here
*/
dispose() {
this.trackView = undefined
}


}


export default FeatureTrack
3 changes: 0 additions & 3 deletions js/feature/segTrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ class SegTrack extends TrackBase {

this._initialColor = this.color || this.constructor.defaultColor
this._initialAltColor = this.altColor || this.constructor.defaultColor

this.didTrackDragEnd = undefined
this.browser.on('trackdragend', () => this.didTrackDragEnd = true)
}

menuItemList() {
Expand Down
1 change: 1 addition & 0 deletions js/ideogramViewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class IdeogramViewport extends TrackViewport {
async loadFeatures() {
const chr = this.referenceFrame.chr;
const features = await this.referenceFrame.genome.getCytobands(chr)
if (this.disposed) return undefined // The viewport was replaced or removed during the load
this.featureCache.set(chr, features)
return features
}
Expand Down
52 changes: 18 additions & 34 deletions js/trackView.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,17 @@ class TrackView {
// Trigger viewport to load features needed to cover current genomic range
// NOTE: these must be loaded synchronously, do not user Promise.all, not all file readers are thread safe
for (let viewport of viewportsToReload) {
if (viewport.disposed) break
await viewport.loadFeatures()
}

if (this.disposed) return // Track was removed during load

// Viewports replaced during the load (a locus change, a closed multi-locus panel) make this update
// obsolete. Stop, so the track is not autoscaled without their features; the change that replaced them
// does its own update.
if (visibleViewports.some(viewport => viewport.disposed)) return

// Special case for variant tracks in multilocus view. The # of rows to allocate to the variant (site)
// section depends on data from all the views. We only need to adjust this however if any data was loaded
// (i.e. reloadableViewports.length > 0)
Expand Down Expand Up @@ -561,6 +567,8 @@ class TrackView {
const visibleViewports = this.viewports.filter(viewport => viewport.isVisible())
for (let vp of visibleViewports) {

if (vp.disposed) continue // Replaced during an earlier viewport's load

const referenceFrame = vp.referenceFrame
const {chr, start, bpPerPixel} = vp.referenceFrame
const end = start + referenceFrame.toBP(vp.getWidth())
Expand Down Expand Up @@ -740,7 +748,7 @@ class TrackView {
this.innerScroll.removeEventListener('mousedown', this.boundTrackScrollMouseDownHandler)
this.browser.columnContainer.removeEventListener('mouseup', this.boundColumnContainerMouseUpHandler)
this.browser.columnContainer.removeEventListener('mousemove', this.boundColumnContainerMouseMoveHandler)
this.browser.columnContainer.removeEventListener('mouseleave', this.boundColumnContainerMouseMoveHandler)
this.browser.columnContainer.removeEventListener('mouseleave', this.boundColumnContainerMouseUpHandler)
}
}

Expand Down Expand Up @@ -843,7 +851,7 @@ class TrackView {
} else {
this.dragHandle.removeEventListener('mousedown', this.boundTrackDragMouseDownHandler)
document.removeEventListener('mouseup', this.boundDocumentTrackDragMouseUpHandler)
this.dragHandle.removeEventListener('mouseup', this.boundTrackDragMouseEnterHandler)
this.dragHandle.removeEventListener('mouseenter', this.boundTrackDragMouseEnterHandler)
this.dragHandle.removeEventListener('mouseout', this.boundTrackDragMouseOutHandler)
}

Expand All @@ -858,37 +866,15 @@ class TrackView {

}

removeDOMFromColumnContainer() {

// Axis
this.axis.remove()
this.removeViewportsFromColumnContainer()

// Sample Info Viewport
this.sampleInfoViewport.dispose()

// SampleName Viewport
this.sampleNameViewport.dispose()

// empty trackScrollbar Column
this.removeTrackScrollMouseHandlers()
this.outerScroll.remove()

// empty trackDrag Column
this.removeTrackDragMouseHandlers()
this.dragHandle.remove()

// empty trackGear Column
this.removeTrackGearMouseHandlers()
this.gearContainer.remove()

}

removeViewportsFromColumnContainer() {
// Track Viewports
/**
* Discard the viewports. They are disposed of, not just removed from the DOM, to release their listeners on
* the document and browser, which would otherwise keep them and their features alive.
*/
disposeViewports() {
for (let viewport of this.viewports) {
viewport.viewportElement.remove()
viewport.dispose()
}
this.viewports = []
}

/**
Expand All @@ -898,9 +884,7 @@ class TrackView {

this.axis.remove()

for (let viewport of this.viewports) {
viewport.dispose()
}
this.disposeViewports()

this.sampleInfoViewport.dispose()

Expand Down
20 changes: 15 additions & 5 deletions js/trackViewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ class TrackViewport extends Viewport {

// Expand the requested range so we can pan a bit without reloading. But not beyond chromosome bounds
const chromosome = await this.browser.genome.loadChromosome(chr)
if (this.disposed) return undefined
const chrLength = chromosome ? chromosome.bpLength : Number.MAX_SAFE_INTEGER
const pixelWidth = this.viewportElement.clientWidth// * 3;
const bpWidth = pixelWidth * referenceFrame.bpPerPixel
Expand All @@ -266,13 +267,15 @@ class TrackViewport extends Viewport {
const fromCache = this.featureCache &&
this.featureCache.containsRange(chr, bpStart, bpEnd, referenceFrame.bpPerPixel, this.windowFunction)
const features = await this.getFeatures(track, chr, bpStart, bpEnd, referenceFrame.bpPerPixel)
if (this.disposed) return undefined // The viewport was replaced or removed during the load
if (features) {
let roiFeatures = []
if (track.roiSets && track.roiSets.length > 0) {
for (let roiSet of track.roiSets) {
const features = await roiSet.getFeatures(chr, bpStart, bpEnd, referenceFrame.bpPerPixel)
roiFeatures.push({track: roiSet, features})
}
if (this.disposed) return undefined
}

const mr = track && (track.resolutionAware) //
Expand All @@ -293,14 +296,16 @@ class TrackViewport extends Viewport {
}
} catch (error) {
// Track might have been removed during load
if (this.trackView && this.trackView.disposed !== true) {
if (!this.disposed && this.trackView && this.trackView.disposed !== true) {
this.showMessage(NOT_LOADED_MESSAGE)
this.browser.alert.present(trackLoadError(this.track.config, error))
console.error(error)
}
} finally {
this.loading = false
this.stopSpinner()
if (!this.disposed) {
this.loading = false
this.stopSpinner()
}
}
}

Expand Down Expand Up @@ -852,8 +857,10 @@ class TrackViewport extends Viewport {

popupTimerID = setTimeout(async () => {

const content = await this.handleTrackClick(event)
if (content) {
// The viewport may be replaced (a locus change) before the timer fires or the
// popup data arrives
const content = this.disposed ? undefined : await this.handleTrackClick(event)
if (content && !this.disposed) {

if (false === event.shiftKey) {

Expand Down Expand Up @@ -1029,6 +1036,9 @@ class TrackViewport extends Viewport {

const features = 'annotation' === track.type ? track.clickedFeatures(clickState) : undefined
const dataList = await track.popupData(clickState, features)
if (this.disposed) {
return
}

const popupClickHandlerResult = this.browser.fireEvent('trackclick', [track, dataList, clickState.genomicLocation, features])

Expand Down
3 changes: 3 additions & 0 deletions js/viewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ class Viewport {
this[key] = undefined;
}
}

// Code that holds a viewport across an await checks this before touching it again
this.disposed = true;
}
}

Expand Down
Loading