-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPluginManager.js
218 lines (186 loc) · 6.71 KB
/
PluginManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { types } from 'mobx-state-tree'
import RendererType from './pluggableElementTypes/renderers/RendererType'
import AdapterType from './pluggableElementTypes/AdapterType'
import TrackType from './pluggableElementTypes/TrackType'
import ViewType from './pluggableElementTypes/ViewType'
import DrawerWidgetType from './pluggableElementTypes/DrawerWidgetType'
import MenuBarType from './pluggableElementTypes/MenuBarType'
import ConnectionType from './pluggableElementTypes/ConnectionType'
import { ConfigurationSchema } from './configuration'
import ReExports from './ReExports'
// little helper class that keeps groups of callbacks that are
// then run in a specified order by group
class PhasedScheduler {
phaseCallbacks = {}
constructor(...phaseOrder) {
this.phaseOrder = phaseOrder
}
add(phase, callback) {
if (!this.phaseOrder.includes(phase)) {
throw new Error(`unknown phase ${phase}`)
}
if (!this.phaseCallbacks[phase]) this.phaseCallbacks[phase] = []
this.phaseCallbacks[phase].push(callback)
}
run() {
this.phaseOrder.forEach(phaseName => {
if (this.phaseCallbacks[phaseName]) {
this.phaseCallbacks[phaseName].forEach(callback => callback())
}
})
}
}
export default class PluginManager {
plugins = []
elementTypes = {}
elementCreationSchedule = new PhasedScheduler(
'renderer',
'adapter',
'track',
'connection',
'view',
'drawer widget',
'menu bar',
)
typeBaseClasses = {
renderer: RendererType,
adapter: AdapterType,
track: TrackType,
connection: ConnectionType,
view: ViewType,
'drawer widget': DrawerWidgetType,
'menu bar': MenuBarType,
}
constructor(initialPlugins = []) {
this.lib = PluginManager.lib
this.getRendererType = this.getElementType.bind(this, 'renderer')
this.getAdapterType = this.getElementType.bind(this, 'adapter')
this.getTrackType = this.getElementType.bind(this, 'track')
this.getViewType = this.getElementType.bind(this, 'view')
this.getDrawerWidgetType = this.getElementType.bind(this, 'drawer widget')
this.getMenuBarType = this.getElementType.bind(this, 'menu bar')
this.getConnectionType = this.getElementType.bind(this, 'connection')
this.addRendererType = this.addElementType.bind(this, 'renderer')
this.addAdapterType = this.addElementType.bind(this, 'adapter')
this.addTrackType = this.addElementType.bind(this, 'track')
this.addViewType = this.addElementType.bind(this, 'view')
this.addDrawerWidgetType = this.addElementType.bind(this, 'drawer widget')
this.addMenuBarType = this.addElementType.bind(this, 'menu bar')
this.addConnectionType = this.addElementType.bind(this, 'connection')
// add all the initial plugins
initialPlugins.forEach(plugin => {
this.addPlugin(plugin)
})
}
/**
* Get the re-exported version of the given package name.
* Throws an error if the package is not re-exported by the plugin manager.
*
* @param {Array[string]} packageNames
* @returns {any} the library's default export
*/
jbrequire = lib => {
if (typeof lib === 'string') {
const pack = ReExports[lib]
if (!pack)
throw new Error(
`No jbrequire re-export defined for package '${lib}'. Either import it normally, or add it to jbrowse-core ReExports`,
)
return pack
}
if (typeof lib === 'function') {
return lib(this)
}
if (lib.default) return this.jbrequire(lib.default)
throw new TypeError(
'lib passed to jbrequire must be either a string or a function',
)
}
addPlugin(plugin) {
if (this.configured) {
throw new Error('JBrowse already configured, cannot add plugins')
}
if (this.plugins.includes(plugin)) {
throw new Error('plugin already installed')
}
// if (!plugin.install) console.error(plugin)
plugin.install(this)
this.plugins.push(plugin)
return this
}
/** get a MST type for the union of all specified pluggable MST types */
pluggableMstType(typeGroup, fieldName, fallback = types.maybe(types.null)) {
const pluggableTypes = this.getElementTypeMembers(typeGroup, fieldName)
// try to smooth over the case when no types are registered, mostly encountered in tests
if (pluggableTypes.length === 0) {
console.warn(
`No JBrowse pluggable types found matching ('${typeGroup}','${fieldName}'), returning null type`,
)
return fallback
}
return types.union(...pluggableTypes)
}
/** get a MST type for the union of all specified pluggable config schemas */
pluggableConfigSchemaType(
typeGroup,
fieldName = 'configSchema',
fallback = ConfigurationSchema('Null', {}),
) {
return this.pluggableMstType(typeGroup, fieldName, fallback)
}
addElementType(groupName, creationCallback) {
if (typeof creationCallback !== 'function') {
throw new Error(
'must provide a callback function that returns the new type object',
)
}
const typeBaseClass = this.typeBaseClasses[groupName]
if (!typeBaseClass) {
throw new Error(`unknown pluggable element type ${groupName}, cannot add`)
}
if (!this.elementTypes[groupName]) this.elementTypes[groupName] = {}
this.elementCreationSchedule.add(groupName, () => {
const element = creationCallback(this)
if (
groupName === 'adapter' &&
!element.AdapterClass.capabilities.length
) {
throw new Error(
`Adapter ${element.AdapterClass.name} must provide a static property "capabilities" that has at least one entry. See BaseAdapter for an example.`,
)
}
if (this.elementTypes[groupName][element.name]) {
throw new Error(
`${groupName} ${element.name} already registered, cannot register it again`,
)
}
this.elementTypes[groupName][element.name] = element
})
return this
}
getElementType(groupName, typeName) {
if (!typeName) {
throw new Error(`invalid type name "${typeName}"`)
}
return (this.elementTypes[groupName] || {})[typeName]
}
getElementTypesInGroup(groupName) {
return Object.values(this.elementTypes[groupName] || {})
}
getElementTypeMembers(groupName, memberName) {
return this.getElementTypesInGroup(groupName)
.map(t => t[memberName])
.filter(m => !!m)
}
configure() {
if (this.configured) throw new Error('already configured')
// run the creation callbacks for each element type in order.
// see elementCreationSchedule above for the creation order
this.elementCreationSchedule.run()
delete this.elementCreationSchedule
this.plugins.forEach(plugin => plugin.configure())
this.configured = true
// console.log(JSON.stringify(getSnapshot(model)))
return this
}
}