UNPKG

@livelike/react-native

Version:

LiveLike React Native package

76 lines (73 loc) 3.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useWidgetExpiryEffect = useWidgetExpiryEffect; var _react = require("react"); var _store = require("../store"); var _types = require("../types"); var _useSelectedFieldStore = require("./useSelectedFieldStore"); var _useWidgetActions = require("./useWidgetActions"); var _useWidgetUIPhase = require("./useWidgetUIPhase"); const MAX_SET_TIMEOUT_DELAY = 2147483647; /** * @description useWidgetExpiryEffect hook runs a timer to update widget UI phase to EXPIRED * once the expiry time has passed. Expiry time is set when creating widget from producer suite * @since 0.1.0 */ function useWidgetExpiryEffect(_ref) { let { widgetId } = _ref; const { updateWidgetUIPhaseAction } = (0, _useWidgetActions.useWidgetActions)({ widgetId }); const expiredTimeStamp = (0, _useSelectedFieldStore.useSelectedFieldStore)(_store.widgetStore, () => { var _widgetStore$get$widg; return (_widgetStore$get$widg = _store.widgetStore.get()[widgetId]) === null || _widgetStore$get$widg === void 0 || (_widgetStore$get$widg = _widgetStore$get$widg.widgetPayload) === null || _widgetStore$get$widg === void 0 ? void 0 : _widgetStore$get$widg.interactive_until; }); const currentWidgetUIPhase = (0, _useWidgetUIPhase.useWidgetUIPhase)({ widgetId }); const isWidgetEndUIPhase = currentWidgetUIPhase === _types.WidgetUIPhase.INTERACTIVE_TIMED_OUT || currentWidgetUIPhase === _types.WidgetUIPhase.FOLLOW_UP_PUBLISHED; (0, _react.useEffect)(() => { if (!expiredTimeStamp || isWidgetEndUIPhase) return; const expiredTime = new Date(expiredTimeStamp); let expiryTimeout; function expiryTimeoutFn() { const timeout = expiredTime.getTime() - Date.now(); if (timeout <= 0) { updateWidgetUIPhaseAction({ widgetId, widgetUIPhase: _types.WidgetUIPhase.EXPIRED }); return; } // setTimeout stores the delay as 32-bit signed integer. So, when interactive_until is set // for more than 24.8 days later, the code executes immediately disabling the widget. // The below if condition is added to handle the same. // If timeout is greater than MAX_SET_TIMEOUT_DELAY, a setTimeout with MAX_SET_TIMEOUT_DELAY // as the delay will run which will recursively call startInteractiveUntilTimer again and // again until the timeout is less than MAX_SET_TIMEOUT_DELAY and eventually sets this.disabled as true if (timeout > MAX_SET_TIMEOUT_DELAY) { expiryTimeout = setTimeout(() => { expiryTimeoutFn(); }, MAX_SET_TIMEOUT_DELAY); } else { expiryTimeout = setTimeout(() => { updateWidgetUIPhaseAction({ widgetId, widgetUIPhase: _types.WidgetUIPhase.EXPIRED }); }, timeout); } } expiryTimeoutFn(); return () => { clearTimeout(expiryTimeout); }; }, [expiredTimeStamp, isWidgetEndUIPhase]); } //# sourceMappingURL=useWidgetExpiryEffect.js.map