Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fix for potential hydration mismatch errors #4675

Merged
merged 9 commits into from
Nov 23, 2024
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
"tslib": "^2.0.1",
"tss-react": "^4.0.0",
"tsx": "^4.17.0",
"typescript": "^5.5.0",
"typescript": "~5.6.0",
"typescript-eslint": "^8.0.1",
"webpack": "^5.64.4",
"webpack-cli": "^5.1.4",
Expand Down
105 changes: 56 additions & 49 deletions plugins/circular-view/src/BaseChordDisplay/models/model.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { getParent, isAlive, types } from 'mobx-state-tree'
// jbrowse
import { ConfigurationReference, getConf } from '@jbrowse/core/configuration'
import { BaseDisplay } from '@jbrowse/core/pluggableElementTypes/models'
import CircularChordRendererType from '@jbrowse/core/pluggableElementTypes/renderers/CircularChordRendererType'
import RendererType from '@jbrowse/core/pluggableElementTypes/renderers/RendererType'
import {
getContainingView,
getSession,
Expand Down Expand Up @@ -61,12 +59,33 @@ export const BaseChordDisplayModel = types
.volatile(() => ({
// NOTE: all this volatile stuff has to be filled in at once
// so that it stays consistent
/**
* #volatile
*/
filled: false,
/**
* #volatile
*/
reactElement: undefined as React.ReactElement | undefined,
/**
* #volatile
*/
data: undefined,
/**
* #volatile
*/
html: undefined as string | undefined,
/**
* #volatile
*/
message: '',
/**
* #volatile
*/
renderingComponent: undefined as undefined | AnyReactComponentType,
/**
* #volatile
*/
refNameMap: undefined as Record<string, string> | undefined,
}))
.actions(self => {
Expand Down Expand Up @@ -131,29 +150,17 @@ export const BaseChordDisplayModel = types
return getEnv(self).pluginManager.getRendererType(self.rendererTypeName)
},

/**
* #method
*/
isCompatibleWithRenderer(renderer: RendererType) {
return !!(renderer instanceof CircularChordRendererType)
},

/**
* #getter
* returns a string feature ID if the globally-selected object
* is probably a feature
* returns a string feature ID if the globally-selected object is probably
* a feature
*/
get selectedFeatureId() {
if (!isAlive(self)) {
return undefined
}
const session = getSession(self)
const { selection } = session
// does it quack like a feature?
if (isFeature(selection)) {
return selection.id()
}
return undefined
const { selection } = getSession(self)
return isFeature(selection) ? selection.id() : undefined
},
}))
.actions(self => ({
Expand All @@ -179,11 +186,11 @@ export const BaseChordDisplayModel = types
html,
renderingComponent,
}: {
message: string
data: any
html: string
reactElement: React.ReactElement
renderingComponent: AnyReactComponentType
message?: string
data?: any
html?: string
reactElement?: React.ReactElement
renderingComponent?: AnyReactComponentType
}) {
if (message) {
self.filled = false
Expand Down Expand Up @@ -230,12 +237,9 @@ export const BaseChordDisplayModel = types
makeAbortableReaction(
self,
renderReactionData,

// @ts-expect-error
renderReactionEffect,
{
name: `${self.type} ${self.id} rendering`,
// delay: self.renderDelay || 300,
fireImmediately: true,
},
self.renderStarted,
Expand All @@ -245,27 +249,34 @@ export const BaseChordDisplayModel = types

makeAbortableReaction(
self,
() => ({
assemblyNames: getTrackAssemblyNames(self.parentTrack),

adapter: getConf(getParent<any>(self, 2), 'adapter'),
assemblyManager: getSession(self).assemblyManager,
}),

async ({ assemblyNames, adapter, assemblyManager }: any, stopToken) => {
return assemblyManager.getRefNameMapForAdapter(
adapter,
assemblyNames[0],
{ stopToken, sessionId: getRpcSessionId(self) },
)
() => {
return {
assemblyNames: getTrackAssemblyNames(self.parentTrack),
adapter: getConf(getParent<any>(self, 2), 'adapter'),
assemblyManager: getSession(self).assemblyManager,
} as const
},
async (args, stopToken) => {
return args
? args.assemblyManager.getRefNameMapForAdapter(
args.adapter,
args.assemblyNames[0],
{
stopToken,
sessionId: getRpcSessionId(self),
},
)
: undefined
},
{
name: `${self.type} ${self.id} getting refNames`,
fireImmediately: true,
},
() => {},
refNameMap => {
self.setRefNameMap(refNameMap)
if (refNameMap) {
self.setRefNameMap(refNameMap)
}
},
error => {
console.error(error)
Expand All @@ -284,15 +295,11 @@ export const BaseChordDisplayModel = types
},
) {
const data = renderReactionData(self)
const rendering = await renderReactionEffect(
{
...data,
exportSVG: opts,
theme: opts.theme || data.renderProps.theme,
},
undefined,
self,
)
const rendering = await renderReactionEffect({
...data,
exportSVG: opts,
theme: opts.theme || data.renderProps.theme,
})
return <ReactRendering rendering={rendering} />
},
}))
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import clone from 'clone'
import { getRpcSessionId } from '@jbrowse/core/util/tracks'
import { getSession, getContainingView } from '@jbrowse/core/util'

// locals
import { CircularViewModel } from '../../CircularView/models/model'

export function renderReactionData(self: any) {
Expand All @@ -24,11 +26,7 @@ export function renderReactionData(self: any) {
}
}

export async function renderReactionEffect(
props: any,
stopToken: string | undefined,
self: any,
) {
export async function renderReactionEffect(props?: any, stopToken?: string) {
if (!props) {
throw new Error('cannot render with no props')
}
Expand All @@ -51,17 +49,11 @@ export async function renderReactionEffect(
return { message: 'Skipping render' }
}

// check renderertype compatibility
if (!self.isCompatibleWithRenderer(rendererType)) {
throw new Error(
`renderer ${rendererType.name} is not compatible with this display type`,
)
}

const { html, ...data } = await rendererType.renderInClient(rpcManager, {
...renderArgs,
...renderProps,
exportSVG,
stopToken,
})

return {
Expand Down
100 changes: 59 additions & 41 deletions plugins/gccontent/src/GCContentAdapter/GCContentAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import {
} from '@jbrowse/core/data_adapters/BaseAdapter'
import { firstValueFrom } from 'rxjs'
import { ObservableCreate } from '@jbrowse/core/util/rxjs'
import { SimpleFeature, Feature, Region } from '@jbrowse/core/util'
import {
SimpleFeature,
Feature,
Region,
updateStatus,
} from '@jbrowse/core/util'
import { toArray } from 'rxjs/operators'
import { checkStopToken } from '@jbrowse/core/util/stopToken'

export default class GCContentAdapter extends BaseFeatureDataAdapter {
private gcMode = 'content'
Expand All @@ -20,12 +26,13 @@ export default class GCContentAdapter extends BaseFeatureDataAdapter {
return adapter.dataAdapter as BaseFeatureDataAdapter
}

public async getRefNames() {
public async getRefNames(opts?: BaseOptions) {
const adapter = await this.configure()
return adapter.getRefNames()
return adapter.getRefNames(opts)
}

public getFeatures(query: Region, opts: BaseOptions) {
public getFeatures(query: Region, opts?: BaseOptions) {
const { statusCallback = () => {}, stopToken } = opts || {}
return ObservableCreate<Feature>(async observer => {
const sequenceAdapter = await this.configure()
const windowSize = this.getConf('windowSize')
Expand All @@ -42,50 +49,61 @@ export default class GCContentAdapter extends BaseFeatureDataAdapter {
return
}

const ret = sequenceAdapter.getFeatures(
{
...query,
start: queryStart,
end: queryEnd,
},
opts,
const feats = await firstValueFrom(
sequenceAdapter
.getFeatures(
{
...query,
start: queryStart,
end: queryEnd,
},
opts,
)
.pipe(toArray()),
)
const feats = await firstValueFrom(ret.pipe(toArray()))
const residues = feats[0]?.get('seq') || ''

for (let i = hw; i < residues.length - hw; i += windowDelta) {
const r = f ? residues[i] : residues.slice(i - hw, i + hw)
let nc = 0
let ng = 0
let len = 0
for (const letter of r) {
if (letter === 'c' || letter === 'C') {
nc++
} else if (letter === 'g' || letter === 'G') {
ng++
let start = performance.now()
await updateStatus('Calculating GC', statusCallback, () => {
for (let i = hw; i < residues.length - hw; i += windowDelta) {
if (performance.now() - start > 400) {
checkStopToken(stopToken)
start = performance.now()
}
if (letter !== 'N') {
len++
const r = f ? residues[i] : residues.slice(i - hw, i + hw)
let nc = 0
let ng = 0
let len = 0
for (const letter of r) {
if (letter === 'c' || letter === 'C') {
nc++
} else if (letter === 'g' || letter === 'G') {
ng++
}
if (letter !== 'N') {
len++
}
}
const pos = queryStart
const score =
this.gcMode === 'content'
? (ng + nc) / (len || 1)
: this.gcMode === 'skew'
? (ng - nc) / (ng + nc || 1)
: 0

observer.next(
new SimpleFeature({
uniqueId: `${this.id}_${pos + i}`,
refName: query.refName,
start: pos + i,
end: pos + i + windowDelta,
score,
}),
)
}
const pos = queryStart
const score =
this.gcMode === 'content'
? (ng + nc) / (len || 1)
: this.gcMode === 'skew'
? (ng - nc) / (ng + nc || 1)
: 0
})

observer.next(
new SimpleFeature({
uniqueId: `${this.id}_${pos + i}`,
refName: query.refName,
start: pos + i,
end: pos + i + windowDelta,
score,
}),
)
}
observer.complete()
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,13 @@ function stateModelFactory(schema: AnyConfigurationSchemaType) {
}
session.setSelection(feature)
},
/**
* #autorun
*/
afterCreate() {
// use color by stand to help indicate inversions better on first load,
// otherwise use selected orientation
if (self.colorBy) {
self.setColorScheme({ ...self.colorBy })
} else {
if (!self.colorBySetting && self.colorBy.type === 'normal') {
self.setColorScheme({ type: 'strand' })
}
},
Expand Down
Loading