diff --git a/src/app/app.spec.ts b/src/app/app.spec.ts index 85c63b9..7ac5486 100644 --- a/src/app/app.spec.ts +++ b/src/app/app.spec.ts @@ -26,6 +26,24 @@ describe('App', () => { expect(app).toBeTruthy(); }); + describe('related thoughts', () => { + // The template calls this for every open window on every change detection + // pass. A new array each time would be a new input value each time. + it('hands a window the same array on every call', () => { + const app = TestBed.createComponent(App).componentInstance; + + expect(app.getRelatedThoughts('projects')).toBe(app.getRelatedThoughts('projects')); + expect(app.getRelatedThoughts('no-such-app')).toBe(app.getRelatedThoughts('no-such-app')); + }); + + it('still lists the thoughts tied to an app, and none for an unknown one', () => { + const app = TestBed.createComponent(App).componentInstance; + + expect(app.getRelatedThoughts('projects').length).toBeGreaterThan(0); + expect(app.getRelatedThoughts('no-such-app')).toEqual([]); + }); + }); + describe('thoughts reader overlay', () => { let fixture: ComponentFixture; let router: Router; diff --git a/src/app/app.ts b/src/app/app.ts index d824d79..7180add 100644 --- a/src/app/app.ts +++ b/src/app/app.ts @@ -17,6 +17,17 @@ import { THOUGHTS } from './thoughts-content/thoughts-data'; import { Window } from './window-manager/window'; import { WindowManagerService } from './window-manager/window-manager.service'; +// THOUGHTS never changes at runtime, so group it once. The template asks for +// these on every change detection pass, and a fresh array each time would be +// a new input value for every open window. +const RELATED_THOUGHT_SLUGS_BY_APP_ID = new Map(); +for (const thought of THOUGHTS) { + if (!thought.relatedApp) continue; + const slugs = RELATED_THOUGHT_SLUGS_BY_APP_ID.get(thought.relatedApp) ?? []; + RELATED_THOUGHT_SLUGS_BY_APP_ID.set(thought.relatedApp, [...slugs, thought.slug]); +} +const NO_RELATED_THOUGHTS: string[] = []; + @Component({ selector: 'app-root', imports: [ @@ -153,7 +164,7 @@ export class App { } getRelatedThoughts(appId: string): string[] { - return THOUGHTS.filter((t) => t.relatedApp === appId).map((t) => t.slug); + return RELATED_THOUGHT_SLUGS_BY_APP_ID.get(appId) ?? NO_RELATED_THOUGHTS; } onThoughtRequested(slug: string): void { diff --git a/src/app/apps/projects-app.spec.ts b/src/app/apps/projects-app.spec.ts index 71df909..9b71d29 100644 --- a/src/app/apps/projects-app.spec.ts +++ b/src/app/apps/projects-app.spec.ts @@ -142,20 +142,4 @@ describe('ProjectsService', () => { expect(service.repos.value()).toEqual(MOCK_REPOS); }); - - it('should react to search signal changes by re-fetching', async () => { - const req1 = httpTesting.expectOne((r) => r.url.includes('github.com')); - req1.flush(MOCK_REPOS); - await fixture.whenStable(); - - service.search.set('angular'); - fixture.detectChanges(); - - const req2 = httpTesting.expectOne((r) => r.url.includes('github.com')); - expect(req2.request.url).toContain('angular'); - req2.flush([MOCK_REPOS[0]]); - await fixture.whenStable(); - - expect(service.repos.value()).toEqual([MOCK_REPOS[0]]); - }); }); diff --git a/src/app/apps/projects.service.ts b/src/app/apps/projects.service.ts index 0f01ca0..31f5483 100644 --- a/src/app/apps/projects.service.ts +++ b/src/app/apps/projects.service.ts @@ -1,16 +1,10 @@ -import { Injectable, signal } from '@angular/core'; +import { Injectable } from '@angular/core'; import { httpResource } from '@angular/common/http'; import { GitHubRepo } from './projects-app'; @Injectable({ providedIn: 'root' }) export class ProjectsService { - readonly search = signal(''); - - readonly repos = httpResource(() => { - const query = this.search(); - if (query) { - return `https://api.github.com/users/gpbsumido/repos?q=${encodeURIComponent(query)}`; - } - return 'https://api.github.com/users/gpbsumido/repos?sort=updated&per_page=30'; - }); + readonly repos = httpResource( + () => 'https://api.github.com/users/gpbsumido/repos?sort=updated&per_page=30', + ); }