|
| 1 | +import { |
| 2 | + assertInInjectionContext, |
| 3 | + createEnvironmentInjector, |
| 4 | + DestroyRef, |
| 5 | + EnvironmentInjector, |
| 6 | + inject, |
| 7 | + InjectionToken, |
| 8 | + type Type |
| 9 | +} from '@angular/core'; |
| 10 | +import type { NgxsPlugin, NgxsPluginFn } from '@ngxs/store/plugins'; |
| 11 | + |
| 12 | +import { PluginManager } from './plugin-manager'; |
| 13 | +import { withNgxsPlugin } from './standalone-features/plugin'; |
| 14 | + |
| 15 | +const REGISTERED_PLUGINS = /* @__PURE__ */ new InjectionToken('', { |
| 16 | + factory: () => { |
| 17 | + const plugins = new Set(); |
| 18 | + inject(DestroyRef).onDestroy(() => plugins.clear()); |
| 19 | + return plugins; |
| 20 | + } |
| 21 | +}); |
| 22 | + |
| 23 | +/** |
| 24 | + * Dynamically registers an NGXS plugin in the current injection context. |
| 25 | + * |
| 26 | + * This function allows you to register NGXS plugins at runtime, creating an isolated |
| 27 | + * environment injector for the plugin. The plugin is automatically cleaned up when |
| 28 | + * the injection context is destroyed. In development mode, the function validates |
| 29 | + * that the same plugin is not registered multiple times. |
| 30 | + * |
| 31 | + * @param plugin - The NGXS plugin to register. Can be either a class type implementing |
| 32 | + * `NgxsPlugin` or a plugin function (`NgxsPluginFn`). |
| 33 | + * |
| 34 | + * @throws {Error} Throws an error if called outside of an injection context. |
| 35 | + * @throws {Error} In development mode, throws an error if the plugin has already been registered. |
| 36 | + * |
| 37 | + * @remarks |
| 38 | + * - Must be called within an injection context (e.g., constructor, field initializer, or `runInInjectionContext`). |
| 39 | + * - The created environment injector is automatically destroyed when the parent context is destroyed. |
| 40 | + * - Duplicate plugin registration is only checked in development mode for performance reasons. |
| 41 | + * |
| 42 | + * @example |
| 43 | + * ```ts |
| 44 | + * // Register a plugin class |
| 45 | + * import { MyThirdPartyIntegrationPlugin } from './plugins/third-party.plugin'; |
| 46 | + * |
| 47 | + * @Component({ |
| 48 | + * selector: 'app-root', |
| 49 | + * template: '...' |
| 50 | + * }) |
| 51 | + * export class AppComponent { |
| 52 | + * constructor() { |
| 53 | + * registerNgxsPlugin(MyThirdPartyIntegrationPlugin); |
| 54 | + * } |
| 55 | + * } |
| 56 | + * ``` |
| 57 | + * |
| 58 | + * @example |
| 59 | + * ```ts |
| 60 | + * // Register a plugin function |
| 61 | + * import { myThirdPartyIntegrationPluginFn } from './plugins/third-party.plugin'; |
| 62 | + * |
| 63 | + * @Component({ |
| 64 | + * selector: 'app-feature', |
| 65 | + * template: '...' |
| 66 | + * }) |
| 67 | + * export class FeatureComponent { |
| 68 | + * constructor() { |
| 69 | + * registerNgxsPlugin(myThirdPartyIntegrationPluginFn); |
| 70 | + * } |
| 71 | + * } |
| 72 | + * ``` |
| 73 | + * |
| 74 | + * @example |
| 75 | + * ```ts |
| 76 | + * // Register conditionally based on environment |
| 77 | + * import { MyDevtoolsPlugin } from './plugins/devtools.plugin'; |
| 78 | + * |
| 79 | + * @Component({ |
| 80 | + * selector: 'app-root', |
| 81 | + * template: '...' |
| 82 | + * }) |
| 83 | + * export class AppComponent { |
| 84 | + * constructor() { |
| 85 | + * if (ngDevMode) { |
| 86 | + * registerNgxsPlugin(MyDevtoolsPlugin); |
| 87 | + * } |
| 88 | + * } |
| 89 | + * } |
| 90 | + * ``` |
| 91 | + */ |
| 92 | +export function registerNgxsPlugin(plugin: Type<NgxsPlugin> | NgxsPluginFn) { |
| 93 | + ngDevMode && assertInInjectionContext(registerNgxsPlugin); |
| 94 | + |
| 95 | + if (typeof ngDevMode !== 'undefined' && ngDevMode) { |
| 96 | + const registeredPlugins = inject(REGISTERED_PLUGINS); |
| 97 | + if (registeredPlugins.has(plugin)) { |
| 98 | + throw new Error( |
| 99 | + 'Plugin has already been registered. Each plugin should only be registered once to avoid unexpected behavior.' |
| 100 | + ); |
| 101 | + } |
| 102 | + registeredPlugins.add(plugin); |
| 103 | + } |
| 104 | + |
| 105 | + // Create a new environment injector with the plugin configuration. |
| 106 | + // This isolates the plugin's dependencies and providers. |
| 107 | + const injector = createEnvironmentInjector( |
| 108 | + [PluginManager, withNgxsPlugin(plugin)], |
| 109 | + inject(EnvironmentInjector) |
| 110 | + ); |
| 111 | + |
| 112 | + // Ensure the created injector is destroyed when the injection context is destroyed. |
| 113 | + // This prevents memory leaks and ensures proper cleanup. |
| 114 | + inject(DestroyRef).onDestroy(() => injector.destroy()); |
| 115 | +} |
0 commit comments