UNPKG

meetup-web-mocks

Version:
93 lines (64 loc) 3.38 kB
'use strict'; exports.__esModule = true; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _invariant = require('invariant'); var _invariant2 = _interopRequireDefault(_invariant); var _ExecutionEnvironment = require('./ExecutionEnvironment'); var _BrowserProtocol = require('./BrowserProtocol'); var BrowserProtocol = _interopRequireWildcard(_BrowserProtocol); var _RefreshProtocol = require('./RefreshProtocol'); var RefreshProtocol = _interopRequireWildcard(_RefreshProtocol); var _DOMUtils = require('./DOMUtils'); var _createHistory = require('./createHistory'); var _createHistory2 = _interopRequireDefault(_createHistory); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Creates and returns a history object that uses HTML5's history API * (pushState, replaceState, and the popstate event) to manage history. * This is the recommended method of managing history in browsers because * it provides the cleanest URLs. * * Note: In browsers that do not support the HTML5 history API full * page reloads will be used to preserve clean URLs. You can force this * behavior using { forceRefresh: true } in options. */ var createBrowserHistory = function createBrowserHistory() { var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; !_ExecutionEnvironment.canUseDOM ? process.env.NODE_ENV !== 'production' ? (0, _invariant2.default)(false, 'Browser history needs a DOM') : (0, _invariant2.default)(false) : void 0; var useRefresh = options.forceRefresh || !(0, _DOMUtils.supportsHistory)(); var Protocol = useRefresh ? RefreshProtocol : BrowserProtocol; var getUserConfirmation = Protocol.getUserConfirmation; var getCurrentLocation = Protocol.getCurrentLocation; var pushLocation = Protocol.pushLocation; var replaceLocation = Protocol.replaceLocation; var go = Protocol.go; var history = (0, _createHistory2.default)(_extends({ getUserConfirmation: getUserConfirmation }, options, { getCurrentLocation: getCurrentLocation, pushLocation: pushLocation, replaceLocation: replaceLocation, go: go })); var listenerCount = 0, stopListener = void 0; var startListener = function startListener(listener, before) { if (++listenerCount === 1) stopListener = BrowserProtocol.startListener(history.transitionTo); var unlisten = before ? history.listenBefore(listener) : history.listen(listener); return function () { unlisten(); if (--listenerCount === 0) stopListener(); }; }; var listenBefore = function listenBefore(listener) { return startListener(listener, true); }; var listen = function listen(listener) { return startListener(listener, false); }; return _extends({}, history, { listenBefore: listenBefore, listen: listen }); }; exports.default = createBrowserHistory;