@@ -108,6 +108,7 @@ const ChatScreen = ({ navigation, route }: Props) => {
108108 const [ messages , setMessages ] = useState < Message [ ] > ( [ ] )
109109 const [ loggedIn , setLoggedIn ] = useState ( false )
110110 const [ message , setMessage ] = useState ( '' )
111+ const messagesBufferRef = useRef < Message [ ] > ( [ ] )
111112 const healthRef = useRef < number | null > ( null )
112113 const statusRef = useRef < Status > ( connection ? 'CONNECTING' : 'OPENING' )
113114 const idRef = useRef ( 0 )
@@ -117,16 +118,27 @@ const ChatScreen = ({ navigation, route }: Props) => {
117118 ? 256
118119 : 100
119120 const addMessage = ( text : MinecraftChat ) =>
120- setMessages ( m => {
121- const trunc = m . length > 500 ? m . slice ( 0 , 499 ) : m
122- return [ { key : idRef . current ++ , text } ] . concat ( trunc )
123- } )
121+ messagesBufferRef . current . unshift ( { key : idRef . current ++ , text } )
124122 const closeChatScreen = ( ) => {
125123 if ( navigation . canGoBack ( ) && statusRef . current !== 'CLOSED' ) {
126124 navigation . goBack ( )
127125 }
128126 }
129127
128+ // Chat message buffering function.
129+ useEffect ( ( ) => {
130+ const interval = setInterval ( ( ) => {
131+ if ( messagesBufferRef . current . length ) {
132+ setMessages ( m => {
133+ const concat = messagesBufferRef . current . concat ( m )
134+ messagesBufferRef . current = [ ]
135+ return concat . length > 500 ? concat . slice ( 0 , 499 ) : concat
136+ } )
137+ }
138+ } , 50 )
139+ return ( ) => clearInterval ( interval )
140+ } , [ ] )
141+
130142 // Screen cleanup function.
131143 useEffect ( ( ) =>
132144 navigation . addListener ( 'beforeRemove' , ( ) => {
0 commit comments