2023-05-31 02:26:37 -05:00
import type { PluginExtensionConfig } from '@grafana/data' ;
2023-03-02 08:42:00 -06:00
import type { AppPluginConfig } from '@grafana/runtime' ;
2023-05-10 04:03:15 -05:00
import { startMeasure , stopMeasure } from 'app/core/utils/metrics' ;
2023-11-14 01:35:40 -06:00
import { getPluginSettings } from 'app/features/plugins/pluginSettings' ;
2022-04-22 08:33:13 -05:00
2024-04-24 02:33:16 -05:00
import { ReactivePluginExtensionsRegistry } from './extensions/reactivePluginExtensionRegistry' ;
2023-03-02 08:42:00 -06:00
import * as pluginLoader from './plugin_loader' ;
2021-11-10 12:06:55 -06:00
2023-03-02 08:42:00 -06:00
export type PluginPreloadResult = {
pluginId : string ;
error? : unknown ;
2023-05-31 02:26:37 -05:00
extensionConfigs : PluginExtensionConfig [ ] ;
2023-03-02 08:42:00 -06:00
} ;
2024-04-24 08:37:22 -05:00
export async function preloadPlugins (
apps : AppPluginConfig [ ] = [ ] ,
registry : ReactivePluginExtensionsRegistry ,
eventName = 'frontend_plugins_preload'
) {
startMeasure ( eventName ) ;
2024-04-24 02:33:16 -05:00
const promises = apps . filter ( ( config ) = > config . preload ) . map ( ( config ) = > preload ( config ) ) ;
const preloadedPlugins = await Promise . all ( promises ) ;
for ( const preloadedPlugin of preloadedPlugins ) {
registry . register ( preloadedPlugin ) ;
}
2024-04-24 08:37:22 -05:00
stopMeasure ( eventName ) ;
2021-11-10 12:06:55 -06:00
}
2023-03-02 08:42:00 -06:00
async function preload ( config : AppPluginConfig ) : Promise < PluginPreloadResult > {
const { path , version , id : pluginId } = config ;
2021-11-10 12:06:55 -06:00
try {
2023-05-10 04:03:15 -05:00
startMeasure ( ` frontend_plugin_preload_ ${ pluginId } ` ) ;
2023-06-05 03:51:36 -05:00
const { plugin } = await pluginLoader . importPluginModule ( {
path ,
version ,
2023-11-10 04:44:54 -06:00
isAngular : config.angular.detected ,
2023-06-05 03:51:36 -05:00
pluginId ,
} ) ;
2023-04-03 03:42:15 -05:00
const { extensionConfigs = [ ] } = plugin ;
2023-11-14 01:35:40 -06:00
// Fetching meta-information for the preloaded app plugin and caching it for later.
// (The function below returns a promise, but it's not awaited for a reason: we don't want to block the preload process, we would only like to cache the result for later.)
getPluginSettings ( pluginId ) ;
2023-04-03 03:42:15 -05:00
return { pluginId , extensionConfigs } ;
2023-03-02 08:42:00 -06:00
} catch ( error ) {
console . error ( ` [Plugins] Failed to preload plugin: ${ path } (version: ${ version } ) ` , error ) ;
2023-04-03 03:42:15 -05:00
return { pluginId , extensionConfigs : [ ] , error } ;
2023-05-10 04:03:15 -05:00
} finally {
stopMeasure ( ` frontend_plugin_preload_ ${ pluginId } ` ) ;
2021-11-10 12:06:55 -06:00
}
}