| 16 | * @template U - The type of plugin instance |
| 17 | */ |
| 18 | export class PluginRegistry< |
| 19 | T extends DragDropManager<any, any>, |
| 20 | W extends PluginConstructor<T> = PluginConstructor<T>, |
| 21 | U extends Plugin<T> = InstanceType<W>, |
| 22 | > { |
| 23 | private instances: Map<W, U> = new Map(); |
| 24 | |
| 25 | /** |
| 26 | * Creates a new plugin registry. |
| 27 | * |
| 28 | * @param manager - The drag and drop manager that owns this registry |
| 29 | */ |
| 30 | constructor(private manager: T) {} |
| 31 | |
| 32 | /** |
| 33 | * Gets all registered plugin instances. |
| 34 | * |
| 35 | * @returns An array of all active plugin instances |
| 36 | */ |
| 37 | public get values(): U[] { |
| 38 | return Array.from(this.instances.values()); |
| 39 | } |
| 40 | |
| 41 | #previousValues: PluginConstructor<T>[] = []; |
| 42 | |
| 43 | /** |
| 44 | * Sets the list of plugins to be used by the registry. |
| 45 | * |
| 46 | * @param entries - Array of plugin constructors or descriptors |
| 47 | * @remarks |
| 48 | * This method: |
| 49 | * - Filters out duplicate plugins |
| 50 | * - Unregisters plugins that are no longer in use |
| 51 | * - Registers new plugins with their options |
| 52 | */ |
| 53 | public set values(entries: Plugins<T>) { |
| 54 | const descriptors = entries |
| 55 | .map(descriptor) |
| 56 | .reduce<PluginDescriptor<T>[]>((acc, descriptor) => { |
| 57 | const existing = acc.find(({plugin}) => plugin === descriptor.plugin); |
| 58 | |
| 59 | if (existing) { |
| 60 | // Keep the first occurrence's position, apply latest options |
| 61 | existing.options = descriptor.options; |
| 62 | return acc; |
| 63 | } |
| 64 | |
| 65 | return [...acc, descriptor]; |
| 66 | }, []); |
| 67 | const constructors = descriptors.map(({plugin}) => plugin); |
| 68 | |
| 69 | for (const plugin of this.#previousValues) { |
| 70 | if (!constructors.includes(plugin)) { |
| 71 | if (plugin.prototype instanceof CorePlugin) { |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | this.unregister(plugin as W); |
nothing calls this directly
no outgoing calls
no test coverage detected