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.
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, andlastIndexProcessed
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, andlastIndexProcessed
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 setslastIndexProcessed
to the index of that double newline, so the next iteration starts there, and so on.Fixes #42. Closes #39.