From dc0c92253dfd9d5be3eb65ea80c402603cedcb0d Mon Sep 17 00:00:00 2001 From: FinnStutzenstein Date: Mon, 10 Sep 2018 08:57:53 +0200 Subject: [PATCH] Type interference for the DS --- .../collectionStringModelMapper.service.ts | 10 +++--- .../app/core/services/data-store.service.ts | 34 +++++++++++-------- .../src/app/core/services/operator.service.ts | 2 +- client/src/app/shared/models/base.model.ts | 4 +-- .../app/shared/models/motions/motion-block.ts | 3 +- .../src/app/shared/models/motions/motion.ts | 2 +- client/src/app/shared/models/users/user.ts | 2 +- .../category-list/category-list.component.ts | 4 +-- .../motion-detail/motion-detail.component.ts | 4 +-- .../motion-list/motion-list.component.ts | 6 ++-- 10 files changed, 39 insertions(+), 32 deletions(-) diff --git a/client/src/app/core/services/collectionStringModelMapper.service.ts b/client/src/app/core/services/collectionStringModelMapper.service.ts index 8a26d5a43..e9d4ebc18 100644 --- a/client/src/app/core/services/collectionStringModelMapper.service.ts +++ b/client/src/app/core/services/collectionStringModelMapper.service.ts @@ -1,4 +1,4 @@ -import { ModelConstructor } from '../../shared/models/base.model'; +import { ModelConstructor, BaseModel } from '../../shared/models/base.model'; /** * Registeres the mapping of collection strings <--> actual types. Every Model should register itself here. @@ -8,14 +8,14 @@ export class CollectionStringModelMapperService { * Mapps collection strings to model constructors. Accessed by {@method registerCollectionElement} and * {@method getCollectionStringType}. */ - private static collectionStringsTypeMapping: { [collectionString: string]: ModelConstructor } = {}; + private static collectionStringsTypeMapping: { [collectionString: string]: ModelConstructor } = {}; /** * Registers the type to the collection string * @param collectionString * @param type */ - public static registerCollectionElement(collectionString: string, type: ModelConstructor) { + public static registerCollectionElement(collectionString: string, type: ModelConstructor) { CollectionStringModelMapperService.collectionStringsTypeMapping[collectionString] = type; } @@ -23,7 +23,7 @@ export class CollectionStringModelMapperService { * Returns the constructor of the requested collection or undefined, if it is not registered. * @param collectionString the requested collection */ - public static getModelConstructor(collectionString: string): ModelConstructor { + public static getModelConstructor(collectionString: string): ModelConstructor { return CollectionStringModelMapperService.collectionStringsTypeMapping[collectionString]; } @@ -31,7 +31,7 @@ export class CollectionStringModelMapperService { * Returns the collection string of a given ModelConstructor or undefined, if it is not registered. * @param ctor */ - public static getCollectionString(ctor: ModelConstructor): string { + public static getCollectionString(ctor: ModelConstructor): string { return Object.keys(CollectionStringModelMapperService.collectionStringsTypeMapping).find( (collectionString: string) => { return ctor === CollectionStringModelMapperService.collectionStringsTypeMapping[collectionString]; diff --git a/client/src/app/core/services/data-store.service.ts b/client/src/app/core/services/data-store.service.ts index 92feec958..8d22a4599 100644 --- a/client/src/app/core/services/data-store.service.ts +++ b/client/src/app/core/services/data-store.service.ts @@ -182,7 +182,7 @@ export class DataStoreService { }); } - private getCollectionString(collectionType: ModelConstructor | string): string { + private getCollectionString(collectionType: ModelConstructor | string): string { if (typeof collectionType === 'string') { return collectionType; } else { @@ -197,10 +197,10 @@ export class DataStoreService { * @param ids One ID of the BaseModel * @return The given BaseModel-subclass instance * @example: this.DS.get(User, 1) - * @example: this.DS.get('core/countdown', 2) + * @example: this.DS.get('core/countdown', 2) */ - public get(collectionType: ModelConstructor | string, id: number): T { - const collectionString = this.getCollectionString(collectionType); + public get(collectionType: ModelConstructor | string, id: number): T { + const collectionString = this.getCollectionString(collectionType); const collection: ModelCollection = this.modelStore[collectionString]; if (!collection) { @@ -211,14 +211,16 @@ export class DataStoreService { } /** - * Read multiple ID's from dataStore + * Read multiple ID's from dataStore. + * * @param collectionType The desired BaseModel or collectionString to be read from the dataStore * @param ids Multiple IDs as a list of IDs of BaseModel * @return The BaseModel-list corresponding to the given ID(s) - * @example: this.DS.get(User, [1,2,3,4,5]) + * @example: this.DS.getMany(User, [1,2,3,4,5]) + * @example: this.DS.getMany('users/user', [1,2,3,4,5]) */ - public getMany(collectionType: ModelConstructor | string, ids: number[]): T[] { - const collectionString = this.getCollectionString(collectionType); + public getMany(collectionType: ModelConstructor | string, ids: number[]): T[] { + const collectionString = this.getCollectionString(collectionType); const collection: ModelCollection = this.modelStore[collectionString]; if (!collection) { @@ -234,12 +236,14 @@ export class DataStoreService { /** * Get all models of the given collection from the DataStore. + * * @param collectionType The desired BaseModel or collectionString to be read from the dataStore * @return The BaseModel-list of all instances of T - * @example: this.DS.get(User) + * @example: this.DS.getAll(User) + * @example: this.DS.getAll('users/user') */ - public getAll(collectionType: ModelConstructor | string): T[] { - const collectionString = this.getCollectionString(collectionType); + public getAll(collectionType: ModelConstructor | string): T[] { + const collectionString = this.getCollectionString(collectionType); const collection: ModelCollection = this.modelStore[collectionString]; if (!collection) { @@ -258,14 +262,15 @@ export class DataStoreService { * @example this.DS.filter(User, myUser => myUser.first_name === "Max") */ public filter( - collectionType: ModelConstructor | string, + collectionType: ModelConstructor | string, callback: (model: T) => boolean ): T[] { return this.getAll(collectionType).filter(callback); } /** - * Add one or multiple models to dataStore + * Add one or multiple models to dataStore. + * * @param ...models The model(s) that shall be add use spread operator ("...") * @example this.DS.add(new User(1)) * @example this.DS.add((new User(2), new User(3))) @@ -296,7 +301,8 @@ export class DataStoreService { } /** - * removes one or multiple models from dataStore + * removes one or multiple models from dataStore. + * * @param Type The desired BaseModel type to be read from the dataStore * @param ...ids An or multiple IDs or a list of IDs of BaseModels. use spread operator ("...") for arrays * @example this.DS.remove(User, myUser.id, 3, 4) diff --git a/client/src/app/core/services/operator.service.ts b/client/src/app/core/services/operator.service.ts index fa9754a43..4777a5355 100644 --- a/client/src/app/core/services/operator.service.ts +++ b/client/src/app/core/services/operator.service.ts @@ -140,7 +140,7 @@ export class OperatorService extends OpenSlidesComponent { private updatePermissions(): void { this.permissions = []; if (!this.user) { - const defaultGroup = this.DS.get('users/group', 1) as Group; + const defaultGroup = this.DS.get('users/group', 1); if (defaultGroup && defaultGroup.permissions instanceof Array) { this.permissions = defaultGroup.permissions; } diff --git a/client/src/app/shared/models/base.model.ts b/client/src/app/shared/models/base.model.ts index 04da7eca8..5e2059292 100644 --- a/client/src/app/shared/models/base.model.ts +++ b/client/src/app/shared/models/base.model.ts @@ -2,8 +2,8 @@ import { OpenSlidesComponent } from 'app/openslides.component'; import { Deserializable } from './deserializable.model'; import { CollectionStringModelMapperService } from '../../core/services/collectionStringModelMapper.service'; -export interface ModelConstructor { - new (...args: any[]): BaseModel; +export interface ModelConstructor { + new (...args: any[]): T; } /** diff --git a/client/src/app/shared/models/motions/motion-block.ts b/client/src/app/shared/models/motions/motion-block.ts index 23c425ab3..88d685131 100644 --- a/client/src/app/shared/models/motions/motion-block.ts +++ b/client/src/app/shared/models/motions/motion-block.ts @@ -1,4 +1,5 @@ import { BaseModel } from '../base.model'; +import { Item } from '../agenda/item'; /** * Representation of a motion block. @@ -19,7 +20,7 @@ export class MotionBlock extends BaseModel { } public getAgenda(): BaseModel | BaseModel[] { - return this.DS.get('agenda/item', this.agenda_item_id); + return this.DS.get('agenda/item', this.agenda_item_id); } } diff --git a/client/src/app/shared/models/motions/motion.ts b/client/src/app/shared/models/motions/motion.ts index b3057b001..5704af5fa 100644 --- a/client/src/app/shared/models/motions/motion.ts +++ b/client/src/app/shared/models/motions/motion.ts @@ -76,7 +76,7 @@ export class Motion extends BaseModel { */ public initDataStoreValues() { // check the containing Workflows in DataStore - const allWorkflows = this.DS.getAll(Workflow); + const allWorkflows = this.DS.getAll(Workflow); allWorkflows.forEach(localWorkflow => { if (localWorkflow.isStateContained(this.state_id)) { this.workflow = localWorkflow as Workflow; diff --git a/client/src/app/shared/models/users/user.ts b/client/src/app/shared/models/users/user.ts index 7be60bd06..bdd94d95d 100644 --- a/client/src/app/shared/models/users/user.ts +++ b/client/src/app/shared/models/users/user.ts @@ -34,7 +34,7 @@ export class User extends BaseModel { } public get groups(): Group[] { - return this.DS.getMany(Group, this.groups_id); + return this.DS.getMany(Group, this.groups_id); } public get full_name(): string { diff --git a/client/src/app/site/motions/category-list/category-list.component.ts b/client/src/app/site/motions/category-list/category-list.component.ts index 42ae05538..009524180 100644 --- a/client/src/app/site/motions/category-list/category-list.component.ts +++ b/client/src/app/site/motions/category-list/category-list.component.ts @@ -53,7 +53,7 @@ export class CategoryListComponent extends BaseComponent implements OnInit { */ public ngOnInit() { super.setTitle('Category'); - this.categoryArray = this.DS.getAll(Category); + this.categoryArray = this.DS.getAll(Category); this.dataSource = new MatTableDataSource(this.categoryArray); this.dataSource.sort = this.sort; @@ -61,7 +61,7 @@ export class CategoryListComponent extends BaseComponent implements OnInit { // The alternative approach is to put the observable as DataSource to the table this.DS.changeObservable.subscribe(newModel => { if (newModel instanceof Category) { - this.categoryArray = this.DS.getAll(Category); + this.categoryArray = this.DS.getAll(Category); this.dataSource.data = this.categoryArray; } }); diff --git a/client/src/app/site/motions/motion-detail/motion-detail.component.ts b/client/src/app/site/motions/motion-detail/motion-detail.component.ts index 81faa3488..d23fa6c17 100644 --- a/client/src/app/site/motions/motion-detail/motion-detail.component.ts +++ b/client/src/app/site/motions/motion-detail/motion-detail.component.ts @@ -88,7 +88,7 @@ export class MotionDetailComponent extends BaseComponent implements OnInit { // load existing motion this.route.params.subscribe(params => { // has the motion of the DataStore was initialized before. - this.motion = this.DS.get(Motion, params.id) as Motion; + this.motion = this.DS.get(Motion, params.id); // Observe motion to get the motion in the parameter and also get the changes this.DS.changeObservable.subscribe(newModel => { @@ -165,7 +165,7 @@ export class MotionDetailComponent extends BaseComponent implements OnInit { * return all Categories. */ public getMotionCategories(): Category[] { - return this.DS.getAll(Category); + return this.DS.getAll(Category); } /** diff --git a/client/src/app/site/motions/motion-list/motion-list.component.ts b/client/src/app/site/motions/motion-list/motion-list.component.ts index 290771bd1..e2c6b3f7d 100644 --- a/client/src/app/site/motions/motion-list/motion-list.component.ts +++ b/client/src/app/site/motions/motion-list/motion-list.component.ts @@ -93,8 +93,8 @@ export class MotionListComponent extends BaseComponent implements OnInit { */ public ngOnInit() { super.setTitle('Motions'); - this.workflowArray = this.DS.getAll(Workflow); - this.motionArray = this.DS.getAll(Motion); + this.workflowArray = this.DS.getAll(Workflow); + this.motionArray = this.DS.getAll(Motion); this.dataSource = new MatTableDataSource(this.motionArray); this.dataSource.paginator = this.paginator; this.dataSource.sort = this.sort; @@ -103,7 +103,7 @@ export class MotionListComponent extends BaseComponent implements OnInit { // The alternative approach is to put the observable as DataSource to the table this.DS.changeObservable.subscribe(newModel => { if (newModel instanceof Motion) { - this.motionArray = this.DS.getAll(Motion); + this.motionArray = this.DS.getAll(Motion); this.dataSource.data = this.motionArray; } });