Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | 100x 132x 132x 132x 132x 132x 132x 132x 58x 64x 64x 98x 13x 13x 13x 70x 12x 12x 13x 70x 572x 83x 83x 72x 11x 4x 11x 61x 100x 61x 20x 15x 20x 41x 41x 41x 41x 83x 4x 20x 10x 10x 10x 10x 10x 19x 19x 19x 20x 27x 100x 40x 5x 35x 35x 1x 1x 1x 34x 34x 26x 26x 26x 23x 44x 23x 3x 20x 6x 4x 6x 6x 26x 26x 86x 37x 14x 100x | /*
Copyright (C) 2022 Sber
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { shallowReactive, type Component } from 'vue';
import { getLoggerWithTag } from '@global/logger/v2/logger.mjs';
import { loadLegacyPlugin } from './loaders/legacy-plugin-loader';
import type { PluginCatalogEntry } from '../../types/plugin-descriptor';
const logger = getLoggerWithTag('f/p/plugin-manager');
export interface LegacyPluginMetadata {
id: string;
documents: string[];
entry: string;
// Общие чанки, которые должны быть загружены до entry.
scripts: string[];
styles: string[];
}
export type CatalogPluginMetadata = PluginCatalogEntry;
export class PluginManager {
private readonly documentRenderers = shallowReactive(new Map<string, Component>());
private readonly pluginIds = new Set<string>();
private readonly reservedDocumentTypes = new Set<string>();
private readonly pluginLoads = new Map<string, Promise<void>>();
private readonly catalogLoaders = new Map<string, () => Promise<void>>();
private readonly catalogPlugins = new Map<string, CatalogPluginMetadata>();
private readonly pluginsByDocumentType = shallowReactive(new Map<string, LegacyPluginMetadata>());
registerDocumentRenderer(
documentType: string,
renderer: Component
): void {
this.documentRenderers.set(documentType, renderer);
}
registerAvailablePlugin(plugin: LegacyPluginMetadata): void {
this.pluginIds.add(plugin.id);
for (const documentType of plugin.documents) {
this.pluginsByDocumentType.set(documentType, plugin);
}
}
registerAvailablePlugins(plugins: LegacyPluginMetadata[]): void {
for (const plugin of plugins) {
this.registerAvailablePlugin(plugin);
}
logger.info(
() => `Зарегистрирована metadata локальных lazy-плагинов: [${plugins.length}]`
);
}
reserveLocalPlugins(
plugins: Pick<LegacyPluginMetadata, 'id' | 'documents'>[],
builtInDocumentTypes: string[]
): void {
for (const plugin of plugins) {
this.pluginIds.add(plugin.id);
for (const documentType of plugin.documents) {
this.reservedDocumentTypes.add(documentType);
}
}
for (const documentType of builtInDocumentTypes) {
this.reservedDocumentTypes.add(documentType);
}
}
registerCatalogPlugins(
plugins: CatalogPluginMetadata[],
loadPlugin: (plugin: CatalogPluginMetadata) => Promise<void> = loadLegacyPlugin
): void {
let registeredCount = 0;
for (const plugin of plugins) {
if (this.pluginIds.has(plugin.id)) {
logger.warn(
() => `Плагин каталога [${plugin.id}] пропущен: ID уже занят`
);
continue;
}
const conflictingType = plugin.documents.find((documentType) => {
return this.reservedDocumentTypes.has(documentType)
|| this.documentRenderers.has(documentType)
|| this.pluginsByDocumentType.has(documentType);
});
if (conflictingType) {
logger.warn(
() => `Плагин каталога [${plugin.id}] пропущен: `
+ `тип документа [${conflictingType}] уже занят`
);
continue;
}
// Принимаем всю запись только после проверки всех заявленных типов.
this.registerAvailablePlugin(plugin);
this.catalogLoaders.set(plugin.id, () => loadPlugin(plugin));
this.catalogPlugins.set(plugin.id, plugin);
registeredCount++;
}
logger.info(
() => `Зарегистрированы плагины каталога: [${registeredCount}]`
);
}
replaceCatalogPlugins(
plugins: CatalogPluginMetadata[],
loadPlugin: (plugin: CatalogPluginMetadata) => Promise<void> = loadLegacyPlugin
): void {
// Сбрасываем только внешние регистрации. Старый JS, CSS и его side effects
// остаются на странице; при проблемах требуется Hard reset.
for (const [pluginId, plugin] of this.catalogPlugins) {
this.pluginIds.delete(pluginId);
this.pluginLoads.delete(pluginId);
this.catalogLoaders.delete(pluginId);
this.catalogPlugins.delete(pluginId);
for (const documentType of plugin.documents) {
Eif (this.pluginsByDocumentType.get(documentType)?.id === pluginId) {
this.pluginsByDocumentType.delete(documentType);
this.documentRenderers.delete(documentType);
}
}
}
this.registerCatalogPlugins(plugins, loadPlugin);
}
getDocumentRenderer(documentType: string): Component | null {
return this.documentRenderers.get(documentType) || null;
}
getAvailablePluginByDocumentType(
documentType: string
): LegacyPluginMetadata | null {
return this.pluginsByDocumentType.get(documentType) || null;
}
loadPluginByDocumentType(documentType: string): Promise<void> {
if (this.hasDocumentRenderer(documentType)) {
return Promise.resolve();
}
const plugin = this.getAvailablePluginByDocumentType(documentType);
if (!plugin) {
const error = new Error(
`Плагин для типа документа [${documentType}] не найден`
);
logger.warn(() => error.message);
return Promise.reject(error);
}
const currentLoad = this.pluginLoads.get(plugin.id);
if (currentLoad) return currentLoad;
logger.info(() => `Начинаем загрузку плагина [${plugin.id}] для типа [${documentType}]`);
const load = this.catalogLoaders.get(plugin.id) || (() => loadLegacyPlugin(plugin));
const pluginLoad = load()
.then(() => {
const missingDocumentTypes = plugin.documents.filter(
(type) => !this.hasDocumentRenderer(type)
);
if (missingDocumentTypes.length) {
throw new Error(
`Плагин [${plugin.id}] не зарегистрировал типы документов: `
+ missingDocumentTypes.join(', ')
);
}
logger.info(() => `Плагин [${plugin.id}] успешно загружен`);
})
.catch((error: unknown) => {
if (this.pluginLoads.get(plugin.id) === pluginLoad) {
this.pluginLoads.delete(plugin.id);
}
logger.error(() => `Ошибка загрузки плагина [${plugin.id}]`, error);
throw error;
});
this.pluginLoads.set(plugin.id, pluginLoad);
return pluginLoad;
}
hasDocumentRenderer(documentType: string): boolean {
return this.documentRenderers.has(documentType);
}
// Включает типы lazy-плагинов, код которых ещё не загружен.
getAvailableDocumentTypes(): string[] {
return [...new Set([
...this.documentRenderers.keys(),
...this.pluginsByDocumentType.keys()
])];
}
getDocumentTypes(): string[] {
return [...this.documentRenderers.keys()];
}
}
export const pluginManager = new PluginManager();
|