Skip to content

Polling doesn't stop after component dismount #174

Open
@gtkatakura

Description

@gtkatakura

I am struggling with a problem when using polling. Seems that even after my component dismount, the polling keeps happening and never stops. It is a very hard problem to reproduce, but I find that when we have the StrictMode around the app, the problems happen all the time. To be able to create a reproducible example, I changed the relay-hook-example/todo of this repository here: gtkatakura/relay-hooks#polling-problem.

  1. I added support to cancel the request into the fetchQuery:

commit changes

    const controller = new AbortController();

    fetch('http://localhost:3000/graphql', {
      signal: controller.signal,
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        query: operation.text,
        variables,
      }),
    })
      .then(response => response.json())
      .then(data => {
        if (data.errors) {
          sink.error(data.errors);
          return;
        }
        sink.next(data);
        sink.complete();
      });

    return () => {
      controller.abort();
    };
  1. I changed the polling to 5 seconds and added a setTimeout into the back-end to resolve the response into 3 seconds

  2. I added a button to hide and show the component on the screen

  3. I enabled the StrictMode

So after these changes, you just need to run the todo example and click into hide to dismount the component with the user query. You will see that after the dismount, it will keep polling the query. And if you click into hide when is happening the fetch, it doesn't cancel the request. If you remove the StrictMode, it will cancel the request properly.

Screenshot from 2021-05-17 16-15-57

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions