-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathconfig.ts
40 lines (35 loc) · 1.04 KB
/
config.ts
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
class Pluggable {
installedPlugins: any
constructor() {
this.installedPlugins = []
}
install(handler, options = {}) {
if (typeof handler !== 'function') {
console.error('plugin.install must receive a function')
handler = () => ({})
}
this.installedPlugins.push({ handler, options })
}
extend(instance) {
const invokeSetup = (plugin) => plugin.handler(instance) // invoke the setup method passed to install
const bindProperty = ([property, value]: [string, any]) => {
instance[property] =
typeof value === 'function' ? value.bind(instance) : value
}
const addAllPropertiesFromSetup = (setupResult) => {
setupResult = typeof setupResult === 'object' ? setupResult : {}
Object.entries(setupResult).forEach(bindProperty)
}
this.installedPlugins.map(invokeSetup).forEach(addAllPropertiesFromSetup)
}
/** For testing */
reset() {
this.installedPlugins = []
}
}
export const config = {
plugins: {
VueWrapper: new Pluggable(),
DOMWrapper: new Pluggable()
}
}