Path: blob/main/src/vs/platform/extensionManagement/common/implicitActivationEvents.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { onUnexpectedError } from '../../../base/common/errors.js';6import { ExtensionIdentifier, IExtensionDescription } from '../../extensions/common/extensions.js';78export interface IActivationEventsGenerator<T> {9(contributions: T[], result: { push(item: string): void }): void;10}1112export class ImplicitActivationEventsImpl {1314private readonly _generators = new Map<string, IActivationEventsGenerator<any>>();15private readonly _cache = new WeakMap<IExtensionDescription, string[]>();1617public register<T>(extensionPointName: string, generator: IActivationEventsGenerator<T>): void {18this._generators.set(extensionPointName, generator);19}2021/**22* This can run correctly only on the renderer process because that is the only place23* where all extension points and all implicit activation events generators are known.24*/25public readActivationEvents(extensionDescription: IExtensionDescription): string[] {26if (!this._cache.has(extensionDescription)) {27this._cache.set(extensionDescription, this._readActivationEvents(extensionDescription));28}29return this._cache.get(extensionDescription)!;30}3132/**33* This can run correctly only on the renderer process because that is the only place34* where all extension points and all implicit activation events generators are known.35*/36public createActivationEventsMap(extensionDescriptions: IExtensionDescription[]): { [extensionId: string]: string[] } {37const result: { [extensionId: string]: string[] } = Object.create(null);38for (const extensionDescription of extensionDescriptions) {39const activationEvents = this.readActivationEvents(extensionDescription);40if (activationEvents.length > 0) {41result[ExtensionIdentifier.toKey(extensionDescription.identifier)] = activationEvents;42}43}44return result;45}4647private _readActivationEvents(desc: IExtensionDescription): string[] {48if (typeof desc.main === 'undefined' && typeof desc.browser === 'undefined') {49return [];50}5152const activationEvents: string[] = (Array.isArray(desc.activationEvents) ? desc.activationEvents.slice(0) : []);5354for (let i = 0; i < activationEvents.length; i++) {55// TODO@joao: there's no easy way to contribute this56if (activationEvents[i] === 'onUri') {57activationEvents[i] = `onUri:${ExtensionIdentifier.toKey(desc.identifier)}`;58}59}6061if (!desc.contributes) {62// no implicit activation events63return activationEvents;64}6566for (const extPointName in desc.contributes) {67const generator = this._generators.get(extPointName);68if (!generator) {69// There's no generator for this extension point70continue;71}72const contrib = (desc.contributes as any)[extPointName];73const contribArr = Array.isArray(contrib) ? contrib : [contrib];74try {75generator(contribArr, activationEvents);76} catch (err) {77onUnexpectedError(err);78}79}8081return activationEvents;82}83}8485export const ImplicitActivationEvents: ImplicitActivationEventsImpl = new ImplicitActivationEventsImpl();868788