UNPKG

@tamagui/react-native-web-lite

Version:
93 lines (90 loc) 3.87 kB
import { canUseDOM, invariant } from "@tamagui/react-native-web-internals"; function _class_call_check(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor); } } function _create_class(Constructor, protoProps, staticProps) { return protoProps && _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), Constructor; } function _define_property(obj, key, value) { return key in obj ? Object.defineProperty(obj, key, { value, enumerable: !0, configurable: !0, writable: !0 }) : obj[key] = value, obj; } var isPrefixed = // eslint-disable-next-line no-prototype-builtins canUseDOM && !document.hasOwnProperty("hidden") && document.hasOwnProperty("webkitHidden"), EVENT_TYPES = ["change", "memoryWarning"], VISIBILITY_CHANGE_EVENT = isPrefixed ? "webkitvisibilitychange" : "visibilitychange", VISIBILITY_STATE_PROPERTY = isPrefixed ? "webkitVisibilityState" : "visibilityState", AppStates = { BACKGROUND: "background", ACTIVE: "active" }, EventEmitter = /* @__PURE__ */function () { "use strict"; function EventEmitter2() { _class_call_check(this, EventEmitter2), _define_property(this, "listeners", {}); } return _create_class(EventEmitter2, [{ key: "addListener", value: function (type, handler) { var _this_listeners, _type, _; (_ = (_this_listeners = this.listeners)[_type = type]) !== null && _ !== void 0 || (_this_listeners[_type] = /* @__PURE__ */new Set()), this.listeners[type].add(handler); } }, { key: "emit", value: function (type, state) { var _this_listeners_type; (_this_listeners_type = this.listeners[type]) === null || _this_listeners_type === void 0 || _this_listeners_type.forEach(function (cb) { return cb(state); }); } }, { key: "removeListener", value: function (type, handler) { var _this_listeners_type; (_this_listeners_type = this.listeners[type]) === null || _this_listeners_type === void 0 || _this_listeners_type.delete(handler); } }]), EventEmitter2; }(), hasBoundVisibilityChangeEvent = !1, changeEmitter = new EventEmitter(), AppState = /* @__PURE__ */function () { "use strict"; function AppState2() { _class_call_check(this, AppState2); } return _create_class(AppState2, null, [{ key: "currentState", get: function () { if (!AppState2.isAvailable) return AppStates.ACTIVE; switch (document[VISIBILITY_STATE_PROPERTY]) { case "hidden": case "prerender": case "unloaded": return AppStates.BACKGROUND; default: return AppStates.ACTIVE; } } }, { key: "addEventListener", value: function (type, handler) { if (AppState2.isAvailable && (invariant(EVENT_TYPES.indexOf(type) !== -1, 'Trying to subscribe to unknown event: "%s"', type), type === "change")) return hasBoundVisibilityChangeEvent || (hasBoundVisibilityChangeEvent = !0, document.addEventListener(VISIBILITY_CHANGE_EVENT, function () { changeEmitter && changeEmitter.emit("change", AppState2.currentState); }, !1)), changeEmitter.addListener(type, handler); } }]), AppState2; }(); _define_property(AppState, "isAvailable", canUseDOM && document[VISIBILITY_STATE_PROPERTY]); export { AppState as default }; //# sourceMappingURL=index.native.js.map