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

Create extension point example #2124

Closed
wants to merge 2 commits into from
Closed
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
25 changes: 25 additions & 0 deletions packages/core/PluginManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ export default class PluginManager {

rootModel?: AbstractRootModel

extensionPoints: Map<string, Function[]> = new Map()

constructor(initialPlugins: (Plugin | PluginLoadRecord)[] = []) {
// add the core plugin
this.addPlugin({ plugin: new CorePlugin(), metadata: { isCore: true } })
Expand Down Expand Up @@ -551,4 +553,27 @@ export default class PluginManager {
): this {
return this.addElementType('rpc method', creationCallback)
}
addToExtensionPoint(
extensionPointName: string,
callback: (arg: unknown) => unknown,
) {
if (!this.extensionPoints.has(extensionPointName)) {
this.extensionPoints.set(extensionPointName, [])
}
const ep = this.extensionPoints.get(extensionPointName)
if (ep) {
// always true
ep.push(callback)
}
}
evaluateExtensionPoint(extensionPointName: string, startingArgs?: unknown) {
const ep = this.extensionPoints.get(extensionPointName)
let accumulator = startingArgs
if (ep) {
for (const callback of ep) {
accumulator = callback(accumulator)
}
}
return accumulator
}
}
14 changes: 14 additions & 0 deletions plugins/linear-genome-view/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
configSchema as linearBasicDisplayConfigSchemaFactory,
modelFactory as linearBasicDisplayModelFactory,
} from './LinearBasicDisplay'
import { IAnyModelType, types } from 'mobx-state-tree'

export default class LinearGenomeViewPlugin extends Plugin {
name = 'LinearGenomeViewPlugin'
Expand Down Expand Up @@ -117,6 +118,19 @@ export default class LinearGenomeViewPlugin extends Plugin {
),
}),
)

pluginManager.addToExtensionPoint(
'Core-extendSessionModel',
sessionModel => {
console.log('foopy')
return types.compose(
sessionModel as IAnyModelType,
types.model('HelloWorld', {
foopy: types.optional(types.string, 'lol'),
}),
)
},
)
}

configure(pluginManager: PluginManager) {
Expand Down
32 changes: 17 additions & 15 deletions products/jbrowse-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ It is likely preferable in most cases to install the tools first however
## Commands

<!-- commands -->
* [`jbrowse add-assembly SEQUENCE`](#jbrowse-add-assembly-sequence)
* [`jbrowse add-connection CONNECTIONURLORPATH`](#jbrowse-add-connection-connectionurlorpath)
* [`jbrowse add-track TRACK`](#jbrowse-add-track-track)
* [`jbrowse add-track-json TRACK`](#jbrowse-add-track-json-track)
* [`jbrowse admin-server`](#jbrowse-admin-server)
* [`jbrowse create LOCALPATH`](#jbrowse-create-localpath)
* [`jbrowse help [COMMAND]`](#jbrowse-help-command)
* [`jbrowse set-default-session`](#jbrowse-set-default-session)
* [`jbrowse upgrade [LOCALPATH]`](#jbrowse-upgrade-localpath)

- [`jbrowse add-assembly SEQUENCE`](#jbrowse-add-assembly-sequence)
- [`jbrowse add-connection CONNECTIONURLORPATH`](#jbrowse-add-connection-connectionurlorpath)
- [`jbrowse add-track TRACK`](#jbrowse-add-track-track)
- [`jbrowse add-track-json TRACK`](#jbrowse-add-track-json-track)
- [`jbrowse admin-server`](#jbrowse-admin-server)
- [`jbrowse create LOCALPATH`](#jbrowse-create-localpath)
- [`jbrowse help [COMMAND]`](#jbrowse-help-command)
- [`jbrowse set-default-session`](#jbrowse-set-default-session)
- [`jbrowse upgrade [LOCALPATH]`](#jbrowse-upgrade-localpath)

## `jbrowse add-assembly SEQUENCE`

Expand Down Expand Up @@ -70,7 +71,7 @@ OPTIONS
show CLI help

-l, --load=copy|symlink|move|inPlace
Required flag when using a local file. Choose how to manage the data directory. Copy, symlink, or move the data
Required flag when using a local file. Choose how to manage the data directory. Copy, symlink, or move the data
directory to the JBrowse directory. Or use inPlace to modify the config without doing any file operations

-n, --name=name
Expand Down Expand Up @@ -179,11 +180,11 @@ EXAMPLES
$ jbrowse add-connection http://mysite.com/jbrowse/data/
$ jbrowse add-connection http://mysite.com/jbrowse/custom_data_folder/ --type JBrowse1Connection
$ jbrowse add-connection http://mysite.com/path/to/hub.txt --assemblyName hg19
$ jbrowse add-connection http://mysite.com/path/to/custom_hub_name.txt --type UCSCTrackHubConnection --assemblyName
$ jbrowse add-connection http://mysite.com/path/to/custom_hub_name.txt --type UCSCTrackHubConnection --assemblyName
hg19
$ jbrowse add-connection http://mysite.com/path/to/custom --type custom --config
$ jbrowse add-connection http://mysite.com/path/to/custom --type custom --config
'{"uri":{"url":"https://mysite.com/path/to/custom"}}' --assemblyName hg19
$ jbrowse add-connection https://mysite.com/path/to/hub.txt --connectionId newId --name newName --target
$ jbrowse add-connection https://mysite.com/path/to/hub.txt --connectionId newId --name newName --target
/path/to/jb2/installation/config.json
```

Expand Down Expand Up @@ -254,7 +255,7 @@ EXAMPLES
# no --load flag to add literal URL for this track to config.json
$ jbrowse add-track https://mywebsite.com/my.bam

# --load move to move the file
# --load move to move the file
$ jbrowse add-track /path/to/my.bam --name 'New Track' --load move

# --load inPlace puts /url/relative/path.bam in the config without performing any file operations
Expand Down Expand Up @@ -397,7 +398,7 @@ OPTIONS

EXAMPLES
$ jbrowse set-default-session --session /path/to/default/session.json
$ jbrowse set-default-session --target /path/to/jb2/installation/config.json --view LinearGenomeView --tracks track1,
$ jbrowse set-default-session --target /path/to/jb2/installation/config.json --view LinearGenomeView --tracks track1,
track2, track3
$ jbrowse set-default-session --view LinearGenomeView, --name newName --viewId view-no-tracks
$ jbrowse set-default-session --currentSession # Prints out current default session
Expand Down Expand Up @@ -438,6 +439,7 @@ EXAMPLES
```

_See code: [src/commands/upgrade.ts](https://github.com/GMOD/jbrowse-components/blob/v1.3.2/products/jbrowse-cli/src/commands/upgrade.ts)_

<!-- commandsstop -->

## Debugging
Expand Down
8 changes: 7 additions & 1 deletion products/jbrowse-web/src/sessionModelFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
types,
walk,
Instance,
IAnyModelType,
} from 'mobx-state-tree'
import PluginManager from '@jbrowse/core/PluginManager'
import RpcManager from '@jbrowse/core/rpc/RpcManager'
Expand All @@ -51,7 +52,7 @@ export default function sessionModelFactory(
assemblyConfigSchemasType = types.frozen(), // if not using sessionAssemblies
) {
const minDrawerWidth = 128
const sessionModel = types
let sessionModel = types
.model('JBrowseWebSessionModel', {
id: types.optional(types.identifier, shortid()),
name: types.string,
Expand Down Expand Up @@ -695,6 +696,11 @@ export default function sessionModelFactory(
},
}))

sessionModel = pluginManager.evaluateExtensionPoint(
'Core-extendSessionModel',
sessionModel,
) as IAnyModelType

return types.snapshotProcessor(sessionModel, {
// @ts-ignore
preProcessor(snapshot) {
Expand Down