Skip to content

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

Merged
wojciechkrol merged 1 commit intobinaryminds:masterfrom
EmilJunker:fix-double-newline-parsing
Feb 23, 2024
Merged

Fix some events getting dispatched twice when double newline is missing at the end#44
wojciechkrol merged 1 commit intobinaryminds:masterfrom
EmilJunker:fix-double-newline-parsing

Conversation

@EmilJunker
Copy link
Contributor

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.

@wojciechkrol wojciechkrol merged commit 6cda129 into binaryminds:master Feb 23, 2024
@EmilJunker EmilJunker deleted the fix-double-newline-parsing branch February 23, 2024 11:27
@taylorgoolsby taylorgoolsby mentioned this pull request Mar 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chatgpt api (openai-node v4.26.0) stream issue with gpt-4 models

2 participants