UNPKG

5.39 kBJavaScriptView Raw
1var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
2
3var _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; };
4
5import warning from 'warning';
6import { createPath } from './PathUtils';
7import { createLocation } from './LocationUtils';
8import createTransitionManager from './createTransitionManager';
9
10var clamp = function clamp(n, lowerBound, upperBound) {
11 return Math.min(Math.max(n, lowerBound), upperBound);
12};
13
14/**
15 * Creates a history object that stores locations in memory.
16 */
17var createMemoryHistory = function createMemoryHistory() {
18 var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
19 var getUserConfirmation = props.getUserConfirmation,
20 _props$initialEntries = props.initialEntries,
21 initialEntries = _props$initialEntries === undefined ? ['/'] : _props$initialEntries,
22 _props$initialIndex = props.initialIndex,
23 initialIndex = _props$initialIndex === undefined ? 0 : _props$initialIndex,
24 _props$keyLength = props.keyLength,
25 keyLength = _props$keyLength === undefined ? 6 : _props$keyLength;
26
27
28 var transitionManager = createTransitionManager();
29
30 var setState = function setState(nextState) {
31 _extends(history, nextState);
32
33 history.length = history.entries.length;
34
35 transitionManager.notifyListeners(history.location, history.action);
36 };
37
38 var createKey = function createKey() {
39 return Math.random().toString(36).substr(2, keyLength);
40 };
41
42 var index = clamp(initialIndex, 0, initialEntries.length - 1);
43 var entries = initialEntries.map(function (entry) {
44 return typeof entry === 'string' ? createLocation(entry, undefined, createKey()) : createLocation(entry, undefined, entry.key || createKey());
45 });
46
47 // Public interface
48
49 var createHref = createPath;
50
51 var push = function push(path, state) {
52 warning(!((typeof path === 'undefined' ? 'undefined' : _typeof(path)) === 'object' && path.state !== undefined && state !== undefined), 'You should avoid providing a 2nd state argument to push when the 1st ' + 'argument is a location-like object that already has state; it is ignored');
53
54 var action = 'PUSH';
55 var location = createLocation(path, state, createKey(), history.location);
56
57 transitionManager.confirmTransitionTo(location, action, getUserConfirmation, function (ok) {
58 if (!ok) return;
59
60 var prevIndex = history.index;
61 var nextIndex = prevIndex + 1;
62
63 var nextEntries = history.entries.slice(0);
64 if (nextEntries.length > nextIndex) {
65 nextEntries.splice(nextIndex, nextEntries.length - nextIndex, location);
66 } else {
67 nextEntries.push(location);
68 }
69
70 setState({
71 action: action,
72 location: location,
73 index: nextIndex,
74 entries: nextEntries
75 });
76 });
77 };
78
79 var replace = function replace(path, state) {
80 warning(!((typeof path === 'undefined' ? 'undefined' : _typeof(path)) === 'object' && path.state !== undefined && state !== undefined), 'You should avoid providing a 2nd state argument to replace when the 1st ' + 'argument is a location-like object that already has state; it is ignored');
81
82 var action = 'REPLACE';
83 var location = createLocation(path, state, createKey(), history.location);
84
85 transitionManager.confirmTransitionTo(location, action, getUserConfirmation, function (ok) {
86 if (!ok) return;
87
88 history.entries[history.index] = location;
89
90 setState({ action: action, location: location });
91 });
92 };
93
94 var go = function go(n) {
95 var nextIndex = clamp(history.index + n, 0, history.entries.length - 1);
96
97 var action = 'POP';
98 var location = history.entries[nextIndex];
99
100 transitionManager.confirmTransitionTo(location, action, getUserConfirmation, function (ok) {
101 if (ok) {
102 setState({
103 action: action,
104 location: location,
105 index: nextIndex
106 });
107 } else {
108 // Mimic the behavior of DOM histories by
109 // causing a render after a cancelled POP.
110 setState();
111 }
112 });
113 };
114
115 var goBack = function goBack() {
116 return go(-1);
117 };
118
119 var goForward = function goForward() {
120 return go(1);
121 };
122
123 var canGo = function canGo(n) {
124 var nextIndex = history.index + n;
125 return nextIndex >= 0 && nextIndex < history.entries.length;
126 };
127
128 var block = function block() {
129 var prompt = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
130 return transitionManager.setPrompt(prompt);
131 };
132
133 var listen = function listen(listener) {
134 return transitionManager.appendListener(listener);
135 };
136
137 var history = {
138 length: entries.length,
139 action: 'POP',
140 location: entries[index],
141 index: index,
142 entries: entries,
143 createHref: createHref,
144 push: push,
145 replace: replace,
146 go: go,
147 goBack: goBack,
148 goForward: goForward,
149 canGo: canGo,
150 block: block,
151 listen: listen
152 };
153
154 return history;
155};
156
157export default createMemoryHistory;
\No newline at end of file