Path: blob/main/src/vs/platform/extensionManagement/common/implicitActivationEvents.ts
5240 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 { IStringDictionary } from '../../../base/common/collections.js';6import { onUnexpectedError } from '../../../base/common/errors.js';7import { ExtensionIdentifier, IExtensionDescription } from '../../extensions/common/extensions.js';89export interface IActivationEventsGenerator<T> {10(contributions: readonly T[]): Iterable<string>;11}1213export class ImplicitActivationEventsImpl {1415private readonly _generators = new Map<string, IActivationEventsGenerator<unknown>>();16private readonly _cache = new WeakMap<IExtensionDescription, string[]>();1718public register<T>(extensionPointName: string, generator: IActivationEventsGenerator<T>): void {19this._generators.set(extensionPointName, generator as IActivationEventsGenerator<unknown>);20}2122/**23* This can run correctly only on the renderer process because that is the only place24* where all extension points and all implicit activation events generators are known.25*/26public readActivationEvents(extensionDescription: IExtensionDescription): string[] {27if (!this._cache.has(extensionDescription)) {28this._cache.set(extensionDescription, this._readActivationEvents(extensionDescription));29}30return this._cache.get(extensionDescription)!;31}3233/**34* This can run correctly only on the renderer process because that is the only place35* where all extension points and all implicit activation events generators are known.36*/37public createActivationEventsMap(extensionDescriptions: IExtensionDescription[]): { [extensionId: string]: string[] } {38const result: { [extensionId: string]: string[] } = Object.create(null);39for (const extensionDescription of extensionDescriptions) {40const activationEvents = this.readActivationEvents(extensionDescription);41if (activationEvents.length > 0) {42result[ExtensionIdentifier.toKey(extensionDescription.identifier)] = activationEvents;43}44}45return result;46}4748private _readActivationEvents(desc: IExtensionDescription): string[] {49if (typeof desc.main === 'undefined' && typeof desc.browser === 'undefined') {50return [];51}5253const activationEvents: string[] = (Array.isArray(desc.activationEvents) ? desc.activationEvents.slice(0) : []);5455for (let i = 0; i < activationEvents.length; i++) {56// TODO@joao: there's no easy way to contribute this57if (activationEvents[i] === 'onUri') {58activationEvents[i] = `onUri:${ExtensionIdentifier.toKey(desc.identifier)}`;59}60}6162if (!desc.contributes) {63// no implicit activation events64return activationEvents;65}6667for (const extPointName in desc.contributes) {68const generator = this._generators.get(extPointName);69if (!generator) {70// There's no generator for this extension point71continue;72}73const contrib = (desc.contributes as IStringDictionary<unknown>)[extPointName];74const contribArr = Array.isArray(contrib) ? contrib : [contrib];75try {76activationEvents.push(...generator(contribArr));77} catch (err) {78onUnexpectedError(err);79}80}8182return activationEvents;83}84}8586export const ImplicitActivationEvents: ImplicitActivationEventsImpl = new ImplicitActivationEventsImpl();878889