Skip to content

Option to pause Polling when window Unfocused #2516

@littlejon

Description

@littlejon

Sorry if this is a silly question, but is it possible to pause polling on a query if the window loses focus.

I currently have a situation where i need to manually initiate a query due to some other bad code where i have used prefetch in a horrible hacky way to bypass cache timeout (it's a pagination/inifinate scroll issue that is first on my list for a refactor!!).

  useEffect(() => {
    let unsubscribe: () => void;

    if (id) {
      const result = dispatch(
        getChatHistory.initiate(
          { id, page: 1 },
          {
            subscribe: true,
            subscriptionOptions: {
              pollingInterval: 10000,
            },
          }
        )
      );

      unsubscribe = result.unsubscribe;
    }

    return () => {
      unsubscribe && unsubscribe();
    };
  }, [id, dispatch]);

If there is no "native" way to pause polling, I think it should be possible to create a listener for the focus/unfocus action dispatches and set some state value accordingly.

I have posted here instead of SO in case the functionality could be useful (please close/delete the issue if there is no merit in the idea and I will ask on SO instead).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions