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

Commit

Permalink
lazy and correct ws reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
elitan committed Sep 10, 2020
1 parent ce397bc commit 65cc928
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"presets": ["@babel/preset-react", "@babel/preset-env"]
"presets": ["@babel/preset-react", "@babel/preset-env"],
"comments": false
}
29 changes: 27 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function generateApolloClient(
uri: wsUri,
options: {
reconnect: true,
lazy: true,
connectionParams: () => {
const connectionHeaders = getheaders(auth);
return {
Expand Down Expand Up @@ -106,17 +107,41 @@ export class NhostApolloProvider extends React.Component {
this.client = client;
this.wsLink = wsLink;

// for debug
// const ssr = typeof window === "undefined";
// if (!ssr) {
// console.log(" not ssr, set up .on");
// this.wsLink.subscriptionClient.on("connecting", () =>
// console.log("connecting")
// );
// this.wsLink.subscriptionClient.on("connected", () =>
// console.log("connected")
// );
// this.wsLink.subscriptionClient.on("reconnecting", () =>
// console.log("reconnecting")
// );
// this.wsLink.subscriptionClient.on("reconnected", () =>
// console.log("reconnected")
// );
// this.wsLink.subscriptionClient.on("disconnected", () =>
// console.log("disconnected")
// );
// this.wsLink.subscriptionClient.on("error", () => console.log("error"));
// }

if (this.props.auth) {
this.props.auth.onTokenChanged(() => {
if (this.wsLink.subscriptionClient.status === 1) {
this.wsLink.subscriptionClient.tryReconnect();
} else {
}
});

this.props.auth.onAuthStateChanged((data) => {
// reconnect ws connection with new auth headers for the logged in user
// reconnect ws connection with new auth headers for the logged in/out user
if (this.wsLink.subscriptionClient.status === 1) {
// must close first to avoid race conditions
this.wsLink.subscriptionClient.close();
// reconnect
this.wsLink.subscriptionClient.tryReconnect();
}
});
Expand Down

0 comments on commit 65cc928

Please sign in to comment.