Skip to content

Teach DI with saved reading progress - #36

Merged
gpbsumido merged 5 commits into
ps/ng-04-signalsfrom
ps/ng-05-di
Nov 21, 2026
Merged

gpbsumido merged 5 commits into
ps/ng-04-signalsfrom
ps/ng-05-di

Conversation

@gpbsumido

@gpbsumido gpbsumido commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Fifth lesson in my Angular guide: dependency injection. The feature slice is saved reading progress.

What shipped

Saved reading progress

  • Every lesson ends with a "Mark lesson complete" toggle. It's a real aria-pressed button, and its label changes as well as its colour.
  • The series nav reads "Part N of M · K of M complete".
  • The Thoughts list badges completed lessons.
  • ReadingProgressService is a root store with a private writable signal and a read-only view.

BROWSER_STORAGE injection token. It's localStorage in the browser and null during prerendering, decided once in its factory. SettingsService now injects it instead of checking isPlatformBrowser and touching the global in two places. That makes the token the only code in src/app that refers to the localStorage global.

Corrupt saved progress throws. The error names the bad value, rather than silently wiping progress.

Lesson 05: DI (/thoughts/angular-05-dependency-injection)

  • Covers the material at three levels (simple, intermediate, expert), then pitfalls and interview Q&A.
  • Simple: inject() and root singletons.
  • Intermediate: the four provider shapes, InjectionToken, and swapping in specs.
  • Expert: the resolution order, modifiers, providers vs viewProviders, route and multi providers, and per-request roots on the server.
  • The live demo puts buttons under one shared provider and under one provider each.
  • The demo's instance numbers come from a provider on the page rather than a module-level counter, which is the SSR leak the lesson warns about.

Version and changelog. 1.4.4, with a matching changelog entry.

Commits

  1. Add failing specs for a storage token, reading progress and lesson 05
  2. Inject browser storage through a token
  3. Save reading progress and show it in the reader and list
  4. Write lesson 05 with a live injector-tree demo
  5. Bump to 1.4.4 with the lesson 05 changelog entry

Verification

  • RED first. The RED commit fails to compile, because the token, the service and the lesson don't exist yet.
  • Settings specs against the old service. Run against the old SettingsService, its two new specs fail for the right reason:
    • expected 'dark' to be 'light': it ignored the injected storage.
    • The second spec found a value written to the real localStorage where null storage was provided.
  • Tests and typechecks. ng test: 51 files, 375 tests, exit 0. Lint and both tsc --noEmit configs are clean.
  • Production build. Exit 0, no budget warnings. Initial bundle 384.14 → 384.98 kB, and the lesson is a 14.3 kB lazy chunk.
  • Prerendered HTML. Lesson 05 contains "Part 5 of 5 · 0 of 5 complete", "Mark lesson complete" and the demo at "A: 0".

TO-DOs for this PR

  • Before/after screenshots of the lesson footer (toggle and progress count), a badged card in the Thoughts list, and the lesson 05 demo, light and dark. Chromium couldn't launch in the environment I built this in.

@gpbsumido
gpbsumido marked this pull request as ready for review November 21, 2026 15:05
@gpbsumido
gpbsumido added this pull request to stack #46 November 21, 2026 17:00
@gpbsumido
gpbsumido merged commit 530c68f into develop Nov 21, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant