Skip to content

Commit

Permalink
Updated examples in readme to fix typos, added more comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
vishwam committed Nov 27, 2020
1 parent 8d65b5d commit 6ff6dd1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 1.0.2
### Changed
- Updated examples in readme to fix typos, added more comments.
- Changed `if` statements in parse.ts to test for specific values instead of truthy/falsy values.

## 1.0.1
### Changed
- Changed the default onOpen validator to allow charset and boundary directives in the content-type
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This library provides an alternate interface for consuming server-sent events, b
* You have access to the response object if you want to do some custom validation/processing before parsing the event source. This is useful in case you have API gateways (like nginx) in front of your application server: if the gateway returns an error, you might want to handle it correctly.
* If the connection gets cut or an error occurs, you have full control over the retry strategy.

In addition, this library also plugs into the browser's [Page Visibility API](https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API) so the connection closes if the document is hidden (e.g., the user minimizes the window), and automatically retries with the last event ID when it becomes visible again. This reduces the load on your server by not having open connections unncessarily (but you can opt out of this behavior if you want.)
In addition, this library also plugs into the browser's [Page Visibility API](https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API) so the connection closes if the document is hidden (e.g., the user minimizes the window), and automatically retries with the last event ID when it becomes visible again. This reduces the load on your server by not having open connections unnecessarily (but you can opt out of this behavior if you want.)

# Install
```sh
Expand All @@ -29,6 +29,8 @@ sse.onmessage = (ev) => {
};

// AFTER:
import { fetchEventSource } from '@microsoft/fetch-event-source';

await fetchEventSource('/api/sse', {
onmessage(ev) {
console.log(ev.data);
Expand Down Expand Up @@ -68,6 +70,8 @@ fetchEventSource('/api/sse', {
}
},
onmessage(msg) {
// if the server emits an error message, throw an exception
// so it gets handled by the onerror callback below:
if (msg.event === 'FatalError') {
throw new FatalError(msg.data);
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft/fetch-event-source",
"version": "1.0.1",
"version": "1.0.2",
"description": "A better API for making Event Source requests, with all the features of fetch()",
"homepage": "https://github.com/Azure/fetch-event-source#readme",
"bugs": {
Expand Down
12 changes: 6 additions & 6 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ export async function* getLines(iter: AsyncIterableIterator<Uint8Array>) {
let discardTrailingNewline = false;

for await (const arr of iter) {
if (buffer) {
// we're still parsing the old line. Append the new bytes into buffer:
buffer = concat(buffer, arr);
} else {
if (buffer === undefined) {
buffer = arr;
position = 0;
fieldLength = -1;
} else {
// we're still parsing the old line. Append the new bytes into buffer:
buffer = concat(buffer, arr);
}

const bufLength = buffer.length;
Expand Down Expand Up @@ -103,7 +103,7 @@ export async function* getLines(iter: AsyncIterableIterator<Uint8Array>) {

if (lineStart === bufLength) {
buffer = undefined; // we've finished reading it
} else if (lineStart) {
} else if (lineStart !== 0) {
// Create a new view into buffer beginning at lineStart so we don't
// need to copy over the previous lines when we get the new arr:
buffer = buffer.subarray(lineStart);
Expand All @@ -117,7 +117,7 @@ export async function* getMessages(iter: AsyncIterableIterator<{ line: Uint8Arra
let message: EventSourceMessage = {};
const decoder = new TextDecoder();
for await (const { line, fieldLength } of iter) {
if (!line.length) {
if (line.length === 0) {
// empty line denotes end of message. Yield our current message if it's not empty:
for (const _ in message) {
yield message;
Expand Down

0 comments on commit 6ff6dd1

Please sign in to comment.