Skip to content

Commit

Permalink
Trigger computation of top labels only on file changes #3339
Browse files Browse the repository at this point in the history
  • Loading branch information
fritschldwg committed Jan 9, 2024
1 parent b36f96f commit a5bb49b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@ describe("updateVisibleTopLabelsEffect", () => {
selectors: [
{
selector: visibleFileStatesSelector,
value: {}
value: []
},
{
selector: codeMapNodesSelector,
value: {}
value: [
{
name: "sample1.ts"
},
{
name: "sample2.ts"
}
]
}
]
}),
Expand All @@ -44,11 +51,9 @@ describe("updateVisibleTopLabelsEffect", () => {
})

it("should set amount of top labels to current app-settings when visible file-states are unchanged", async () => {
const visibleFileStates = {}
const codeMapNodes = {}
const visibleFileStates = []

store.overrideSelector(visibleFileStatesSelector, visibleFileStates as ReturnType<typeof visibleFileStatesSelector>)
store.overrideSelector(codeMapNodesSelector, codeMapNodes as ReturnType<typeof codeMapNodesSelector>)
store.refreshState()

expect(await getLastAction(store)).toEqual(setAmountOfTopLabels({ value: 5 }))
Expand All @@ -64,17 +69,8 @@ describe("updateVisibleTopLabelsEffect", () => {
}
}
]
const codeMapNodes = [
{
name: "sample1.ts"
},
{
name: "sample2.ts"
}
]

store.overrideSelector(visibleFileStatesSelector, visibleFileStates as ReturnType<typeof visibleFileStatesSelector>)
store.overrideSelector(codeMapNodesSelector, codeMapNodes as ReturnType<typeof codeMapNodesSelector>)
store.refreshState()

expect(await getLastAction(store)).toEqual(setAmountOfTopLabels({ value: 1 }))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from "@angular/core"
import { createEffect } from "@ngrx/effects"

import { combineLatest, map, pairwise } from "rxjs"
import { map, pairwise, withLatestFrom } from "rxjs"
import { visibleFileStatesSelector } from "../../selectors/visibleFileStates.selector"
import { codeMapNodesSelector } from "../../selectors/accumulatedData/codeMapNodes.selector"
import { setAmountOfTopLabels } from "../../store/appSettings/amountOfTopLabels/amountOfTopLabels.actions"
Expand All @@ -14,16 +14,14 @@ export class UpdateVisibleTopLabelsEffect {
constructor(private store: Store<CcState>, private state: State<CcState>) {}

updateVisibleTopLabels$ = createEffect(() =>
combineLatest([
this.store.select(visibleFileStatesSelector).pipe(pairwise()),
this.store.select(codeMapNodesSelector).pipe(pairwise())
]).pipe(
map(([[previousVisibleFileStates, currentVisibleFileStates], [, currentCodeMapNodes]]) => {
const state = this.state.getValue()
const isVisibleFileStatesUnchanged = JSON.stringify(previousVisibleFileStates) === JSON.stringify(currentVisibleFileStates)
const amountOfTopLabels = isVisibleFileStatesUnchanged
? state.appSettings.amountOfTopLabels
: getNumberOfTopLabels(currentCodeMapNodes)
this.store.select(visibleFileStatesSelector).pipe(
pairwise(),
withLatestFrom(this.store.select(codeMapNodesSelector)),
map(([[previousVisibleFileStates, currentVisibleFileStates], codeMapNodes]) => {
const isUnchanged = JSON.stringify(previousVisibleFileStates) === JSON.stringify(currentVisibleFileStates)
const amountOfTopLabels = isUnchanged
? this.state.getValue().appSettings.amountOfTopLabels
: getNumberOfTopLabels(codeMapNodes)

return setAmountOfTopLabels({ value: amountOfTopLabels })
})
Expand Down

0 comments on commit a5bb49b

Please sign in to comment.