Co-authored-by: Ivan Akulov <mail@iamakulov.com> Co-authored-by: Satish Gandham <hello@satishgandham.com> Co-authored-by: Ivan Akulov <iamakulov@outlook.com> Co-authored-by: Aishwarya UR <aishwarya@appsmith.com> Co-authored-by: Shrikant Sharat Kandula <shrikant@appsmith.com> Co-authored-by: somangshu <somangshu.goswami1508@gmail.com>
34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
import type { EventChannel } from "redux-saga";
|
|
import { eventChannel } from "redux-saga";
|
|
import { call, fork, put, take } from "redux-saga/effects";
|
|
import { pageVisibilityAppEvent } from "actions/pageVisibilityActions";
|
|
|
|
// Track page visibility
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API
|
|
function listenToVisibilityEvents() {
|
|
return eventChannel((emitter) => {
|
|
document.addEventListener("visibilitychange", emitter, false);
|
|
|
|
return () => {
|
|
document.removeEventListener("visibilitychange", emitter, false);
|
|
};
|
|
});
|
|
}
|
|
|
|
function* handleTabVisibilityConnection() {
|
|
const channel: EventChannel<unknown> = yield call(listenToVisibilityEvents);
|
|
while (true) {
|
|
const event: {
|
|
target: { visibilityState: DocumentVisibilityState };
|
|
} = yield take(channel);
|
|
// Only invoke when page gets visible
|
|
if (event.target && event.target.visibilityState === "visible") {
|
|
yield put(pageVisibilityAppEvent(event.target.visibilityState));
|
|
}
|
|
}
|
|
}
|
|
|
|
export default function* rootSaga() {
|
|
yield fork(handleTabVisibilityConnection);
|
|
}
|