redux-requests
redux-requests copied to clipboard
Few parameters needed to be added for polling (if window active & if window focus)
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 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:
-
false
by default - if
true
,useQuery
will removepoll
from meta for any query load - if changing from
false
totrue
,stopPolling
will be dispatched - if changing from
true
tofalse
, 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 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... 😃
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!
Got it, thank you very much for your interest.
I'll work with a private wrapper as you said and share the solution here.