-
-
Notifications
You must be signed in to change notification settings - Fork 127
Add fixture: trimming a capped chat from the front breaks maintainScrollAtEnd #462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
frankberliner
wants to merge
1
commit into
LegendApp:main
Choose a base branch
from
frankberliner:add-chat-trim-fixture
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import { useEffect, useState } from "react"; | ||
| import { StyleSheet, Text, View } from "react-native"; | ||
|
|
||
| import { LegendList } from "@legendapp/list/react-native"; | ||
|
|
||
| // Reproduces a freeze when capping a chat list by trimming the OLDEST messages | ||
| // from the FRONT of the data array while the list is pinned to the bottom. | ||
| // | ||
| // High-throughput chats (e.g. a livestream chat) often cap the list to a fixed | ||
| // window to bound memory over long sessions. Until the cap (MAX) is reached this | ||
| // is append-only and maintainScrollAtEnd follows the newest message perfectly. | ||
| // Once trimming starts, the live flow breaks: | ||
| // - maintainVisibleContentPosition data:false -> the viewport visibly JUMPS on | ||
| // every trim (the removed top content is not compensated) | ||
| // - data:true (used below) -> the removal is compensated so | ||
| // there is no jump, but maintainScrollAtEnd stops following: after the anchor | ||
| // adjustment isWithinMaintainScrollAtEndThreshold flips to false, so the | ||
| // newest messages stay just below the fold = "freeze". | ||
| // | ||
| // What to watch: "latest #N" in the header should always equal the number on the | ||
| // message at the very bottom of the list. They match while append-only; once | ||
| // trimming kicks in (count reaches MAX) they DIVERGE β the list no longer stays | ||
| // pinned to the newest message, and a real app would have to scroll manually. | ||
|
|
||
| type Message = { id: string; text: string }; | ||
|
|
||
| const MAX = 40; // cap; intentionally small so trimming starts within a few seconds | ||
|
|
||
| const LINES = [ | ||
| "short message", | ||
| "a slightly longer chat message here", | ||
| "an even longer message that wraps onto multiple lines so row heights vary", | ||
| ]; | ||
|
|
||
| let idCounter = 0; | ||
| const makeMessage = (): Message => { | ||
| idCounter += 1; | ||
| return { id: String(idCounter), text: `#${idCounter} ${LINES[idCounter % LINES.length]}` }; | ||
| }; | ||
|
|
||
| const ChatTrim = () => { | ||
| const [messages, setMessages] = useState<Message[]>(() => Array.from({ length: 10 }, makeMessage)); | ||
|
|
||
| useEffect(() => { | ||
| const interval = setInterval(() => { | ||
| setMessages((prev) => { | ||
| // Append a small batch to simulate throughput / batched updates. | ||
| const batchSize = 1 + (idCounter % 3); // 1..3 per tick | ||
| const next = [...prev, ...Array.from({ length: batchSize }, makeMessage)]; | ||
| // Cap memory by trimming the OLDEST messages from the FRONT: | ||
| return next.length > MAX ? next.slice(next.length - MAX) : next; | ||
| }); | ||
| }, 250); | ||
| return () => clearInterval(interval); | ||
| }, []); | ||
|
|
||
| return ( | ||
| <View style={styles.container}> | ||
| <Text style={styles.header}> | ||
| count: {messages.length} / cap {MAX} β latest is #{idCounter} | ||
| </Text> | ||
| <LegendList | ||
| alignItemsAtEnd | ||
| contentContainerStyle={styles.content} | ||
| data={messages} | ||
| estimatedItemSize={48} | ||
| keyExtractor={(item) => item.id} | ||
| maintainScrollAtEnd | ||
| maintainScrollAtEndThreshold={0.1} | ||
| maintainVisibleContentPosition={{ data: true }} | ||
| recycleItems | ||
| renderItem={({ item }) => ( | ||
| <View style={styles.bubble}> | ||
| <Text style={styles.text}>{item.text}</Text> | ||
| </View> | ||
| )} | ||
| style={styles.list} | ||
| /> | ||
| </View> | ||
| ); | ||
| }; | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| bubble: { | ||
| backgroundColor: "#222", | ||
| borderRadius: 8, | ||
| marginVertical: 3, | ||
| paddingHorizontal: 10, | ||
| paddingVertical: 6, | ||
| }, | ||
| container: { | ||
| backgroundColor: "#111", | ||
| flex: 1, | ||
| }, | ||
| content: { | ||
| paddingHorizontal: 10, | ||
| }, | ||
| header: { | ||
| color: "#0f0", | ||
| padding: 8, | ||
| }, | ||
| list: { | ||
| flex: 1, | ||
| }, | ||
| text: { | ||
| color: "#fff", | ||
| }, | ||
| }); | ||
|
|
||
| export default ChatTrim; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When this fixture is run in a React dev tree that replays state updater functions (for example StrictMode/concurrent development), this updater can be invoked more than once for a single committed tick, but
makeMessagemutates the module-levelidCounter. That can advancelatest #Nand even change the batch contents for discarded updater calls, so the header can diverge from the bottom row for reasons unrelated to themaintainScrollAtEndregression the fixture is meant to demonstrate. Generate the next messages from a ref/local counter in an effect before callingsetMessages, or otherwise keep the updater pure.Useful? React with πΒ / π.