From 8dc1ee0adc2f14aa2f37c8514613c6835441b9f0 Mon Sep 17 00:00:00 2001 From: Tejas Kumar Date: Thu, 19 Jul 2018 16:23:19 +0200 Subject: [PATCH] Add error handling to Poll, do not rerender polled results unless content changes --- package.json | 5 +++-- src/Poll.tsx | 30 +++++++++++++++++++++++++----- yarn.lock | 10 +++++++--- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 5f86b291..9ef9b1c7 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/Poll.tsx b/src/Poll.tsx index 5407adf4..0180b046 100644 --- a/src/Poll.tsx +++ b/src/Poll.tsx @@ -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"; @@ -147,6 +149,16 @@ class ContextlessPoll extends React.Component, Readonly { + if (response.status === 304) { + return false; + } + if (equal(this.state.data, nextData)) { + return false; + } + return true; + }; + /** * This thing does the actual poll. */ @@ -173,11 +185,19 @@ class ContextlessPoll extends React.Component, Readonly ({ - 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)); diff --git a/yarn.lock b/yarn.lock index de151228..052253e5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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"