I've a ReactJS application made up of 3 simple elements: 2 numbers and an input text. This is a test page of course.
The 2 numbers refers to the number of elements that are present in 2 separate Firestore collections, and the input text doesn't do anything.
The goal of my experiment is to understand why if I write a huge amount of docs in a short time in the collections (I'm using k6 in order to) and just print those two numbers by listening to Firestore snapshots, my page completely freezes until all the messages have arrived.
Here is my code
const App = () => {
const [col1, setCol1] = useState(0);
const [col2, setCol2] = useState(0);
const [value, setValue] = useState('');
useEffect(() => {
console.debug('*** EFFECT');
db
.collection('abcd/efgh/collection1')
.orderBy('timestamp')
.startAt(new Date())
.limitToLast(1)
.onSnapshot(({ docs }) => {
setCol1((e) => e + 1);
});
db
.collection('qwer/tyui/collection2')
.orderBy('timestamp')
.onSnapshot(({ docs }) => {
console.debug('***', docs.length);
setCol2(docs.length);
});
}, []);
return (
<>
{exist}
<br />
{messages}
<br />
<Input onChange={({ target: { value } }) => setValue(value)} value={value} />
</>
);
};
Of course the final target is to understand how to not cause the freeze.
From this I see that spawning a separate thread will help, so the equivalent in JS desktop are WebWorkers.
Could that be a right idea?
Do you have any other suggestion?