binaryminds / react-native-sse

Event Source implementation for React Native. Server-Sent Events (SSE) for iOS and Android 🚀
https://www.npmjs.com/package/react-native-sse
MIT License
202 stars 30 forks source link

Fix some events getting dispatched twice when double newline is missing at the end #44

Closed EmilJunker closed 7 months ago

EmilJunker commented 7 months ago

As surfaced in #42, react-native-sse currently dispatches the same event twice when there is no extra newline at the end of the response text. For example, here is what currently happens for a given response text:

response text #​1: data: MESSAGE ONE\n\ndata: MESSAGE TWO\n <-- note that there is only one \n here results in parsed lines [ "data: MESSAGE ONE", "", "data: MESSAGE TWO", "" ] so the events "MESSAGE ONE" and "MESSAGE TWO" both get dispatched, and lastIndexProcessed is set to the index of the double newline \n\n which is 19. However, the actual length of the response text is 37.

response text #​2: data: MESSAGE ONE\n\ndata: MESSAGE TWO\n\ndata: MESSAGE THREE\n\ndata: MESSAGE FOUR\n starting at index 19, this gives parsed lines [ "data: MESSAGE TWO", "", "data: MESSAGE THREE", "", "data: MESSAGE FOUR", "" ] so the event "MESSAGE TWO" gets dispatched again, "MESSAGE THREE" and "MESSAGE FOUR" also get dispatched, and lastIndexProcessed is set to 59. However, the actual length of the response text is 78.

And it continues like this... always dispatching the last event from the previous chunk twice.

This PR fixes that problem. My solution is inspired by #39 by @GidoHakvoort, but it avoids some shortcomings and problems of that implementation.

With these changes, react-native-sse will now always only look at the chunk of the response from the lastIndexProcessed till the last double newline found in the response text. Afterwards it sets lastIndexProcessed to the index of that double newline, so the next iteration starts there, and so on.

Fixes #42. Closes #39.