English
Checkpoint
What must work
Run in Source/Chatex.Web:
powershell
npm run buildpowershell
npm test -- --watch=falsepowershell
npm run lintpowershell
npm run format:checkCompare the finished files with the reference:
powershell
git diff --no-index Source/Chatex.Web/src/app/features/conversation/message-list.ts D:\Work\Chatex-Reference\Source\Chatex.Web\src\app\features\conversation\message-list.tspowershell
git diff --no-index Source/Chatex.Web/src/app/features/conversation/attachments.ts D:\Work\Chatex-Reference\Source\Chatex.Web\src\app\features\conversation\attachments.tsBoth print nothing, and so does message-list.spec.ts. The store, the composer, their specs and the page get their realtime parts in topic 20.
Commit:
powershell
git add .powershell
git commit -m "Add the message store, the message list, the composer and the conversation page"Common mistakes
Table 19.1. Common mistakes in the conversation
| Symptom | Cause | Fix |
|---|---|---|
NG0201: No provider found for `_MessagesStore`. when opening a chat | providers: [MessagesStore] is missing in the page's @Component (the store is not provided in the root) | provide it in the page |
NG0950: Input "chatId" is required but no value is available yet. | the router does not bind route parameters to inputs (withComponentInputBinding() missing), or a test did not call setInput | add the feature in app.config.ts; call fixture.componentRef.setInput in tests |
| messages of the previous chat appear in the new one after a quick switch | a late response was applied without checking generation | compare the generation before changing the state |
| the list jumps down when older messages load | the scroll position is not restored after prepending, or the browser's own anchoring interferes | restore scrollTop in afterNextRender; keep [overflow-anchor:none] |
| a sent message appears twice | the pending copy is not removed, or messages are appended instead of merged by id | remove the pending message and merge the server's copy |
| the box keeps the text and the files of the previous chat | the chatId effect that resets the composer is missing; Angular reuses the component between chats | reset in an effect on chatId() |
| the unread badge is cleared although the tab was in the background | markRead ignores document.visibilityState | check visibility, and re-check on visibilitychange |
Self-check questions
- Why is
MessagesStoreprovided by the conversation page and not in the root? What is its lifetime? - What does "optimistic sending" mean in Chatex, and why are edits and deletions not optimistic?
- How does the store keep exactly one copy of a message that arrives both as an HTTP response and as an event?
- What problem does the
generationcounter solve? Describe the race it prevents. - Why does
markReadrememberlastMarkedRead? Why is a deleted message never used as the read position? - Why does
buildRowsexist, and why is it exported? - What is a discriminated union? How does
Rowuse it in the template? - How does the message list keep the view still when older messages are prepended?
- Why are attachments uploaded as soon as they are picked? What does the composer have to track because of that?
- Why does the composer need an effect on
chatId, although a new chat opens a "new" page?
Exercises
- Upload progress. Add
withXhr()toprovideHttpClientinapp.config.tsand upload a large file (a few megabytes) with the network throttled to "Slow 4G" in the developer tools. Compare the progress bar with the default backend. What does the change cost (see Angular's documentation ofwithXhr)? - Draft per chat. Keep the unsent text of each chat in a
Mapin the composer, so that switching chats and back restores the draft. Write a component test for it. - Day separators. Add a test to
message-list.spec.tsthat checks the label of the separator for a message of today ('Today'). Why is a test for "Yesterday" around midnight tricky, and how would you makedayLabeltestable at any time of day?