From 9f7ab2e1547be5b0cd46a35632da0dcb988ff078 Mon Sep 17 00:00:00 2001 From: Arjunlal B Date: Wed, 20 Sep 2023 15:13:18 -0700 Subject: [PATCH] fix: add `getOnce` method to preference service --- .../src/preference/preference.service.ts | 18 +++++++++ .../src/table/table.component.test.ts | 6 ++- .../components/src/table/table.component.ts | 39 ++++++++----------- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/projects/common/src/preference/preference.service.ts b/projects/common/src/preference/preference.service.ts index d498d341d..890feb61c 100644 --- a/projects/common/src/preference/preference.service.ts +++ b/projects/common/src/preference/preference.service.ts @@ -55,6 +55,24 @@ export class PreferenceService { ); } + /** + * Returns the current storage value if defined, else the default value. + */ + public getOnce( + key: PreferenceKey, + defaultValue?: T, + type: StorageType = PreferenceService.DEFAULT_STORAGE_TYPE + ): T { + const storedValue = this.preferenceStorage(type).get(this.asStorageKey(key)); + const value = this.fromStorageValue(storedValue) ?? defaultValue; + + if (value === undefined) { + throw Error(`No value found or default provided for preferenceKey: ${key}`); + } + + return value; + } + public set( key: PreferenceKey, value: PreferenceValue, diff --git a/projects/components/src/table/table.component.test.ts b/projects/components/src/table/table.component.test.ts index a99b7a62c..3a7020b80 100644 --- a/projects/components/src/table/table.component.test.ts +++ b/projects/components/src/table/table.component.test.ts @@ -1,7 +1,7 @@ /* eslint-disable max-lines */ import { fakeAsync, flush } from '@angular/core/testing'; import { ActivatedRoute, convertToParamMap } from '@angular/router'; -import { DomElementMeasurerService, NavigationService } from '@hypertrace/common'; +import { DomElementMeasurerService, NavigationService, PreferenceService } from '@hypertrace/common'; import { runFakeRxjs } from '@hypertrace/test-utils'; import { createHostFactory, mockProvider } from '@ngneat/spectator/jest'; import { MockComponent } from 'ng-mocks'; @@ -75,6 +75,10 @@ describe('Table component', () => { }), mockProvider(ModalService, { createModal: jest.fn().mockReturnValue({ closed$: of([]) }) + }), + mockProvider(PreferenceService, { + getOnce: jest.fn().mockReturnValue({ columns: [] }), + set: jest.fn() }) ], declarations: [MockComponent(PaginatorComponent), MockComponent(SearchBoxComponent)], diff --git a/projects/components/src/table/table.component.ts b/projects/components/src/table/table.component.ts index f598e7871..0fd4ecf87 100644 --- a/projects/components/src/table/table.component.ts +++ b/projects/components/src/table/table.component.ts @@ -37,8 +37,8 @@ import { TypedSimpleChanges } from '@hypertrace/common'; import { isNil, pick, without } from 'lodash-es'; -import { BehaviorSubject, combineLatest, merge, Observable, of, Subject } from 'rxjs'; -import { filter, first, map, switchMap, take, tap } from 'rxjs/operators'; +import { BehaviorSubject, combineLatest, merge, Observable, Subject } from 'rxjs'; +import { filter, map, switchMap, take, tap } from 'rxjs/operators'; import { FilterAttribute } from '../filtering/filter/filter-attribute'; import { LoadAsyncConfig } from '../load-async/load-async.service'; import { PageEvent } from '../paginator/page.event'; @@ -705,19 +705,17 @@ export class TableComponent private saveTablePreferences(columns: TableColumnConfig[]): void { if (isNonEmptyString(this.id)) { - this.getLocalPreferences().subscribe(preferences => - this.setLocalPreferences({ - ...preferences, - columns: columns.map(column => this.dehydratePersistedColumnConfig(column)) - }) - ); + this.setLocalPreferences({ + ...this.getLocalPreferences(), + columns: columns.map(column => this.dehydratePersistedColumnConfig(column)) + }); } } - private getLocalPreferences(): Observable { + private getLocalPreferences(): TableLocalPreferences { return isNonEmptyString(this.id) - ? this.preferenceService.get(this.id, {}, StorageType.Local).pipe(first()) - : of({ columns: [] }); + ? this.preferenceService.getOnce(this.id, {}, StorageType.Local) + : { columns: [] }; } private setLocalPreferences(preferences: TableLocalPreferences): void { @@ -773,18 +771,13 @@ export class TableComponent this.columnDefaultConfigs = columnConfigurations; } - this.getLocalPreferences() - .pipe(take(1)) - .subscribe(preferences => { - const restoredColumnConfigs = this.hydratePersistedColumnConfigs( - columnConfigurations, - preferences.columns ?? [] - ); - const visibleColumns = restoredColumnConfigs.filter(column => column.visible); - this.initialColumnConfigIdWidthMap = new Map(visibleColumns.map(column => [column.id, column.width ?? -1])); - this.updateVisibleColumns(visibleColumns); - this.columnConfigsSubject.next(restoredColumnConfigs); - }); + const preferences = this.getLocalPreferences(); + + const restoredColumnConfigs = this.hydratePersistedColumnConfigs(columnConfigurations, preferences.columns ?? []); + const visibleColumns = restoredColumnConfigs.filter(column => column.visible); + this.initialColumnConfigIdWidthMap = new Map(visibleColumns.map(column => [column.id, column.width ?? -1])); + this.updateVisibleColumns(visibleColumns); + this.columnConfigsSubject.next(restoredColumnConfigs); } private checkColumnWidthCompatibilityOrThrow(width?: TableColumnWidth): void {