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