diff --git a/src/Squidex/app/components/internal/app/settings/clients-page.component.ts b/src/Squidex/app/components/internal/app/settings/clients-page.component.ts index 188999e13..bf8b6901b 100644 --- a/src/Squidex/app/components/internal/app/settings/clients-page.component.ts +++ b/src/Squidex/app/components/internal/app/settings/clients-page.component.ts @@ -13,8 +13,8 @@ import { AppClientsService, AppComponentBase, AppsStoreService, - ArrayHelper, CreateAppClientDto, + ImmutableList, NotificationService, UpdateAppClientDto, UsersProviderService @@ -30,7 +30,7 @@ function rename(client: AppClientDto, name: string) { template }) export class ClientsPageComponent extends AppComponentBase implements Ng2.OnInit { - public appClients: AppClientDto[]; + public appClients: ImmutableList; public createForm = this.formBuilder.group({ @@ -57,7 +57,7 @@ export class ClientsPageComponent extends AppComponentBase implements Ng2.OnInit this.appName() .switchMap(app => this.appClientsService.getClients(app).retry(2)) .subscribe(dtos => { - this.appClients = dtos; + this.appClients = new ImmutableList(dtos); }, error => { this.notifyError(error); }); @@ -67,7 +67,7 @@ export class ClientsPageComponent extends AppComponentBase implements Ng2.OnInit this.appName() .switchMap(app => this.appClientsService.deleteClient(app, client.id)) .subscribe(() => { - this.appClients = ArrayHelper.remove(this.appClients, client); + this.appClients = this.appClients.remove(client); }, error => { this.notifyError(error); }); @@ -77,7 +77,7 @@ export class ClientsPageComponent extends AppComponentBase implements Ng2.OnInit this.appName() .switchMap(app => this.appClientsService.updateClient(app, client.id, new UpdateAppClientDto(name))) .subscribe(() => { - this.appClients = ArrayHelper.replace(this.appClients, client, rename(client, name)); + this.appClients = this.appClients.update(client, c => rename(c, name)); }, error => { this.notifyError(error); }); @@ -94,7 +94,7 @@ export class ClientsPageComponent extends AppComponentBase implements Ng2.OnInit this.appName() .switchMap(app => this.appClientsService.postClient(app, dto)) .subscribe(dto => { - this.appClients = ArrayHelper.push(this.appClients, dto); + this.appClients = this.appClients.add(dto); this.reset(); }, error => { this.notifyError(error); diff --git a/src/Squidex/app/components/internal/app/settings/contributors-page.component.ts b/src/Squidex/app/components/internal/app/settings/contributors-page.component.ts index 963908c7b..387e8f9c5 100644 --- a/src/Squidex/app/components/internal/app/settings/contributors-page.component.ts +++ b/src/Squidex/app/components/internal/app/settings/contributors-page.component.ts @@ -14,10 +14,10 @@ import { AppContributorDto, AppContributorsService, AppsStoreService, - ArrayHelper, AuthService, AutocompleteItem, AutocompleteSource, + ImmutableList, NotificationService, UserDto, UsersProviderService, @@ -61,7 +61,7 @@ function changePermission(contributor: AppContributorDto, permission: string): A template }) export class ContributorsPageComponent extends AppComponentBase implements Ng2.OnInit { - public appContributors: AppContributorDto[] = []; + public appContributors = new ImmutableList(); public currentUserId: string; @@ -95,7 +95,7 @@ export class ContributorsPageComponent extends AppComponentBase implements Ng2.O this.appName() .switchMap(app => this.appContributorsService.getContributors(app).retry(2)) .subscribe(dtos => { - this.appContributors = dtos; + this.appContributors = new ImmutableList(dtos); }, error => { this.notifyError(error); }); @@ -114,7 +114,7 @@ export class ContributorsPageComponent extends AppComponentBase implements Ng2.O this.appName() .switchMap(app => this.appContributorsService.postContributor(app, contributor)) .subscribe(() => { - this.appContributors = ArrayHelper.push(this.appContributors, contributor); + this.appContributors = this.appContributors.add(contributor); }, error => { this.notifyError(error); }); @@ -126,7 +126,7 @@ export class ContributorsPageComponent extends AppComponentBase implements Ng2.O this.appName() .switchMap(app => this.appContributorsService.postContributor(app, newContributor)) .subscribe(() => { - this.appContributors = ArrayHelper.replace(this.appContributors, contributor, newContributor); + this.appContributors = this.appContributors.update(contributor, c => newContributor); }, error => { this.notifyError(error); }); @@ -136,7 +136,7 @@ export class ContributorsPageComponent extends AppComponentBase implements Ng2.O this.appName() .switchMap(app => this.appContributorsService.deleteContributor(app, contributor.contributorId)) .subscribe(() => { - this.appContributors = ArrayHelper.push(this.appContributors, contributor); + this.appContributors = this.appContributors.add(contributor); }, error => { this.notifyError(error); }); diff --git a/src/Squidex/app/components/internal/app/settings/languages-page.component.ts b/src/Squidex/app/components/internal/app/settings/languages-page.component.ts index f472813e2..deb51100b 100644 --- a/src/Squidex/app/components/internal/app/settings/languages-page.component.ts +++ b/src/Squidex/app/components/internal/app/settings/languages-page.component.ts @@ -13,9 +13,9 @@ import { AppLanguageDto, AppLanguagesService, AppsStoreService, - ArrayHelper, LanguageDto, LanguageService, + ImmutableList, NotificationService, UpdateAppLanguageDto, UsersProviderService @@ -28,7 +28,7 @@ import { }) export class LanguagesPageComponent extends AppComponentBase implements Ng2.OnInit { public allLanguages: LanguageDto[] = []; - public appLanguages: AppLanguageDto[] = []; + public appLanguages = new ImmutableList(); public selectedLanguage: LanguageDto | null = null; @@ -58,7 +58,7 @@ export class LanguagesPageComponent extends AppComponentBase implements Ng2.OnIn this.appName() .switchMap(app => this.appLanguagesService.getLanguages(app).retry(2)) .subscribe(dtos => { - this.appLanguages = dtos; + this.appLanguages = new ImmutableList(); }, error => { this.notifyError(error); }); @@ -68,7 +68,7 @@ export class LanguagesPageComponent extends AppComponentBase implements Ng2.OnIn this.appName() .switchMap(app => this.appLanguagesService.postLanguages(app, new AddAppLanguageDto(this.selectedLanguage.iso2Code))) .subscribe(dto => { - this.appLanguages = ArrayHelper.push(this.appLanguages, dto); + this.appLanguages = this.appLanguages.add(dto); }, error => { this.notifyError(error); }); @@ -80,7 +80,7 @@ export class LanguagesPageComponent extends AppComponentBase implements Ng2.OnIn this.appName() .switchMap(app => this.appLanguagesService.deleteLanguage(app, language.iso2Code)) .subscribe(dto => { - this.appLanguages = ArrayHelper.push(this.appLanguages, language); + this.appLanguages = this.appLanguages.remove(dto); }, error => { this.notifyError(error); }); diff --git a/src/Squidex/app/framework/declarations.ts b/src/Squidex/app/framework/declarations.ts index 8b4228d1d..8514ff9ce 100644 --- a/src/Squidex/app/framework/declarations.ts +++ b/src/Squidex/app/framework/declarations.ts @@ -44,7 +44,6 @@ export * from './utils/date-time'; export * from './utils/duration'; export * from './utils/immutable-id-map'; export * from './utils/immutable-list'; -export * from './utils/immutable-object'; export * from './utils/immutable-set'; export * from './utils/math-helper'; export * from './utils/modal-view'; diff --git a/src/Squidex/app/framework/utils/immutable-list.ts b/src/Squidex/app/framework/utils/immutable-list.ts index d543439db..21781a850 100644 --- a/src/Squidex/app/framework/utils/immutable-list.ts +++ b/src/Squidex/app/framework/utils/immutable-list.ts @@ -7,9 +7,13 @@ import * as Immutable from 'immutable'; -export class ImmutableList { +export class ImmutableList implements Iterable { private readonly items: Immutable.List; + public [Symbol.iterator](): Iterator { + return this.items.values(); + } + public get size(): number { return this.items.size; } @@ -17,7 +21,7 @@ export class ImmutableList { constructor(items?: T[] | Immutable.List) { if (Array.isArray(items)) { this.items = Immutable.List(items); - } else { + } else if (items) { this.items = items || Immutable.List(); } @@ -32,8 +36,8 @@ export class ImmutableList { return this.items.toArray(); } - public map(projection: (item: T) => R): R[] { - return this.items.map(v => projection(v!)).toArray(); + public map(projection: (item: T) => R): ImmutableList { + return new ImmutableList(this.items.map(v => projection(v!)).toArray()); } public filter(projection: (item: T) => boolean): T[] { diff --git a/src/Squidex/app/framework/utils/immutable-object.spec.ts b/src/Squidex/app/framework/utils/immutable-object.spec.ts deleted file mode 100644 index 1c539eb0e..000000000 --- a/src/Squidex/app/framework/utils/immutable-object.spec.ts +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Squidex Headless CMS - * - * @license - * Copyright (c) Sebastian Stehle. All rights reserved - */ - -import { ImmutableObject } from './../'; - -class MockupObject extends ImmutableObject { - constructor( - public property1: string, - public property2: string - ) { - super(); - } - - public changeProperty1(newValue: string): MockupObject { - return super.cloned((x: MockupObject) => x.property1 = newValue); - } - - public clone(): ImmutableObject { - return new MockupObject(this.property1, this.property2); - } -} - -describe('ImmutableObject', () => { - it('should create new instance on update', () => { - const oldObj = new MockupObject('old1', 'old2'); - const newObj = oldObj.changeProperty1('new1'); - - expect(oldObj.property1).toBe('old1'); - expect(oldObj.property2).toBe('old2'); - - expect(newObj.property1).toBe('new1'); - expect(newObj.property2).toBe('old2'); - }); -}); diff --git a/src/Squidex/app/framework/utils/immutable-object.ts b/src/Squidex/app/framework/utils/immutable-object.ts deleted file mode 100644 index eb16636d4..000000000 --- a/src/Squidex/app/framework/utils/immutable-object.ts +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Squidex Headless CMS - * - * @license - * Copyright (c) Sebastian Stehle. All rights reserved - */ - -/* tslint:disable:no-empty */ - -export abstract class ImmutableObject { - public abstract clone(): ImmutableObject; - - protected afterClone() { } - - protected cloned(updater: (instance: ImmutableObject) => void) { - const cloned = this.clone(); - - updater(cloned); - - cloned.afterClone(); - - return cloned; - } -} \ No newline at end of file diff --git a/src/Squidex/app/framework/utils/immutable-set.ts b/src/Squidex/app/framework/utils/immutable-set.ts index a7bdc366d..a484938da 100644 --- a/src/Squidex/app/framework/utils/immutable-set.ts +++ b/src/Squidex/app/framework/utils/immutable-set.ts @@ -7,13 +7,17 @@ import * as Immutable from 'immutable'; -export class ImmutableSet { +export class ImmutableSet implements Iterable { private readonly items: Immutable.Set; public get size(): number { return this.items.size; } + public [Symbol.iterator](): Iterator { + return this.items.values(); + } + constructor(items?: T[] | Immutable.Set) { if (Array.isArray(items)) { this.items = Immutable.Set(items);