whodis-react
Version:
React hooks and components for secure, best practices authentication in seconds
32 lines • 1.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getUniOnScreenFocusEventStream = void 0;
const getPackageSafely_1 = require("./getPackageSafely");
const hasPackageAvailable_1 = require("./hasPackageAvailable");
/**
* .what = a universal utility for subscribing to screen focus events
* .why =
* - uses Window api in runtime=browser
* - uses AppState api in runtime=native
*/
const getUniOnScreenFocusEventStream = () => {
const isRuntimeNative = (0, hasPackageAvailable_1.hasPackageAccessible)('react-native');
// if native, use appstate api
if (isRuntimeNative) {
const { AppState } = (0, getPackageSafely_1.getPackageSafely)('react-native');
let subscription = undefined;
return {
subscribe: (input) => {
subscription = AppState.addEventListener('focus', input.consumer);
},
unsubscribe: () => subscription === null || subscription === void 0 ? void 0 : subscription.remove(),
};
}
// else, use window api
return {
subscribe: (input) => { var _a; return (_a = globalThis.addEventListener) === null || _a === void 0 ? void 0 : _a.call(globalThis, 'focus', input.consumer); },
unsubscribe: (input) => { var _a; return (_a = globalThis.removeEventListener) === null || _a === void 0 ? void 0 : _a.call(globalThis, 'focus', input.consumer); },
};
};
exports.getUniOnScreenFocusEventStream = getUniOnScreenFocusEventStream;
//# sourceMappingURL=getUniOnScreenFocusEventStream.js.map