sw-fe/src/apps/AppRegistry.js

84 lines
1.7 KiB
JavaScript

/**
* AppRegistry - Central registry for app definitions
*
* Apps register themselves here with their mount functions and metadata.
* This is the "catalog" of available apps - not instances.
*/
import { SLOT_IDS } from './types';
/** @type {Map<string, import('./types').AppDefinition>} */
const registry = new Map();
/**
* Register an app definition
* @param {import('./types').AppDefinition} definition
*/
export function registerApp(definition) {
if (!definition.appId) {
throw new Error('App definition must have appId');
}
if (!definition.mountWidget) {
throw new Error('App definition must have mountWidget function');
}
if (registry.has(definition.appId)) {
console.warn(`App ${definition.appId} already registered, overwriting`);
}
registry.set(definition.appId, {
order: 0,
defaultSlot: SLOT_IDS.TOP_RIGHT,
permissions: {},
...definition,
});
}
/**
* Get an app definition by ID
* @param {string} appId
* @returns {import('./types').AppDefinition | undefined}
*/
export function getApp(appId) {
return registry.get(appId);
}
/**
* Get all registered apps
* @returns {import('./types').AppDefinition[]}
*/
export function getAllApps() {
return Array.from(registry.values());
}
/**
* Check if an app is registered
* @param {string} appId
* @returns {boolean}
*/
export function hasApp(appId) {
return registry.has(appId);
}
/**
* Unregister an app (mainly for testing)
* @param {string} appId
*/
export function unregisterApp(appId) {
registry.delete(appId);
}
/**
* Clear all registered apps (mainly for testing)
*/
export function clearRegistry() {
registry.clear();
}
export default {
registerApp,
getApp,
getAllApps,
hasApp,
unregisterApp,
clearRegistry,
};