File size: 1,396 Bytes
96c8713 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | import { Subscribable } from "./subscribable.js";
//#region src/focusManager.ts
var FocusManager = class extends Subscribable {
#focused;
#cleanup;
#setup;
constructor() {
super();
this.#setup = (onFocus) => {
if (typeof window !== "undefined" && window.addEventListener) {
const listener = () => onFocus();
window.addEventListener("visibilitychange", listener, false);
return () => {
window.removeEventListener("visibilitychange", listener);
};
}
};
}
onSubscribe() {
if (!this.#cleanup) this.setEventListener(this.#setup);
}
onUnsubscribe() {
if (!this.hasListeners()) {
this.#cleanup?.();
this.#cleanup = void 0;
}
}
setEventListener(setup) {
this.#setup = setup;
this.#cleanup?.();
this.#cleanup = setup((focused) => {
if (typeof focused === "boolean") this.setFocused(focused);
else this.onFocus();
});
}
setFocused(focused) {
if (this.#focused !== focused) {
this.#focused = focused;
this.onFocus();
}
}
onFocus() {
const isFocused = this.isFocused();
this.listeners.forEach((listener) => {
listener(isFocused);
});
}
isFocused() {
if (typeof this.#focused === "boolean") return this.#focused;
return globalThis.document?.visibilityState !== "hidden";
}
};
const focusManager = new FocusManager();
//#endregion
export { FocusManager, focusManager };
//# sourceMappingURL=focusManager.js.map |