import { Subscribable } from './subscribable.mjs'; import { isServer } from './utils.mjs'; class FocusManager extends Subscribable { constructor() { super(); this.setup = onFocus => { // addEventListener does not exist in React Native, but window does // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (!isServer && window.addEventListener) { const listener = () => onFocus(); // Listen to visibillitychange and focus window.addEventListener('visibilitychange', listener, false); window.addEventListener('focus', listener, false); return () => { // Be sure to unsubscribe if a new handler is set window.removeEventListener('visibilitychange', listener); window.removeEventListener('focus', listener); }; } return; }; } onSubscribe() { if (!this.cleanup) { this.setEventListener(this.setup); } } onUnsubscribe() { if (!this.hasListeners()) { var _this$cleanup; (_this$cleanup = this.cleanup) == null ? void 0 : _this$cleanup.call(this); this.cleanup = undefined; } } setEventListener(setup) { var _this$cleanup2; this.setup = setup; (_this$cleanup2 = this.cleanup) == null ? void 0 : _this$cleanup2.call(this); this.cleanup = setup(focused => { if (typeof focused === 'boolean') { this.setFocused(focused); } else { this.onFocus(); } }); } setFocused(focused) { const changed = this.focused !== focused; if (changed) { this.focused = focused; this.onFocus(); } } onFocus() { this.listeners.forEach(({ listener }) => { listener(); }); } 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); } } const focusManager = new FocusManager(); export { FocusManager, focusManager }; //# sourceMappingURL=focusManager.mjs.map