mirror of
https://github.com/vbenjs/vue-vben-admin.git
synced 2025-08-27 11:02:11 +08:00
feat(demo): hooks useRequest 异步数据管理 (#3447)
This commit is contained in:
30
packages/hooks/src/useRequest/utils/subscribeFocus.ts
Normal file
30
packages/hooks/src/useRequest/utils/subscribeFocus.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { isBrowser } from './isBrowser';
|
||||
import { isDocumentVisible } from './isDocumentVisible';
|
||||
import { isOnline } from './isOnline';
|
||||
|
||||
type Listener = () => void;
|
||||
|
||||
const listeners: Listener[] = [];
|
||||
|
||||
if (isBrowser) {
|
||||
const revalidate = () => {
|
||||
if (!isDocumentVisible() || !isOnline()) return;
|
||||
for (let i = 0; i < listeners.length; i++) {
|
||||
const listener = listeners[i];
|
||||
listener();
|
||||
}
|
||||
};
|
||||
window.addEventListener('visibilitychange', revalidate, false);
|
||||
window.addEventListener('focus', revalidate, false);
|
||||
}
|
||||
|
||||
export default function subscribe(listener: Listener) {
|
||||
listeners.push(listener);
|
||||
|
||||
return function unsubscribe() {
|
||||
const index = listeners.indexOf(listener);
|
||||
if (index > -1) {
|
||||
listeners.splice(index, 1);
|
||||
}
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user