Skip to content
This repository has been archived by the owner on Nov 11, 2023. It is now read-only.

Commit

Permalink
Add error handling to Poll, do not rerender polled results unless con…
Browse files Browse the repository at this point in the history
…tent changes
  • Loading branch information
Tejas Kumar committed Jul 19, 2018
1 parent 7e255dd commit 8dc1ee0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@
"tslint": "^5.10.0",
"tslint-config-prettier": "^1.13.0",
"tslint-plugin-blank-line": "^0.0.8",
"typescript": "^3.1.0-dev.20180717"
"typescript": "^2.9.2"
},
"dependencies": {
"react": "^16.4.1"
"react": "^16.4.1",
"react-fast-compare": "^2.0.1"
},
"peerDependencies": {
"react": "^16.4.1"
Expand Down
30 changes: 25 additions & 5 deletions src/Poll.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React from "react";
import equal from "react-fast-compare";

import { RestfulReactConsumer } from "./Context";
import { GetComponentProps, GetComponentState, Meta as GetComponentMeta } from "./Get";

Expand Down Expand Up @@ -147,6 +149,16 @@ class ContextlessPoll<T> extends React.Component<PollProps<T>, Readonly<PollStat

private keepPolling = !this.props.lazy;

private isModified = (response: Response, nextData: T) => {
if (response.status === 304) {
return false;
}
if (equal(this.state.data, nextData)) {
return false;
}
return true;
};

/**
* This thing does the actual poll.
*/
Expand All @@ -173,11 +185,19 @@ class ContextlessPoll<T> extends React.Component<PollProps<T>, Readonly<PollStat
const responseBody =
response.headers.get("content-type") === "application/json" ? await response.json() : await response.text();

this.setState(() => ({
loading: false,
lastResponse: response,
data: resolve ? resolve(responseBody) : responseBody,
}));
if (!response.ok) {
const error = `${response.status} ${response.statusText}`;
this.setState({ loading: false, lastResponse: response, data: responseBody, error });
throw new Error(`Failed to Poll: ${error}`);
}

if (this.isModified(response, responseBody)) {
this.setState(() => ({
loading: false,
lastResponse: response,
data: resolve ? resolve(responseBody) : responseBody,
}));
}

// Wait for interval to pass.
await new Promise(resolvePromise => setTimeout(resolvePromise, interval));
Expand Down
10 changes: 7 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3201,6 +3201,10 @@ rc@^1.2.7:
minimist "^1.2.0"
strip-json-comments "~2.0.1"

react-fast-compare@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/react-fast-compare/-/react-fast-compare-2.0.1.tgz#92c91ac4814aa8cfcfb04780ad1bfff2bbe3a3c6"

react@^16.4.1:
version "16.4.1"
resolved "https://registry.yarnpkg.com/react/-/react-16.4.1.tgz#de51ba5764b5dbcd1f9079037b862bd26b82fe32"
Expand Down Expand Up @@ -3980,9 +3984,9 @@ type-check@~0.3.2:
dependencies:
prelude-ls "~1.1.2"

typescript@^3.1.0-dev.20180717:
version "3.1.0-dev.20180717"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.1.0-dev.20180717.tgz#a623e4df35ee102641020b1fe9dd5258547be74a"
typescript@^2.9.2:
version "2.9.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.9.2.tgz#1cbf61d05d6b96269244eb6a3bce4bd914e0f00c"

ua-parser-js@^0.7.18:
version "0.7.18"
Expand Down

0 comments on commit 8dc1ee0

Please sign in to comment.