From 01befbb07dc8cb49f70368c31cf48ed6015beb89 Mon Sep 17 00:00:00 2001 From: Johannes Millan Date: Tue, 19 Apr 2022 20:47:48 +0200 Subject: [PATCH] feat(perf): simplify distinctUntilChanged check --- .../work-context/store/work-context.selectors.ts | 4 ++++ .../work-context/work-context.service.ts | 7 ++++--- src/app/util/is-shallow-equal.ts | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 src/app/util/is-shallow-equal.ts diff --git a/src/app/features/work-context/store/work-context.selectors.ts b/src/app/features/work-context/store/work-context.selectors.ts index 12c213ec84..45f0ae411f 100644 --- a/src/app/features/work-context/store/work-context.selectors.ts +++ b/src/app/features/work-context/store/work-context.selectors.ts @@ -72,6 +72,10 @@ export const selectActiveWorkContext = createSelector( }, ); +export const selectActiveContextTheme = createSelector( + selectActiveWorkContext, + (workContext) => workContext.theme, +); export const selectStartableTasksForActiveContext = createSelector( selectActiveWorkContext, selectTaskEntities, diff --git a/src/app/features/work-context/work-context.service.ts b/src/app/features/work-context/work-context.service.ts index c83a1b1a51..3d2846f0e0 100644 --- a/src/app/features/work-context/work-context.service.ts +++ b/src/app/features/work-context/work-context.service.ts @@ -28,7 +28,6 @@ import { import { TODAY_TAG } from '../tag/tag.const'; import { TagService } from '../tag/tag.service'; import { Task, TaskPlanned, TaskWithSubTasks } from '../tasks/task.model'; -import { distinctUntilChangedObject } from '../../util/distinct-until-changed-object'; import { getWorklogStr } from '../../util/get-work-log-str'; import { hasTasksToWorkOn, mapEstimateRemainingFromTasks } from './work-context.util'; import { @@ -65,6 +64,8 @@ import { selectNotesById } from '../note/store/note.reducer'; import { TranslateService } from '@ngx-translate/core'; import { T } from '../../t.const'; import { distinctUntilChangedSimpleArray } from '../../util/distinct-until-changed-simple-array'; +import { isShallowEqual } from '../../util/is-shallow-equal'; +import { distinctUntilChangedObject } from '../../util/distinct-until-changed-object'; @Injectable({ providedIn: 'root', @@ -81,7 +82,7 @@ export class WorkContextService { // should be treated as private _afterDataLoaded$: Observable = this._isAllDataLoaded$.pipe( - filter((v) => v === true), + filter((v) => v), shareReplay(1), ); @@ -170,7 +171,7 @@ export class WorkContextService { currentTheme$: Observable = this.activeWorkContext$.pipe( map((awc) => awc.theme), - distinctUntilChanged(distinctUntilChangedObject), + distinctUntilChanged(isShallowEqual), ); advancedCfg$: Observable = this.activeWorkContext$.pipe( diff --git a/src/app/util/is-shallow-equal.ts b/src/app/util/is-shallow-equal.ts new file mode 100644 index 0000000000..745314c1c7 --- /dev/null +++ b/src/app/util/is-shallow-equal.ts @@ -0,0 +1,16 @@ +export const isShallowEqual = ( + a: { [key: string]: boolean | number | string | undefined | null }, + b: { [key: string]: boolean | number | string | undefined | null }, +): boolean => { + const keys1 = Object.keys(a); + const keys2 = Object.keys(b); + if (keys1.length !== keys2.length) { + return false; + } + for (const key of keys1) { + if (a[key] !== b[key]) { + return false; + } + } + return true; +};