voicebot-react-native-expo
Version:
This is a voicebot-react-native package of Kipps AI voice bot for React Native Expo
157 lines (155 loc) • 6.44 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
const DEFAULT_DELAY = 1000;
class TimeoutHandler {
constructor() {
_defineProperty(this, "handlerRef", {
id: -1
});
}
get handler() {
return this.handlerRef.id;
}
set handler(n) {
this.handlerRef.id = n;
}
clear() {
clearTimeout(this.handlerRef.id);
}
}
function setIntervalWithTimeout(callback, intervalMs, handleWrapper = new TimeoutHandler()) {
let cleared = false;
const timeout = () => {
handleWrapper.handler = setTimeout(() => {
callback(() => {
cleared = true;
handleWrapper.clear();
});
if (!cleared) {
timeout();
}
}, intervalMs);
};
timeout();
return handleWrapper;
}
/**
* Detects when this is in the viewport and visible.
*
* Will not fire visibility changes for zero width/height components.
*/
class ViewPortDetector extends _react.Component {
constructor(props) {
super(props);
_defineProperty(this, "lastValue", null);
_defineProperty(this, "interval", null);
_defineProperty(this, "view", null);
_defineProperty(this, "lastAppStateActive", false);
_defineProperty(this, "appStateSubscription", null);
_defineProperty(this, "hasValidTimeout", (disabled, delay) => {
let disabledValue = disabled ?? false;
let delayValue = delay ?? DEFAULT_DELAY;
return _reactNative.AppState.currentState === 'active' && !disabledValue && delayValue > 0;
});
_defineProperty(this, "handleAppStateChange", nextAppState => {
let nextAppStateActive = nextAppState === 'active';
if (this.lastAppStateActive !== nextAppStateActive) {
this.checkVisibility();
}
this.lastAppStateActive = nextAppStateActive;
if (!this.hasValidTimeout(this.props.disabled, this.props.delay)) {
this.stopWatching();
} else {
this.startWatching();
}
});
_defineProperty(this, "startWatching", () => {
if (this.interval) {
return;
}
this.interval = setIntervalWithTimeout(this.checkVisibility, this.props.delay || DEFAULT_DELAY);
});
_defineProperty(this, "stopWatching", () => {
var _this$interval;
(_this$interval = this.interval) === null || _this$interval === void 0 || _this$interval.clear();
this.interval = null;
});
_defineProperty(this, "checkVisibility", () => {
if (!this.view) {
return;
}
if (_reactNative.AppState.currentState !== 'active') {
this.updateVisibility(false);
return;
}
this.view.measure((_x, _y, width, height, _pageX, _pageY) => {
this.checkInViewPort(width, height);
});
});
_defineProperty(this, "checkInViewPort", (width, height) => {
let isVisible;
// Not visible if any of these are missing.
if (!width || !height) {
isVisible = false;
} else {
isVisible = true;
}
this.updateVisibility(isVisible);
});
_defineProperty(this, "updateVisibility", isVisible => {
if (this.lastValue !== isVisible) {
var _this$props$onChange, _this$props;
this.lastValue = isVisible;
(_this$props$onChange = (_this$props = this.props).onChange) === null || _this$props$onChange === void 0 || _this$props$onChange.call(_this$props, isVisible);
}
});
this.state = {
rectTop: 0,
rectBottom: 0
};
}
componentDidMount() {
this.lastAppStateActive = _reactNative.AppState.currentState === 'active';
this.appStateSubscription = _reactNative.AppState.addEventListener('change', this.handleAppStateChange);
if (this.hasValidTimeout(this.props.disabled, this.props.delay)) {
this.startWatching();
}
}
componentWillUnmount() {
var _this$appStateSubscri;
(_this$appStateSubscri = this.appStateSubscription) === null || _this$appStateSubscri === void 0 || _this$appStateSubscri.remove();
this.appStateSubscription = null;
this.stopWatching();
}
UNSAFE_componentWillReceiveProps(nextProps) {
if (!this.hasValidTimeout(nextProps.disabled, nextProps.delay)) {
this.stopWatching();
} else {
if (this.props.propKey !== nextProps.propKey) {
this.lastValue = null;
}
this.startWatching();
}
}
render() {
return /*#__PURE__*/_react.default.createElement(_reactNative.View, _extends({
collapsable: false,
ref: component => {
this.view = component;
}
}, this.props), this.props.children);
}
}
exports.default = ViewPortDetector;
//# sourceMappingURL=ViewPortDetector.js.map