-
Notifications
You must be signed in to change notification settings - Fork 1
/
get.ts
202 lines (193 loc) · 5.71 KB
/
get.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
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
/*
* Copyright © 2023 Norman Fomferra
* Permissions are hereby granted under the terms of the MIT License:
* https://opensource.org/licenses/MIT.
*/
import memoizeOne from "memoize-one";
import type { ContributionPoint, Extension } from "@/core/types";
import { getExtensionContext } from "@/core/extension-context/get";
import { frameworkStore } from "@/core/store";
import { getExtensions } from "@/core/extension/get";
/**
* Gets the contribution point for the given contribution point identifier.
*
* @category Extension Contribution API
* @param contribPointId - The contribution point identifier.
* @returns A read-only map of code contributions or `undefined`
* if it cannot be found.
*/
export function getContributionPoint<TM = unknown, TS = TM>(
contribPointId: string
): ContributionPoint<TM, TS> | undefined;
/**
* Gets the contribution point for the given contribution point identifier.
*
* @category Extension Contribution API
* @param contribPointId - The contribution point identifier.
* @param mustExist - If `true`, an error is raised
* if the contribution point does not exist.
* @returns A read-only map of code contributions.
*/
export function getContributionPoint<TM = unknown, TS = TM>(
contribPointId: string,
mustExist: true
): ContributionPoint<TM, TS>;
export function getContributionPoint<TM = unknown, TS = TM>(
contribPointId: string,
mustExist?: true
): ContributionPoint<TM, TS> | undefined {
const contributionPoint = frameworkStore.getState().contributionPoints[
contribPointId
] as ContributionPoint<TM, TS> | undefined;
if (mustExist && !contributionPoint) {
throw new Error(`Unregistered contribution point '${contribPointId}'.`);
}
return contributionPoint;
}
/**
* Gets a stable snapshot of the framework's contribution points.
*
* @category Extension Contribution API
*/
export function getContributionPoints(): ContributionPoint[] {
return getContributionPointsMemo(
frameworkStore.getState().contributionPoints
);
}
/**
* Memoized implementation helper for hooks.
* @internal
*/
export const getContributionPointsMemo = memoizeOne(
(
contributionPoints: Record<string, ContributionPoint>
): ContributionPoint[] => {
return Object.values(contributionPoints);
}
);
/**
* Gets a stable snapshot of the processed contributions from
* the manifest (package.json) of the registered extensions.
*
* Note, an extension does not need to be activated nor will it be
* activated while its contributions are collected.
*
* @category Extension Contribution API
* @param contribPointId - The contribution point identifier.
* @param contribKey - An optional key if the contribution
* is a key-value mapping.
* @returns An array of all extension contributions.
* If a contribution is an array, its flattened items are appended
* to the return value.
*/
export function getContributions<T>(
contribPointId: string,
contribKey?: string | undefined | null
): T[] {
return getContributionsMemo(
getExtensions(),
contribPointId,
contribKey
) as T[];
}
/**
* Memoized implementation helper.
* @internal
*/
export const getContributionsMemo = memoizeOne(
<T>(
extensions: Extension[],
contribPointId: string,
contribKey?: string | undefined | null
): T[] => {
const items: T[] = [];
extensions.forEach((extension) => {
const contrib = getExtensionContribution<T>(
extension.id,
contribPointId,
contribKey
);
if (contrib) {
if (Array.isArray(contrib)) {
items.push(...(contrib as T[]));
} else {
items.push(contrib);
}
}
});
return items;
}
);
/**
* Gets a snapshot of the processed contributions from the
* manifest (package.json) of the registered extensions as a
* mapping of extension identifiers to extension contributions.
*
* Note, an extension does not need to be activated nor will it be
* activated while its contributions are collected.
*
* @category Extension Contribution API
* @param contribPointId - The contribution point identifier.
* @param contribKey - An optional key if the contribution
* is a key-value mapping.
* @returns A mapping of extension identifier to extension contribution.
*/
export function getExtensionContributions<T>(
contribPointId: string,
contribKey?: string | undefined | null
): ReadonlyMap<string, T> {
return getExtensionContributionsMemo(
getExtensions(),
contribPointId,
contribKey
) as ReadonlyMap<string, T>;
}
/**
* Memoized implementation helper.
* @internal
*/
export const getExtensionContributionsMemo = memoizeOne(
<T>(
extensions: Extension[],
contribPointId: string,
contribKey?: string | undefined | null
): ReadonlyMap<string, T> => {
const map = new Map<string, T>();
extensions.forEach((extension) => {
const contrib = getExtensionContribution<T>(
extension.id,
contribPointId,
contribKey
);
if (contrib !== undefined) {
map.set(extension.id, contrib);
}
});
return map;
}
);
function getExtensionContribution<T>(
extensionId: string,
contribPointId: string,
contribKey: string | undefined | null
): T | undefined {
const ctx = getExtensionContext(extensionId, true);
let contrib = ctx.contributions.get(contribPointId);
if (!contrib) {
return;
}
if (typeof contribKey === "string") {
if (typeof contrib !== "object") {
throw new Error(
`Extension '${ctx.extensionId}': ` +
`contributions to point '${contribPointId}' ` +
`must be given as object, but was ${typeof contrib}.`
);
}
contrib = (contrib as Record<string, unknown>)[contribKey];
if (!contrib) {
return;
}
}
return contrib as T;
}