Skip to content

Commit

Permalink
View model list equality check helper
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianjoel committed Dec 16, 2024
1 parent 24250ce commit 4c640e6
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 33 deletions.
20 changes: 20 additions & 0 deletions client/src/app/infrastructure/utils/functions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Identifiable } from 'src/app/domain/interfaces';
import { BaseModel } from 'src/app/domain/models/base/base-model';
import { BaseViewModel } from 'src/app/site/base/base-view-model';

import { Decimal, Id } from '../../domain/definitions/key-types';

Expand Down Expand Up @@ -521,3 +523,21 @@ export function findIndexInSortedArray<T>(array: T[], toFind: T, compareFn: (a:
}
return -1;
}

export function viewModelListEqual<M extends BaseModel>(l1: BaseViewModel<M>[], l2: BaseViewModel<M>[]): boolean {
if (l1?.length !== l2?.length) {
return false;
}

for (let i = 0; i < l1.length; i++) {
if (l1[i].viewModelUpdateTimestamp !== l2[i].viewModelUpdateTimestamp) {
return false;
}

if (l1[i].id !== l2[i].id) {
return false;
}
}

return true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TranslateService } from '@ngx-translate/core';
import { combineLatest, distinctUntilChanged, Subscription } from 'rxjs';
import { Id } from 'src/app/domain/definitions/key-types';
import { Permission } from 'src/app/domain/definitions/permission';
import { viewModelListEqual } from 'src/app/infrastructure/utils';
import { VoteControllerService } from 'src/app/site/pages/meetings/modules/poll/services/vote-controller.service';
import { VotingService } from 'src/app/site/pages/meetings/modules/poll/services/voting.service';
import { ViewPoll } from 'src/app/site/pages/meetings/pages/polls';
Expand Down Expand Up @@ -43,14 +44,7 @@ export class VotingBannerService {
) {
combineLatest([
this.activeMeeting.meetingIdObservable.pipe(distinctUntilChanged()),
this.activePolls.activePollsObservable.pipe(
distinctUntilChanged((previous, current) => {
const prevStarted = previous.map(p => p.id);
const currStarted = current.map(p => p.id);

return prevStarted.length === currStarted.length && currStarted.equals(prevStarted);
})
),
this.activePolls.activePollsObservable.pipe(distinctUntilChanged(viewModelListEqual)),
this.meetingSettingsService.get(`users_enable_vote_delegations`).pipe(distinctUntilChanged()),
this.meetingSettingsService.get(`users_forbid_delegator_to_vote`).pipe(distinctUntilChanged()),
this.operator.userObservable.pipe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { BaseMeetingControllerService } from 'src/app/site/pages/meetings/base/b
import { MeetingControllerServiceCollectorService } from 'src/app/site/pages/meetings/services/meeting-controller-service-collector.service';

import { ViewPoll } from '../../../../pages/polls';
import { viewModelListEqual } from 'src/app/infrastructure/utils';

@Injectable({ providedIn: `root` })
export class PollControllerService extends BaseMeetingControllerService<ViewPoll, Poll> {
Expand All @@ -22,12 +23,7 @@ export class PollControllerService extends BaseMeetingControllerService<ViewPoll

this.getViewModelListObservableOfStarted()
.pipe(
distinctUntilChanged((previous, current) => {
const prevStarted = previous.map(p => p.id);
const currStarted = current.map(p => p.id);

return prevStarted.length === currStarted.length && currStarted.equals(prevStarted);
}),
distinctUntilChanged(viewModelListEqual),
map(value => value.map(p => p.id))
)
.subscribe(startedPolls => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ViewMotion } from '../../../../view-models';
import { ViewMotionChangeRecommendation, ViewUnifiedChange } from '../../view-models';
import { LineNumberingService } from '../line-numbering.service';
import { MotionDiffService } from '../motion-diff.service';
import { viewModelListEqual } from 'src/app/infrastructure/utils';

@Injectable({
providedIn: `root`
Expand Down Expand Up @@ -48,12 +49,7 @@ export class MotionChangeRecommendationControllerService extends BaseMeetingCont
public getChangeRecosOfMotionObservable(motionId: Id): Observable<ViewMotionChangeRecommendation[]> {
return this.getViewModelListObservable().pipe(
map((recos: ViewMotionChangeRecommendation[]) => recos.filter(reco => reco.motion_id === motionId)),
distinctUntilChanged(
(prev, curr) =>
prev?.length === curr?.length &&
Math.max(...prev.map(e => e.viewModelUpdateTimestamp)) ===
Math.max(...curr.map(e => e.viewModelUpdateTimestamp))
)
distinctUntilChanged(viewModelListEqual)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { MotionRepositoryService } from 'src/app/gateways/repositories/motions';

import { ViewMotion } from '../../../view-models';
import { MotionControllerService } from '../motion-controller.service';
import { viewModelListEqual } from 'src/app/infrastructure/utils';

@Injectable({
providedIn: `root`
Expand Down Expand Up @@ -43,13 +44,7 @@ export class AmendmentControllerService {
public getViewModelListObservableFor(motion: Identifiable): Observable<ViewMotion[]> {
return this.getViewModelListObservable().pipe(
map(_motions => _motions.filter(_motion => _motion.lead_motion_id === motion.id)),
distinctUntilChanged(
(prev, curr) =>
prev?.length === curr?.length &&
Math.max(...prev.map(e => e.viewModelUpdateTimestamp)) ===
Math.max(...curr.map(e => e.viewModelUpdateTimestamp)) &&
prev?.map(e => e.state_id).equals(curr?.map(e => e.state_id))
)
distinctUntilChanged(viewModelListEqual)
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Injectable } from '@angular/core';
import { BehaviorSubject, distinctUntilChanged, map, Observable, Subscription } from 'rxjs';
import { Id } from 'src/app/domain/definitions/key-types';
import { viewModelListEqual } from 'src/app/infrastructure/utils';
import { ModelRequestService } from 'src/app/site/services/model-request.service';

import { PollControllerService } from '../modules/poll/services/poll-controller.service';
Expand Down Expand Up @@ -44,12 +45,7 @@ export class ActivePollsService {
.getViewModelListObservable()
.pipe(
map(polls => polls.filter(p => p.isStarted)),
distinctUntilChanged((previous, current) => {
const prevStarted = previous.map(p => p.id);
const currStarted = current.map(p => p.id);

return prevStarted.length === currStarted.length && currStarted.equals(prevStarted);
})
distinctUntilChanged(viewModelListEqual)
)
.subscribe(polls => {
this.pollIds = polls.map(poll => poll.id);
Expand Down

0 comments on commit 4c640e6

Please sign in to comment.