UNPKG

@mopinion/deploy

Version:

Deploy your Mopinion feedback forms with ease

56 lines (55 loc) 2.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = createGlobalListenerStore; var _storage = require("../storage"); var _utils = require("../utils"); var _createListenerStore = _interopRequireDefault(require("./createListenerStore")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function createGlobalListenerStore() { var _getStorage; var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, _ref$onLocationChange = _ref.onLocationChange, onLocationChange = _ref$onLocationChange === void 0 ? function () {} : _ref$onLocationChange, _ref$onSessionChange = _ref.onSessionChange, onSessionChange = _ref$onSessionChange === void 0 ? function () {} : _ref$onSessionChange; var state = { currentLocation: document.location.href, previousLocation: (_getStorage = (0, _storage.getStorage)('previousLocation', 'sessionStorage')) !== null && _getStorage !== void 0 ? _getStorage : document.referrer, session: typeof (0, _storage.getStorage)('session') === 'number' ? (0, _storage.getStorage)('session') : (0, _utils.timeInSeconds)() }; var store = (0, _createListenerStore["default"])(); function setListeners() { store.setIntervalListener(function () { var didChange = state.currentLocation !== document.location.href; if (didChange) { state.previousLocation = state.currentLocation; state.currentLocation = document.location.href; (0, _storage.setStorage)('previousLocation', state.previousLocation, 'sessionStorage'); onLocationChange(); } }, 1000); store.setIntervalListener(function () { state.session = (0, _utils.timeInSeconds)(); (0, _storage.setStorage)('session', state.session); onSessionChange(); }, 30000); } return { setListeners: setListeners, clearListeners: function clearListeners() { return store.clearListeners(true); }, getCurrentLocation: function getCurrentLocation() { return state.currentLocation; }, getPreviousLocation: function getPreviousLocation() { return state.previousLocation; }, getSession: function getSession() { return state.session; } }; } //# sourceMappingURL=createGlobalListenerStore.js.map