-
Notifications
You must be signed in to change notification settings - Fork 1
/
load.ts
44 lines (42 loc) · 1.7 KB
/
load.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
/*
* Copyright © 2023 Norman Fomferra
* Permissions are hereby granted under the terms of the MIT License:
* https://opensource.org/licenses/MIT.
*/
import { getStoreRecord } from "@/core/store";
import { emitActivationEvent } from "@/core/code-contrib/emit";
import { getContributionPoint } from "@/core/contrib-point/get";
/**
* Load code contribution data.
* If the extension that provides the contribution
* is not yet activated it will be activated using the
* given `contribPoint.activationEvent`, if any.
*
* @category Extension Contribution API
* @typeParam Value - Type of the loaded code contribution value
* @param contribPointId - The contribution point identifier.
* @param contribId - The code contribution identifier.
* @returns A promise that resolves to the code contribution data.
* @throws Error - If the contribution point is unknown
* or if the contribution identifier is not registered
*/
export async function loadCodeContribution<Value>(
contribPointId: string,
contribId: string
): Promise<Value> {
let contribDataMap = getStoreRecord("codeContributions", contribPointId);
if (!contribDataMap || !contribDataMap.has(contribId)) {
const contribPoint = getContributionPoint(contribPointId, true);
const activationEvent = contribPoint.codeInfo?.activationEvent;
if (activationEvent) {
await emitActivationEvent(activationEvent.replace("${id}", contribId));
}
}
contribDataMap = getStoreRecord("codeContributions", contribPointId);
if (!contribDataMap || !contribDataMap.has(contribId)) {
throw new Error(
`Unregistered code contribution '${contribPointId}/${contribId}'.`
);
}
return Promise.resolve(contribDataMap.get(contribId) as Value);
}