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

Commit

Permalink
Implement Contiamo Polling
Browse files Browse the repository at this point in the history
  • Loading branch information
Tejas Kumar committed Jul 23, 2018
1 parent 77c331b commit 223bb03
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"lint-staged": "^7.2.0",
"prettier": "^1.13.5",
"rollup": "^0.61.2",
"rollup-plugin-typescript2": "^0.15.0",
"rollup-plugin-typescript2": "^0.16.1",
"ts-jest": "^22.4.6",
"tslint": "^5.10.0",
"tslint-config-prettier": "^1.13.0",
Expand Down
49 changes: 36 additions & 13 deletions src/Poll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,19 @@ interface PollProps<T = {}> {
*/
children: (data: T | null, states: States<T>, actions: Actions, meta: Meta) => React.ReactNode;
/**
* How long do we wait between requests?
* How long do we wait between repeating a request?
* Value in milliseconds.
*
* Defaults to 1000.
*/
interval?: number;
/**
* How long should a request stay open?
* Value in seconds.
*
* Defaults to 60.
*/
wait?: number;
/**
* A stop condition for the poll that expects
* a boolean.
Expand Down Expand Up @@ -122,6 +130,10 @@ interface PollState<T> {
* Do we currently have an error?
*/
error?: GetComponentState<T>["error"];
/**
* Index of the last polled response.
*/
lastPollIndex?: string;
}

/**
Expand All @@ -132,18 +144,13 @@ class ContextlessPoll<T> extends React.Component<PollProps<T>, Readonly<PollStat
data: null,
loading: !this.props.lazy,
lastResponse: null,
polling: false,
polling: !this.props.lazy,
finished: false,
};

public static getDerivedStateFromProps(props: Pick<PollProps, "lazy">) {
return {
polling: !props.lazy,
};
}

public static defaultProps = {
interval: 1000,
wait: 60,
resolve: (data: any) => data,
};

Expand All @@ -159,6 +166,9 @@ class ContextlessPoll<T> extends React.Component<PollProps<T>, Readonly<PollStat
return true;
};

private getRequestOptions = () =>
typeof this.props.requestOptions === "function" ? this.props.requestOptions() : this.props.requestOptions || {};

/**
* This thing does the actual poll.
*/
Expand All @@ -175,11 +185,19 @@ class ContextlessPoll<T> extends React.Component<PollProps<T>, Readonly<PollStat
}

// If we should keep going,
const { base, path, requestOptions, resolve, interval } = this.props;
const request = new Request(
`${base}${path}`,
typeof requestOptions === "function" ? requestOptions() : requestOptions,
);
const { base, path, resolve, interval, wait } = this.props;
const { lastPollIndex } = this.state;
const requestOptions = this.getRequestOptions();

const request = new Request(`${base}${path}`, {
...requestOptions,

headers: {
Prefer: `wait=${wait};${lastPollIndex ? `index=${lastPollIndex}` : ""}`,

...requestOptions.headers,
},
});
const response = await fetch(request);

const responseBody =
Expand All @@ -191,11 +209,16 @@ class ContextlessPoll<T> extends React.Component<PollProps<T>, Readonly<PollStat
throw new Error(`Failed to Poll: ${error}`);
}

if (!(window as any).aaaa) {
(window as any).aaaa = response.headers;
}

if (this.isModified(response, responseBody)) {
this.setState(() => ({
loading: false,
lastResponse: response,
data: resolve ? resolve(responseBody) : responseBody,
lastPollIndex: response.headers.get("x-polling-index") || undefined,
}));
}

Expand Down
36 changes: 16 additions & 20 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1275,17 +1275,17 @@ fragment-cache@^0.2.1:
dependencies:
map-cache "^0.2.2"

fs-extra@6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-6.0.0.tgz#0f0afb290bb3deb87978da816fcd3c7797f3a817"
fs-extra@5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-5.0.0.tgz#414d0110cdd06705734d055652c5411260c31abd"
dependencies:
graceful-fs "^4.1.2"
jsonfile "^4.0.0"
universalify "^0.1.0"

fs-extra@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-5.0.0.tgz#414d0110cdd06705734d055652c5411260c31abd"
fs-extra@6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-6.0.0.tgz#0f0afb290bb3deb87978da816fcd3c7797f3a817"
dependencies:
graceful-fs "^4.1.2"
jsonfile "^4.0.0"
Expand Down Expand Up @@ -3391,7 +3391,7 @@ resolve@1.1.7:
version "1.1.7"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b"

resolve@^1.1.7, resolve@^1.3.2, resolve@^1.7.1:
resolve@1.8.1, resolve@^1.1.7, resolve@^1.3.2:
version "1.8.1"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.8.1.tgz#82f1ec19a423ac1fbd080b0bab06ba36e84a7a26"
dependencies:
Expand Down Expand Up @@ -3420,16 +3420,16 @@ rimraf@^2.5.4, rimraf@^2.6.1:
dependencies:
glob "^7.0.5"

rollup-plugin-typescript2@^0.15.0:
version "0.15.1"
resolved "https://registry.yarnpkg.com/rollup-plugin-typescript2/-/rollup-plugin-typescript2-0.15.1.tgz#7b35d0eaa6ad5a54a253ed158a565b99d8f15372"
rollup-plugin-typescript2@^0.16.1:
version "0.16.1"
resolved "https://registry.yarnpkg.com/rollup-plugin-typescript2/-/rollup-plugin-typescript2-0.16.1.tgz#72e1f8a2e450550eabdc3d474e735feae322b474"
dependencies:
fs-extra "^5.0.0"
resolve "^1.7.1"
rollup-pluginutils "^2.0.1"
tslib "1.9.2"
fs-extra "5.0.0"
resolve "1.8.1"
rollup-pluginutils "2.3.0"
tslib "1.9.3"

rollup-pluginutils@^2.0.1:
rollup-pluginutils@2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/rollup-pluginutils/-/rollup-pluginutils-2.3.0.tgz#478ace04bd7f6da2e724356ca798214884738fc4"
dependencies:
Expand Down Expand Up @@ -3927,11 +3927,7 @@ ts-jest@^22.4.6:
source-map-support "^0.5.5"
yargs "^11.0.0"

tslib@1.9.2:
version "1.9.2"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.2.tgz#8be0cc9a1f6dc7727c38deb16c2ebd1a2892988e"

tslib@^1.8.0, tslib@^1.8.1, tslib@^1.9.0:
tslib@1.9.3, tslib@^1.8.0, tslib@^1.8.1, tslib@^1.9.0:
version "1.9.3"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.3.tgz#d7e4dd79245d85428c4d7e4822a79917954ca286"

Expand Down

0 comments on commit 223bb03

Please sign in to comment.