diff --git a/src/core/classes/items-management/items-manager-sources-tracker.ts b/src/core/classes/items-management/items-manager-sources-tracker.ts index 997f5c556..8b7f5743e 100644 --- a/src/core/classes/items-management/items-manager-sources-tracker.ts +++ b/src/core/classes/items-management/items-manager-sources-tracker.ts @@ -18,6 +18,7 @@ type SourceConstructor = T extends { new(...args: unknown[]): infer P } ? P : never; type InstanceTracking = { instance: CoreItemsManagerSource; references: unknown[] }; type Instances = Record; @@ -36,14 +37,14 @@ export class CoreItemsManagerSourcesTracker { * @param constructorArguments Arguments to create a new instance, used to find out if an instance already exists. * @returns Source. */ - static getOrCreateSource( - constructor: SourceConstructor, - constructorArguments: ConstructorParameters>, - ): T { + static getOrCreateSource>( + constructor: C, + constructorArguments: ConstructorParameters, + ): SourceConstuctorInstance { const id = constructor.getSourceId(...constructorArguments); const constructorInstances = this.getConstructorInstances(constructor); - return constructorInstances[id]?.instance as T + return constructorInstances[id]?.instance as SourceConstuctorInstance ?? this.createInstance(id, constructor, constructorArguments); } diff --git a/src/core/classes/items-management/items-manager.ts b/src/core/classes/items-management/items-manager.ts index 8ec06d964..ebab07bc7 100644 --- a/src/core/classes/items-management/items-manager.ts +++ b/src/core/classes/items-management/items-manager.ts @@ -21,13 +21,13 @@ import { CoreItemsManagerSourcesTracker } from './items-manager-sources-tracker' /** * Helper to manage a collection of items in a page. */ -export abstract class CoreItemsManager { +export abstract class CoreItemsManager = CoreItemsManagerSource> { - protected source?: { instance: CoreItemsManagerSource; unsubscribe: () => void }; + protected source?: { instance: Source; unsubscribe: () => void }; protected itemsMap: Record | null = null; protected selectedItem: Item | null = null; - constructor(source: CoreItemsManagerSource) { + constructor(source: Source) { this.setSource(source); } @@ -36,7 +36,7 @@ export abstract class CoreItemsManager { * * @returns Source. */ - getSource(): CoreItemsManagerSource { + getSource(): Source { if (!this.source) { throw new Error('Source is missing from items manager'); } @@ -49,7 +49,7 @@ export abstract class CoreItemsManager { * * @param newSource New source. */ - setSource(newSource: CoreItemsManagerSource | null): void { + setSource(newSource: Source | null): void { if (this.source) { CoreItemsManagerSourcesTracker.removeReference(this.source.instance, this); diff --git a/src/core/classes/items-management/list-items-manager.ts b/src/core/classes/items-management/list-items-manager.ts index ff1b87ec8..cfb7f4750 100644 --- a/src/core/classes/items-management/list-items-manager.ts +++ b/src/core/classes/items-management/list-items-manager.ts @@ -26,13 +26,16 @@ import { CoreItemsManagerSource } from './items-manager-source'; /** * Helper class to manage the state and routing of a list of items in a page. */ -export abstract class CoreListItemsManager extends CoreItemsManager { +export abstract class CoreListItemsManager< + Item = unknown, + Source extends CoreItemsManagerSource = CoreItemsManagerSource +> extends CoreItemsManager { protected pageRouteLocator?: unknown | ActivatedRoute; protected splitView?: CoreSplitViewComponent; protected splitViewOutletSubscription?: Subscription; - constructor(source: CoreItemsManagerSource, pageRouteLocator: unknown | ActivatedRoute) { + constructor(source: Source, pageRouteLocator: unknown | ActivatedRoute) { super(source); this.pageRouteLocator = pageRouteLocator; diff --git a/src/core/classes/items-management/swipe-items-manager.ts b/src/core/classes/items-management/swipe-items-manager.ts index 18cb6b40c..c18c3398d 100644 --- a/src/core/classes/items-management/swipe-items-manager.ts +++ b/src/core/classes/items-management/swipe-items-manager.ts @@ -17,11 +17,16 @@ import { ActivatedRoute } from '@angular/router'; import { CoreNavigator } from '@services/navigator'; import { CoreItemsManager } from './items-manager'; +import { CoreItemsManagerSource } from './items-manager-source'; /** * Helper class to manage the state and routing of a swipeable page. */ -export abstract class CoreSwipeItemsManager extends CoreItemsManager { +export abstract class CoreSwipeItemsManager< + Item = unknown, + Source extends CoreItemsManagerSource = CoreItemsManagerSource +> + extends CoreItemsManager { /** * Process page started operations.