Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/extensionManagement/common/implicitActivationEvents.ts
5240 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { IStringDictionary } from '../../../base/common/collections.js';
7
import { onUnexpectedError } from '../../../base/common/errors.js';
8
import { ExtensionIdentifier, IExtensionDescription } from '../../extensions/common/extensions.js';
9
10
export interface IActivationEventsGenerator<T> {
11
(contributions: readonly T[]): Iterable<string>;
12
}
13
14
export class ImplicitActivationEventsImpl {
15
16
private readonly _generators = new Map<string, IActivationEventsGenerator<unknown>>();
17
private readonly _cache = new WeakMap<IExtensionDescription, string[]>();
18
19
public register<T>(extensionPointName: string, generator: IActivationEventsGenerator<T>): void {
20
this._generators.set(extensionPointName, generator as IActivationEventsGenerator<unknown>);
21
}
22
23
/**
24
* This can run correctly only on the renderer process because that is the only place
25
* where all extension points and all implicit activation events generators are known.
26
*/
27
public readActivationEvents(extensionDescription: IExtensionDescription): string[] {
28
if (!this._cache.has(extensionDescription)) {
29
this._cache.set(extensionDescription, this._readActivationEvents(extensionDescription));
30
}
31
return this._cache.get(extensionDescription)!;
32
}
33
34
/**
35
* This can run correctly only on the renderer process because that is the only place
36
* where all extension points and all implicit activation events generators are known.
37
*/
38
public createActivationEventsMap(extensionDescriptions: IExtensionDescription[]): { [extensionId: string]: string[] } {
39
const result: { [extensionId: string]: string[] } = Object.create(null);
40
for (const extensionDescription of extensionDescriptions) {
41
const activationEvents = this.readActivationEvents(extensionDescription);
42
if (activationEvents.length > 0) {
43
result[ExtensionIdentifier.toKey(extensionDescription.identifier)] = activationEvents;
44
}
45
}
46
return result;
47
}
48
49
private _readActivationEvents(desc: IExtensionDescription): string[] {
50
if (typeof desc.main === 'undefined' && typeof desc.browser === 'undefined') {
51
return [];
52
}
53
54
const activationEvents: string[] = (Array.isArray(desc.activationEvents) ? desc.activationEvents.slice(0) : []);
55
56
for (let i = 0; i < activationEvents.length; i++) {
57
// TODO@joao: there's no easy way to contribute this
58
if (activationEvents[i] === 'onUri') {
59
activationEvents[i] = `onUri:${ExtensionIdentifier.toKey(desc.identifier)}`;
60
}
61
}
62
63
if (!desc.contributes) {
64
// no implicit activation events
65
return activationEvents;
66
}
67
68
for (const extPointName in desc.contributes) {
69
const generator = this._generators.get(extPointName);
70
if (!generator) {
71
// There's no generator for this extension point
72
continue;
73
}
74
const contrib = (desc.contributes as IStringDictionary<unknown>)[extPointName];
75
const contribArr = Array.isArray(contrib) ? contrib : [contrib];
76
try {
77
activationEvents.push(...generator(contribArr));
78
} catch (err) {
79
onUnexpectedError(err);
80
}
81
}
82
83
return activationEvents;
84
}
85
}
86
87
export const ImplicitActivationEvents: ImplicitActivationEventsImpl = new ImplicitActivationEventsImpl();
88
89