Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Few parameters needed to be added for polling (if window active & if window focus) #446

Open
favger opened this issue Feb 21, 2021 · 4 comments

Comments

@favger
Copy link

favger commented Feb 21, 2021

meta: {
pollWhenHidden: false,
revalidateOnFocus: false
}

if (
config.isRequestAction(action) &&
config.isRequestActionQuery(action) &&
action.meta?.poll &&
!action.meta.polled
) {
intervals[getIntervalKey(action)] = setInterval(() => {
store.dispatch({ ...action, meta: { ...action.meta, polled: true } });
}, action.meta.poll * 1000);
}

Something like this (pollWhenHidden):

      intervals[getIntervalKey(action)] = setInterval(() => {
        const isWindowActive = store.getState().requests.isWindowActive;
        if(action.meta.pollWhenHidden === false && !isWindowActive) return;
        store.dispatch({ ...action, meta: { ...action.meta, polled: true } });
      }, action.meta.poll * 1000);

Something like this (revalidateOnFocus):

     window.addEventListener("focus", () => {
        if(action.meta?.poll && action.meta.revalidateOnFocus === true){
           store.dispatch({ ...action, meta: { ...action.meta, polled: true } });
        }
     }

@klis87 We need your help in this matter.

It's going to be a super but super thing if we integrate it into the library.

@klis87
Copy link
Owner

klis87 commented Feb 21, 2021

@favger I don't think I would add window listeners into the core, this is too opinionated and DOM dependent. What we could probably do is a way to pause/resume pollings. Interestingly, I don't think we need to add anything to core, as instead of pause/resume polling, you can just stopPolling and then just fetch sth with meta.poll again to resume. What we need I think is some extra property on useQuery level, like pausePolling: boolean, it could work like this:

  1. false by default
  2. if true, useQuery will remove poll from meta for any query load
  3. if changing from false to true, stopPolling will be dispatched
  4. if changing from true to false, query will be dispatched, without stripping poll from meta of course

We could also add pausePolling to RequestsProvider, so that this could be configured for all hooks.

Then, you would use whatever logic you want, like yours, to set this pausePolling flag, and pause/resume logic will be done for you. Is that solution suitable for you?

Btw, if you use polling, you might be also interested in recently added websocket and subscriptions!

@favger
Copy link
Author

favger commented Feb 23, 2021

@klis87 I was just trying to explain the situation. I have no idea how to integrate the features into the library.

"Then, you would use whatever logic you want, like yours, to set this pausePolling flag, and pause/resume logic will be done for you. Is that solution suitable for you?"

  • I think that would be an extra effort.
  • Like creating a global variable (isWindowActive), we have to check it inside the library codes.
  • Would it take you a lot of time to add these features to the code?

I hope I could explain the situation... 😃

@klis87
Copy link
Owner

klis87 commented Feb 23, 2021

I think that would be an extra effort

I understand, but we cannot add opinionated things to the core, as API will grow too much, I want to keep things as flexible and minimalistic as possible

Like creating a global variable (isWindowActive), we have to check it inside the library codes.

If I understand correctly yes, you would put whatever logic you need like isWindowActive on the app level and pass it to pausePolling

Would it take you a lot of time to add these features to the code?

My version I guess not, probably several hours, but I am very busy recently, and also have other tasks on TODO list, so I don't know when this could be added. But I would always accept a PR!

If not, it is possible to wrap useQuery on your own on the app level in 1 place, and to refetch query and dispatch stopPolling action whenever it suits you - React way!

@favger
Copy link
Author

favger commented Feb 23, 2021

Got it, thank you very much for your interest.

I'll work with a private wrapper as you said and share the solution here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants