redux-requests icon indicating copy to clipboard operation
redux-requests copied to clipboard

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

Open favger opened this issue 4 years ago • 4 comments

meta: { pollWhenHidden: false, revalidateOnFocus: false }

https://github.com/klis87/redux-requests/blob/0176826fd239d9e1319439cae2c96f1140728792/packages/redux-requests/src/middleware/create-polling-middleware.js#L44-L53

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.

favger avatar Feb 21 '21 14:02 favger

@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!

klis87 avatar Feb 21 '21 22:02 klis87

@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... 😃

favger avatar Feb 23 '21 14:02 favger

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!

klis87 avatar Feb 23 '21 15:02 klis87

Got it, thank you very much for your interest.

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

favger avatar Feb 23 '21 15:02 favger