Skip to content

Commit

Permalink
Allow passing props to extension point
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Sep 27, 2022
1 parent 4135f9c commit 36d9e36
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 56 deletions.
13 changes: 9 additions & 4 deletions packages/core/PluginManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ export default class PluginManager {

addToExtensionPoint<T>(
extensionPointName: string,
callback: (extendee: T) => T,
callback: (extendee: T, props: Record<string, unknown>) => T,
) {
let callbacks = this.extensionPoints.get(extensionPointName)
if (!callbacks) {
Expand All @@ -562,13 +562,17 @@ export default class PluginManager {
callbacks.push(callback)
}

evaluateExtensionPoint(extensionPointName: string, extendee: unknown) {
evaluateExtensionPoint(
extensionPointName: string,
extendee: unknown,
props?: Record<string, unknown>,
) {
const callbacks = this.extensionPoints.get(extensionPointName)
let accumulator = extendee
if (callbacks) {
for (const callback of callbacks) {
try {
accumulator = callback(accumulator)
accumulator = callback(accumulator, props)
} catch (error) {
console.error(error)
}
Expand All @@ -580,13 +584,14 @@ export default class PluginManager {
async evaluateAsyncExtensionPoint(
extensionPointName: string,
extendee: unknown,
props?: Record<string, unknown>,
) {
const callbacks = this.extensionPoints.get(extensionPointName)
let accumulator = extendee
if (callbacks) {
for (const callback of callbacks) {
try {
accumulator = await callback(accumulator)
accumulator = await callback(accumulator, props)
} catch (error) {
console.error(error)
}
Expand Down
8 changes: 4 additions & 4 deletions packages/core/util/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ export interface SessionWithConfigEditing extends AbstractSessionModel {
configuration: AnyConfigurationModel | SnapshotIn<AnyConfigurationModel>,
): void
}
export function isSessionWithAddTracks(thing: {
addTrackConf?: Function
disableAddTracks?: boolean
}): thing is SessionWithConfigEditing {
export function isSessionWithAddTracks(
thing: unknown,
): thing is SessionWithConfigEditing {
return (
// @ts-ignore
isSessionModel(thing) && 'addTrackConf' in thing && !thing.disableAddTracks
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ function HierarchicalTrackSelectorHeader({

const items = getEnv(model).pluginManager.evaluateExtensionPoint(
'TrackSelector-multiTrackMenuItems',
[],
{ session },
) as MenuItem[]
return (
<div
Expand Down
103 changes: 57 additions & 46 deletions plugins/wiggle/src/CreateMultiWiggleExtension/index.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,68 @@
import { lazy } from 'react'
import PluginManager from '@jbrowse/core/PluginManager'
import { readConfObject } from '@jbrowse/core/configuration'
import { getSession } from '@jbrowse/core/util'
import { getSession, isSessionWithAddTracks } from '@jbrowse/core/util'
import { HierarchicalTrackSelectorModel } from '@jbrowse/plugin-data-management'

const ConfirmDialog = lazy(() => import('./ConfirmDialog'))

export default function (pm: PluginManager) {
pm.addToExtensionPoint('TrackSelector-multiTrackMenuItems', () => {
return [
{
label: 'Create multi-wiggle track',
onClick: (model: HierarchicalTrackSelectorModel) => {
const tracks = model.selection
const trackIds = tracks.map(c => readConfObject(c, 'name'))
function makeTrack(arg: { name: string }) {
const subadapters = tracks
.map(c => readConfObject(c, 'adapter'))
.map((c, idx) => ({ ...c, source: trackIds[idx] }))
const assemblyNames = [
...new Set(
tracks.map(c => readConfObject(c, 'assemblyNames')).flat(),
),
]
const now = +Date.now()
const trackId = `multitrack-${now}-sessionTrack`
pm.addToExtensionPoint(
'TrackSelector-multiTrackMenuItems',
(items: unknown[], props: Record<string, unknown>) => {
const { session } = props
return [
...items,
...(isSessionWithAddTracks(session)
? [
{
label: 'Create multi-wiggle track',
onClick: (model: HierarchicalTrackSelectorModel) => {
const tracks = model.selection
const trackIds = tracks.map(c => readConfObject(c, 'name'))
function makeTrack(arg: { name: string }) {
const subadapters = tracks
.map(c => readConfObject(c, 'adapter'))
.map((c, idx) => ({ ...c, source: trackIds[idx] }))
const assemblyNames = [
...new Set(
tracks
.map(c => readConfObject(c, 'assemblyNames'))
.flat(),
),
]
const now = +Date.now()
const trackId = `multitrack-${now}-sessionTrack`

getSession(model).addTrackConf({
type: 'MultiQuantitativeTrack',
trackId,
name: arg.name,
assemblyNames,
adapter: {
type: 'MultiWiggleAdapter',
subadapters,
},
})
model.view.showTrack(trackId)
}
getSession(model).queueDialog(handleClose => [
ConfirmDialog,
{
tracks,
onClose: (arg: boolean, arg1: { name: string }) => {
if (arg) {
makeTrack(arg1)
}
handleClose()
getSession(model).addTrackConf({
type: 'MultiQuantitativeTrack',
trackId,
name: arg.name,
assemblyNames,
adapter: {
type: 'MultiWiggleAdapter',
subadapters,
},
})
model.view.showTrack(trackId)
}
getSession(model).queueDialog(handleClose => [
ConfirmDialog,
{
tracks,
onClose: (arg: boolean, arg1: { name: string }) => {
if (arg) {
makeTrack(arg1)
}
handleClose()
},
},
])
},
},
},
])
},
},
]
})
]
: []),
]
},
)
}
2 changes: 1 addition & 1 deletion plugins/wiggle/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import PluginManager from '@jbrowse/core/PluginManager'
import { FileLocation } from '@jbrowse/core/util/types'
import {
AdapterGuesser,
getFileName,
TrackTypeGuesser,
getFileName,
} from '@jbrowse/core/util/tracks'

// locals
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface ViewStateOptions {
plugins?: PluginConstructor[]
location?: string | Location
defaultSession?: SessionSnapshot
disableAddTracks: boolean
disableAddTracks?: boolean
onChange?: (patch: IJsonPatch, reversePatch: IJsonPatch) => void
}

Expand Down

0 comments on commit 36d9e36

Please sign in to comment.