cult-of-coders / grapher-react

Provides easy to use React Components that are suitable for grapher package.
https://atmospherejs.com/cultofcoders/grapher-react
38 stars 19 forks source link

Prevent situations where a logout can trigger unsubscribe of ready() data #24

Open theodorDiaconu opened 6 years ago

theodorDiaconu commented 6 years ago

Flow:

This was the fix, but I'm not sure exactly why, I leave it here to merge it later.


  const subscriptionError = new ReactiveVar();

  return withTracker((props) => {
    const query = handler(props);

    const subscriptionHandle = query.subscribe({
      onStop(err) {
        if (err) {
          subscriptionError.set(err);
        }
      },
      onReady() {
        subscriptionError.set(null);
      },
    });

    const isReady = subscriptionHandle.ready();

    return {
      isReady,
      query,
      config,
      props,
      subscriptionError,
    };
  })(dataTracker(errorTracker(QueryComponent)));
}```

```const dataTracker = withTracker(({ query, config, props, isReady, subscriptionError }) => ({
  grapher: {
    data: query.fetch(),
    isLoading: !isReady,
    error: subscriptionError,
  },
  query,
  config,
  props,
}));```
Floriferous commented 6 years ago

Just tried to clone this package and make these changes but it did not work.

Here's my setup:

When the user logs out, the subcomponent query is rerun, and the firewall throws an error, crashign the app on the client.

theodorDiaconu commented 6 years ago

@Floriferous it's normal for the subcomponent to re-run.

You logout -> Your subscriptions get updated. If you did not unmount the component, it may take some time before React unmounts it thus triggering unsubscribe. I would advise you to do something like history.push('/login'); Meteor.logout(), so you already unmount anything unnecessary.