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

Fix bug in desktop where first track gets stuck loading #2819

Merged
merged 1 commit into from
Mar 16, 2022
Merged
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
39 changes: 18 additions & 21 deletions products/jbrowse-desktop/src/rpc.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import RpcServer from 'librpc-web-mod'
import { useStaticRendering } from 'mobx-react'

import PluginManager from '@jbrowse/core/PluginManager'
import { remoteAbortRpcHandler } from '@jbrowse/core/rpc/remoteAbortSignals'
import RpcMethodType from '@jbrowse/core/pluggableElementTypes/RpcMethodType'
import { remoteAbortRpcHandler } from '@jbrowse/core/rpc/remoteAbortSignals'
import PluginLoader, { PluginDefinition } from '@jbrowse/core/PluginLoader'
import corePlugins from './corePlugins'

Expand All @@ -22,8 +22,8 @@ interface WorkerConfiguration {

let jbPluginManager: PluginManager | undefined

// waits for a message from the main thread containing our configuration,
// which must be sent on boot
// waits for a message from the main thread containing our configuration, which
// must be sent on boot
function receiveConfiguration(): Promise<WorkerConfiguration> {
const configurationP: Promise<WorkerConfiguration> = new Promise(resolve => {
// listen for the configuration
Expand All @@ -32,7 +32,7 @@ function receiveConfiguration(): Promise<WorkerConfiguration> {
self.onmessage = () => {}
}
})
postMessage('ready')
postMessage('readyForConfig')
return configurationP
}

Expand All @@ -46,9 +46,7 @@ async function getPluginManager() {
pluginLoader.installGlobalReExports(self)
const runtimePlugins = await pluginLoader.load()
const plugins = [...corePlugins.map(p => ({ plugin: p })), ...runtimePlugins]
const pluginManager = new PluginManager(
plugins.map(({ plugin: P }) => new P()),
)
const pluginManager = new PluginManager(plugins.map(P => new P.plugin()))
pluginManager.createPluggableElements()
pluginManager.configure()
jbPluginManager = pluginManager
Expand All @@ -61,41 +59,40 @@ interface WrappedFuncArgs {
[key: string]: unknown
}

function wrapForRpc(
func: (args: unknown, rpcDriverClassName: string) => unknown,
) {
type RpcFunc = (args: unknown, rpcDriverClassName: string) => unknown

function wrapForRpc(func: RpcFunc) {
return (args: WrappedFuncArgs) => {
const { channel, rpcDriverClassName } = args
return func(
{
...args,
statusCallback: (message: string) => {
// @ts-ignore
self.rpcServer.emit(args.channel, message)
self.rpcServer.emit(channel, message)
},
},
args.rpcDriverClassName,
rpcDriverClassName,
)
}
}

getPluginManager()
.then(pluginManager => {
const rpcConfig: { [methodName: string]: Function } = {}
const rpcMethods = pluginManager.getElementTypesInGroup('rpc method')
rpcMethods.forEach(rpcMethod => {
if (!(rpcMethod instanceof RpcMethodType)) {
throw new Error('invalid rpc method??')
}

rpcConfig[rpcMethod.name] = wrapForRpc(rpcMethod.execute.bind(rpcMethod))
})
const rpcConfig = Object.fromEntries(
pluginManager.getElementTypesInGroup('rpc method').map(entry => {
const { execute, name } = entry as RpcMethodType
return [name, wrapForRpc((execute as RpcFunc).bind(entry))]
}),
)

// @ts-ignore
self.rpcServer = new RpcServer.Server({
...rpcConfig,
...remoteAbortRpcHandler(),
ping: () => {}, // < the ping method is required by the worker driver for checking the health of the worker
})
postMessage('ready')
})
.catch(error => {
// @ts-ignore
Expand Down