UNPKG

react-view-router

Version:
183 lines (180 loc) 6.22 kB
import "core-js/modules/es6.symbol.js"; import "core-js/modules/es6.array.filter.js"; import "core-js/modules/es7.object.get-own-property-descriptors.js"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import "core-js/modules/es6.array.map.js"; import "core-js/modules/es6.regexp.search.js"; import { Action, HistoryType } from './types'; import { createEvents, createPath, createKey, parsePath, freeze, clamp, allowTx } from './utils'; /** * A memory history stores locations in memory. This is useful in stateful * environments where there is no web browser, such as node tests or React * Native. * * @see https://github.com/ReactTraining/history/tree/master/docs/api-reference.md#memoryhistory */ /** * A user-supplied object that describes a location. Used when providing * entries to `createMemoryHistory` via its `initialEntries` option. */ export function createMemoryHref(to) { return typeof to === 'string' ? to : createPath(to); } /** * Memory history stores the current location in memory. It is designed for use * in stateful non-browser environments like tests and React Native. * * @see https://github.com/ReactTraining/history/tree/master/docs/api-reference.md#creatememoryhistory */ export function createMemoryHistory(options = {}) { var _options$initialEntri = options.initialEntries, initialEntries = _options$initialEntri === void 0 ? ['/'] : _options$initialEntri, initialIndex = options.initialIndex; var entries = initialEntries.map(entry => { var location = freeze(_objectSpread({ pathname: '/', search: '', hash: '', state: null, key: createKey() }, typeof entry === 'string' ? parsePath(entry) : entry)); return location; }); var index = clamp(initialIndex == null ? entries.length - 1 : initialIndex, 0, entries.length - 1); var type = HistoryType.memory; var action = Action.Push; var location = entries[index]; var listeners = createEvents(); var blockers = createEvents(); var createHref = to => createMemoryHref(to); function getNextLocation(to, state = null) { var _location = location, _location$pathname = _location.pathname, pathname = _location$pathname === void 0 ? '/' : _location$pathname, _location$search = _location.search, search = _location$search === void 0 ? '' : _location$search, _location$hash = _location.hash, hash = _location$hash === void 0 ? '' : _location$hash; return freeze(_objectSpread(_objectSpread({ pathname, search, hash }, typeof to === 'string' ? parsePath(to) : to), {}, { state, key: createKey() })); } function applyTx(nextAction, nextLocation, nextIndex, payload) { action = nextAction; location = nextLocation; index = nextIndex; listeners.call({ action, location, index }, payload); } function getIndex(delta) { var ret = index; if (delta) { index = clamp(index + delta, 0, entries.length - 1); } return ret; } function push(to, state) { var nextAction = Action.Push; var nextLocation = getNextLocation(to, state); var callback = (ok, payload) => { if (!ok) return; index = getIndex(typeof to !== 'string' ? to.delta : undefined) + 1; entries.splice(index, entries.length, nextLocation); applyTx(nextAction, nextLocation, index, payload); }; if (allowTx(blockers, nextAction, nextLocation, index, getIndex(typeof to !== 'string' ? to.delta : undefined) + 1, callback)) { callback(true, to); } } function replace(to, state) { var nextAction = Action.Replace; var nextLocation = getNextLocation(to, state); var callback = (ok, payload) => { if (!ok) return; if (typeof to !== 'string' && to.delta) { index = clamp(index + to.delta, 0, entries.length - 1); } entries[index] = nextLocation; applyTx(nextAction, nextLocation, index, payload); }; if (allowTx(blockers, nextAction, nextLocation, index, getIndex(typeof to !== 'string' ? to.delta : undefined), callback)) { callback(true, to); } } function go(delta) { var nextIndex = clamp(index + delta, 0, entries.length - 1); var nextAction = Action.Pop; var nextLocation = entries[nextIndex]; var callback = (ok, payload) => { if (!ok) return; index = nextIndex; applyTx(nextAction, nextLocation, index, payload); }; if (allowTx(blockers, nextAction, nextLocation, index, getIndex(delta), callback)) { callback(true); } } var history = { get extra() { return options.extra; }, get type() { return type; }, get action() { return action; }, get location() { return location; }, get index() { return index; }, get length() { return entries.length; }, get state() { return location.state; }, get realtimeLocation() { return location; }, createHref, getIndexAndLocation() { return [index, location]; }, push, replace, replaceState(state) { return location.state = state; }, refresh() { return [index, location]; }, go, back() { go(-1); }, forward() { go(1); }, listen(listener) { return listeners.push(listener); }, block(blocker) { return blockers.push(blocker); } }; return history; } //# sourceMappingURL=memory.js.map