UNPKG

react-query

Version:

Hooks for managing, caching and syncing asynchronous and remote data in React

87 lines (67 loc) 2.3 kB
import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose"; import { Subscribable } from './subscribable'; import { isServer } from './utils'; var FocusManager = /*#__PURE__*/function (_Subscribable) { _inheritsLoose(FocusManager, _Subscribable); function FocusManager() { return _Subscribable.apply(this, arguments) || this; } var _proto = FocusManager.prototype; _proto.onSubscribe = function onSubscribe() { if (!this.removeEventListener) { this.setDefaultEventListener(); } }; _proto.setEventListener = function setEventListener(setup) { var _this = this; if (this.removeEventListener) { this.removeEventListener(); } this.removeEventListener = setup(function (focused) { if (typeof focused === 'boolean') { _this.setFocused(focused); } else { _this.onFocus(); } }); }; _proto.setFocused = function setFocused(focused) { this.focused = focused; if (focused) { this.onFocus(); } }; _proto.onFocus = function onFocus() { this.listeners.forEach(function (listener) { listener(); }); }; _proto.isFocused = function isFocused() { if (typeof this.focused === 'boolean') { return this.focused; } // document global can be unavailable in react native if (typeof document === 'undefined') { return true; } return [undefined, 'visible', 'prerender'].includes(document.visibilityState); }; _proto.setDefaultEventListener = function setDefaultEventListener() { var _window; if (!isServer && ((_window = window) == null ? void 0 : _window.addEventListener)) { this.setEventListener(function (onFocus) { var listener = function listener() { return onFocus(); }; // Listen to visibillitychange and focus window.addEventListener('visibilitychange', listener, false); window.addEventListener('focus', listener, false); return function () { // Be sure to unsubscribe if a new handler is set window.removeEventListener('visibilitychange', listener); window.removeEventListener('focus', listener); }; }); } }; return FocusManager; }(Subscribable); export var focusManager = new FocusManager();