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
174 stars 27 forks source link

Fix to only handle events once an empty line is present. #39

Closed GidoHakvoort closed 4 months ago

GidoHakvoort commented 6 months ago

Fix to only handle events once an empty line is present.

When events are sent as multipart messages onreadystatechange will be called multiple times. Since response.substr(this.lastIndexProcessed).split('\n') also return an empty string with only 1 newline, dispatch will also be called multiple time (e.g. "event: add\n data: 4\n".split('\n') will give [ "event: add", " data: 4", "" ]). This fix will check if indeed (a new) empty line is present before processing the event.