-
Notifications
You must be signed in to change notification settings - Fork 61
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
Allow using web worker RPC on embedded LGV #3276
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if there is any reason to avoid just making the webworkerrpc config available, reduces the boilerplate in apps that want to use webworker rpc hence this diff