feat(perf): simplify distinctUntilChanged check

This commit is contained in:
Johannes Millan 2022-04-19 20:47:48 +02:00
parent d7edddc173
commit 01befbb07d
3 changed files with 24 additions and 3 deletions

View file

@ -72,6 +72,10 @@ export const selectActiveWorkContext = createSelector(
},
);
export const selectActiveContextTheme = createSelector(
selectActiveWorkContext,
(workContext) => workContext.theme,
);
export const selectStartableTasksForActiveContext = createSelector(
selectActiveWorkContext,
selectTaskEntities,

View file

@ -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<unknown> = this._isAllDataLoaded$.pipe(
filter((v) => v === true),
filter((v) => v),
shareReplay(1),
);
@ -170,7 +171,7 @@ export class WorkContextService {
currentTheme$: Observable<WorkContextThemeCfg> = this.activeWorkContext$.pipe(
map((awc) => awc.theme),
distinctUntilChanged(distinctUntilChangedObject),
distinctUntilChanged<WorkContextThemeCfg>(isShallowEqual),
);
advancedCfg$: Observable<WorkContextAdvancedCfg> = this.activeWorkContext$.pipe(

View file

@ -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;
};