-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow using web worker RPC on embedded LGV (#3276)
* Add rpc web worker functionality for embedded * Wire up * Mock for test * Remove inaccurate comments
- Loading branch information
Showing
22 changed files
with
237 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
products/jbrowse-react-circular-genome-view/tsconfig.build.es5.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
products/jbrowse-react-circular-genome-view/tsconfig.build.esm.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export { createJBrowseTheme, ThemeProvider } from './deprecations' | ||
export { default as JBrowseLinearGenomeView } from './JBrowseLinearGenomeView' | ||
export { default as createModel } from './createModel' | ||
export { default as createViewState } from './createViewState' | ||
export { default as loadPlugins } from './loadPlugins' | ||
export type { ViewModel } from './createModel/createModel' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export { createJBrowseTheme, ThemeProvider } from './deprecations' | ||
export { default as JBrowseLinearGenomeView } from './JBrowseLinearGenomeView' | ||
export { default as createModel } from './createModel' | ||
export { default as createViewState } from './createViewState' | ||
export { default as loadPlugins } from './loadPlugins' | ||
export { default as makeWorkerInstance } from './makeWorkerInstance' | ||
export type { ViewModel } from './createModel/createModel' |
14 changes: 14 additions & 0 deletions
14
products/jbrowse-react-linear-genome-view/src/makeWorkerInstance.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// this is in a separate module here so it can be mocked out by jest. the | ||
// import.meta.url is not well recognized by jest and we use MainThreadRpc in | ||
// tests anyways right now | ||
// | ||
// note: this uses webpack 5 native worker modules | ||
// | ||
// see https://github.com/cmdcolin/cra-webpack5-web-worker-example for simple example | ||
// and docs https://webpack.js.org/guides/web-workers/ | ||
// | ||
// also note: the craco config for webpack publicPath: 'auto' is needed for | ||
// these workers | ||
export default function makeWorkerInstance() { | ||
return new Worker(new URL('./rpcWorker', import.meta.url)) | ||
} |
106 changes: 106 additions & 0 deletions
106
products/jbrowse-react-linear-genome-view/src/rpcWorker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* eslint-disable no-restricted-globals */ | ||
import './workerPolyfill' | ||
|
||
// @ts-ignore | ||
import RpcServer from 'librpc-web-mod' | ||
import { enableStaticRendering } from 'mobx-react' | ||
|
||
import PluginManager from '@jbrowse/core/PluginManager' | ||
import { remoteAbortRpcHandler } from '@jbrowse/core/rpc/remoteAbortSignals' | ||
import PluginLoader, { PluginDefinition } from '@jbrowse/core/PluginLoader' | ||
import { serializeError } from 'serialize-error' | ||
|
||
// locals | ||
import corePlugins from './corePlugins' | ||
|
||
// static rendering is used for "SSR" style rendering which is done on the | ||
// worker | ||
enableStaticRendering(true) | ||
|
||
interface WorkerConfiguration { | ||
plugins: PluginDefinition[] | ||
windowHref: string | ||
} | ||
|
||
// waits for a message from the main thread containing our configuration, which | ||
// must be sent on boot | ||
function receiveConfiguration() { | ||
const configurationP: Promise<WorkerConfiguration> = new Promise(resolve => { | ||
function listener(event: MessageEvent) { | ||
if (event.data.message === 'config') { | ||
resolve(event.data.config as WorkerConfiguration) | ||
removeEventListener('message', listener) | ||
} | ||
} | ||
self.addEventListener('message', listener) | ||
}) | ||
postMessage({ message: 'readyForConfig' }) | ||
return configurationP | ||
} | ||
|
||
async function getPluginManager() { | ||
// Load runtime plugins | ||
const config = await receiveConfiguration() | ||
const pluginLoader = new PluginLoader(config.plugins, { | ||
fetchESM: url => import(/* webpackIgnore:true */ url), | ||
}) | ||
pluginLoader.installGlobalReExports(self) | ||
const runtimePlugins = await pluginLoader.load(config.windowHref) | ||
const plugins = [...corePlugins.map(p => ({ plugin: p })), ...runtimePlugins] | ||
const pluginManager = new PluginManager(plugins.map(P => new P.plugin())) | ||
pluginManager.createPluggableElements() | ||
pluginManager.configure() | ||
|
||
return pluginManager | ||
} | ||
|
||
interface WrappedFuncArgs { | ||
rpcDriverClassName: string | ||
channel: string | ||
[key: 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(channel, message) | ||
}, | ||
}, | ||
rpcDriverClassName, | ||
) | ||
} | ||
} | ||
|
||
getPluginManager() | ||
.then(pluginManager => { | ||
const rpcConfig = Object.fromEntries( | ||
pluginManager | ||
.getRpcElements() | ||
.map(entry => [entry.name, wrapForRpc(entry.execute.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({ message: 'ready' }) | ||
}) | ||
.catch(error => { | ||
postMessage({ message: 'error', error: serializeError(error) }) | ||
}) | ||
|
||
export default () => { | ||
/* do nothing */ | ||
} |
33 changes: 33 additions & 0 deletions
33
products/jbrowse-react-linear-genome-view/src/workerPolyfill.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* eslint-disable no-restricted-globals */ | ||
|
||
// this is a little polyfill for running in workers that | ||
// contains just enough stubbing to make webpack style-loader | ||
// think that it is actually inserting styles into the DOM | ||
|
||
self.window = { | ||
addEventListener() {}, | ||
fetch: self.fetch.bind(self), | ||
location: self.location, | ||
Date: self.Date, | ||
requestIdleCallback: cb => cb(), | ||
cancelIdleCallback: () => {}, | ||
requestAnimationFrame: cb => cb(), | ||
cancelAnimationFrame: () => {}, | ||
navigator: {}, | ||
} | ||
self.document = { | ||
createTextNode() {}, | ||
querySelector() { | ||
return { appendChild() {} } | ||
}, | ||
documentElement: {}, | ||
querySelectorAll: () => [], | ||
createElement() { | ||
return { | ||
style: {}, | ||
setAttribute() {}, | ||
removeAttribute() {}, | ||
appendChild() {}, | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.