-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathextensionSlot.ts
More file actions
83 lines (71 loc) · 2.15 KB
/
Copy pathextensionSlot.ts
File metadata and controls
83 lines (71 loc) · 2.15 KB
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
import {
AppHost,
ContributionPredicate,
ExtensionItem,
ExtensionItemFilter,
ExtensionSlot,
Shell,
SlotKey,
CustomExtensionSlot,
CustomExtensionSlotHandler,
Contribution
} from './API'
import _ from 'lodash'
export interface AnyExtensionSlot {
readonly name: string
readonly declaringShell?: Shell
}
const alwaysTrue = () => true
export function createExtensionSlot<T>(key: SlotKey<T>, host: AppHost, declaringShell?: Shell): ExtensionSlot<T> & AnyExtensionSlot {
let items: ExtensionItem<T>[] = []
return {
host,
declaringShell,
name: key.name,
contribute,
getItems,
getSingleItem,
getItemByName,
discardBy
}
function contribute(fromShell: Shell, item: T, condition?: ContributionPredicate): Contribution {
const contribution = {
shell: fromShell,
contribution: item,
condition: condition || alwaysTrue,
uniqueId: _.uniqueId(`${fromShell.name}_extItem_`)
}
items.push(contribution)
return {
unsubscribe: () => {
items = items.filter(i => i !== contribution)
}
}
}
function getItems(forceAll: boolean = false): ExtensionItem<T>[] {
return forceAll ? items : items.filter(item => item.condition())
}
function getSingleItem(): ExtensionItem<T> {
return items.find(item => item.condition()) as ExtensionItem<T>
}
function getItemByName(name: string): ExtensionItem<T> {
return items.find(item => item.name === name && item.condition()) as ExtensionItem<T>
}
function discardBy(predicate: ExtensionItemFilter<T>) {
items = items.filter(v => !predicate(v))
}
}
export function createCustomExtensionSlot<T>(
key: SlotKey<T>,
handler: CustomExtensionSlotHandler<T>,
host: AppHost,
declaringShell?: Shell
): CustomExtensionSlot<T> & AnyExtensionSlot {
return {
name: key.name,
host,
declaringShell,
contribute: (...args) => handler.contribute(...args),
discardBy: (...args) => handler.discardBy(...args)
}
}