UNPKG

20.8 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = useLinking;
7
8var _core = require("@react-navigation/core");
9
10var _nonSecure = require("nanoid/non-secure");
11
12var React = _interopRequireWildcard(require("react"));
13
14var _ServerContext = _interopRequireDefault(require("./ServerContext"));
15
16function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
18function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
19
20function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
21
22const createMemoryHistory = () => {
23 let index = 0;
24 let items = []; // Pending callbacks for `history.go(n)`
25 // We might modify the callback stored if it was interrupted, so we have a ref to identify it
26
27 const pending = [];
28
29 const interrupt = () => {
30 // If another history operation was performed we need to interrupt existing ones
31 // This makes sure that calls such as `history.replace` after `history.go` don't happen
32 // Since otherwise it won't be correct if something else has changed
33 pending.forEach(it => {
34 const cb = it.cb;
35
36 it.cb = () => cb(true);
37 });
38 };
39
40 const history = {
41 get index() {
42 var _window$history$state;
43
44 // We store an id in the state instead of an index
45 // Index could get out of sync with in-memory values if page reloads
46 const id = (_window$history$state = window.history.state) === null || _window$history$state === void 0 ? void 0 : _window$history$state.id;
47
48 if (id) {
49 const index = items.findIndex(item => item.id === id);
50 return index > -1 ? index : 0;
51 }
52
53 return 0;
54 },
55
56 get(index) {
57 return items[index];
58 },
59
60 backIndex({
61 path
62 }) {
63 // We need to find the index from the element before current to get closest path to go back to
64 for (let i = index - 1; i >= 0; i--) {
65 const item = items[i];
66
67 if (item.path === path) {
68 return i;
69 }
70 }
71
72 return -1;
73 },
74
75 push({
76 path,
77 state
78 }) {
79 interrupt();
80 const id = (0, _nonSecure.nanoid)(); // When a new entry is pushed, all the existing entries after index will be inaccessible
81 // So we remove any existing entries after the current index to clean them up
82
83 items = items.slice(0, index + 1);
84 items.push({
85 path,
86 state,
87 id
88 });
89 index = items.length - 1; // We pass empty string for title because it's ignored in all browsers except safari
90 // We don't store state object in history.state because:
91 // - browsers have limits on how big it can be, and we don't control the size
92 // - while not recommended, there could be non-serializable data in state
93
94 window.history.pushState({
95 id
96 }, '', path);
97 },
98
99 replace({
100 path,
101 state
102 }) {
103 var _window$history$state2, _window$history$state3;
104
105 interrupt();
106 const id = (_window$history$state2 = (_window$history$state3 = window.history.state) === null || _window$history$state3 === void 0 ? void 0 : _window$history$state3.id) !== null && _window$history$state2 !== void 0 ? _window$history$state2 : (0, _nonSecure.nanoid)();
107
108 if (items.length) {
109 items[index] = {
110 path,
111 state,
112 id
113 };
114 } else {
115 // This is the first time any state modifications are done
116 // So we need to push the entry as there's nothing to replace
117 items.push({
118 path,
119 state,
120 id
121 });
122 }
123
124 window.history.replaceState({
125 id
126 }, '', path);
127 },
128
129 // `history.go(n)` is asynchronous, there are couple of things to keep in mind:
130 // - it won't do anything if we can't go `n` steps, the `popstate` event won't fire.
131 // - each `history.go(n)` call will trigger a separate `popstate` event with correct location.
132 // - the `popstate` event fires before the next frame after calling `history.go(n)`.
133 // This method differs from `history.go(n)` in the sense that it'll go back as many steps it can.
134 go(n) {
135 interrupt();
136
137 if (n > 0) {
138 // We shouldn't go forward more than available index
139 n = Math.min(n, items.length - 1);
140 } else if (n < 0) {
141 // We shouldn't go back more than the 0 index
142 // Otherwise we'll exit the page
143 n = index + n < 0 ? -index : n;
144 }
145
146 if (n === 0) {
147 return;
148 }
149
150 index += n; // When we call `history.go`, `popstate` will fire when there's history to go back to
151 // So we need to somehow handle following cases:
152 // - There's history to go back, `history.go` is called, and `popstate` fires
153 // - `history.go` is called multiple times, we need to resolve on respective `popstate`
154 // - No history to go back, but `history.go` was called, browser has no API to detect it
155
156 return new Promise((resolve, reject) => {
157 const done = interrupted => {
158 clearTimeout(timer);
159
160 if (interrupted) {
161 reject(new Error('History was changed during navigation.'));
162 return;
163 } // There seems to be a bug in Chrome regarding updating the title
164 // If we set a title just before calling `history.go`, the title gets lost
165 // However the value of `document.title` is still what we set it to
166 // It's just not displayed in the tab bar
167 // To update the tab bar, we need to reset the title to something else first (e.g. '')
168 // And set the title to what it was before so it gets applied
169 // It won't work without setting it to empty string coz otherwise title isn't changing
170 // Which means that the browser won't do anything after setting the title
171
172
173 const {
174 title
175 } = window.document;
176 window.document.title = '';
177 window.document.title = title;
178 resolve();
179 };
180
181 pending.push({
182 ref: done,
183 cb: done
184 }); // If navigation didn't happen within 100ms, assume that it won't happen
185 // This may not be accurate, but hopefully it won't take so much time
186 // In Chrome, navigation seems to happen instantly in next microtask
187 // But on Firefox, it seems to take much longer, around 50ms from our testing
188 // We're using a hacky timeout since there doesn't seem to be way to know for sure
189
190 const timer = setTimeout(() => {
191 const index = pending.findIndex(it => it.ref === done);
192
193 if (index > -1) {
194 pending[index].cb();
195 pending.splice(index, 1);
196 }
197 }, 100);
198
199 const onPopState = () => {
200 const last = pending.pop();
201 window.removeEventListener('popstate', onPopState);
202 last === null || last === void 0 ? void 0 : last.cb();
203 };
204
205 window.addEventListener('popstate', onPopState);
206 window.history.go(n);
207 });
208 },
209
210 // The `popstate` event is triggered when history changes, except `pushState` and `replaceState`
211 // If we call `history.go(n)` ourselves, we don't want it to trigger the listener
212 // Here we normalize it so that only external changes (e.g. user pressing back/forward) trigger the listener
213 listen(listener) {
214 const onPopState = () => {
215 if (pending.length) {
216 // This was triggered by `history.go(n)`, we shouldn't call the listener
217 return;
218 }
219
220 listener();
221 };
222
223 window.addEventListener('popstate', onPopState);
224 return () => window.removeEventListener('popstate', onPopState);
225 }
226
227 };
228 return history;
229};
230/**
231 * Find the matching navigation state that changed between 2 navigation states
232 * e.g.: a -> b -> c -> d and a -> b -> c -> e -> f, if history in b changed, b is the matching state
233 */
234
235
236const findMatchingState = (a, b) => {
237 if (a === undefined || b === undefined || a.key !== b.key) {
238 return [undefined, undefined];
239 } // Tab and drawer will have `history` property, but stack will have history in `routes`
240
241
242 const aHistoryLength = a.history ? a.history.length : a.routes.length;
243 const bHistoryLength = b.history ? b.history.length : b.routes.length;
244 const aRoute = a.routes[a.index];
245 const bRoute = b.routes[b.index];
246 const aChildState = aRoute.state;
247 const bChildState = bRoute.state; // Stop here if this is the state object that changed:
248 // - history length is different
249 // - focused routes are different
250 // - one of them doesn't have child state
251 // - child state keys are different
252
253 if (aHistoryLength !== bHistoryLength || aRoute.key !== bRoute.key || aChildState === undefined || bChildState === undefined || aChildState.key !== bChildState.key) {
254 return [a, b];
255 }
256
257 return findMatchingState(aChildState, bChildState);
258};
259/**
260 * Run async function in series as it's called.
261 */
262
263
264const series = cb => {
265 // Whether we're currently handling a callback
266 let handling = false;
267 let queue = [];
268
269 const callback = async () => {
270 try {
271 if (handling) {
272 // If we're currently handling a previous event, wait before handling this one
273 // Add the callback to the beginning of the queue
274 queue.unshift(callback);
275 return;
276 }
277
278 handling = true;
279 await cb();
280 } finally {
281 handling = false;
282
283 if (queue.length) {
284 // If we have queued items, handle the last one
285 const last = queue.pop();
286 last === null || last === void 0 ? void 0 : last();
287 }
288 }
289 };
290
291 return callback;
292};
293
294let isUsingLinking = false;
295
296function useLinking(ref, {
297 independent,
298 enabled = true,
299 config,
300 getStateFromPath = _core.getStateFromPath,
301 getPathFromState = _core.getPathFromState,
302 getActionFromState = _core.getActionFromState
303}) {
304 React.useEffect(() => {
305 if (independent) {
306 return undefined;
307 }
308
309 if (enabled !== false && isUsingLinking) {
310 throw new Error(['Looks like you have configured linking in multiple places. This is likely an error since URL integration should only be handled in one place to avoid conflicts. Make sure that:', "- You are not using both 'linking' prop and 'useLinking'", "- You don't have 'useLinking' in multiple components"].join('\n').trim());
311 } else {
312 isUsingLinking = enabled !== false;
313 }
314
315 return () => {
316 isUsingLinking = false;
317 };
318 });
319 const [history] = React.useState(createMemoryHistory); // We store these options in ref to avoid re-creating getInitialState and re-subscribing listeners
320 // This lets user avoid wrapping the items in `React.useCallback` or `React.useMemo`
321 // Not re-creating `getInitialState` is important coz it makes it easier for the user to use in an effect
322
323 const enabledRef = React.useRef(enabled);
324 const configRef = React.useRef(config);
325 const getStateFromPathRef = React.useRef(getStateFromPath);
326 const getPathFromStateRef = React.useRef(getPathFromState);
327 const getActionFromStateRef = React.useRef(getActionFromState);
328 React.useEffect(() => {
329 enabledRef.current = enabled;
330 configRef.current = config;
331 getStateFromPathRef.current = getStateFromPath;
332 getPathFromStateRef.current = getPathFromState;
333 getActionFromStateRef.current = getActionFromState;
334 });
335 const server = React.useContext(_ServerContext.default);
336 const getInitialState = React.useCallback(() => {
337 let value;
338
339 if (enabledRef.current) {
340 var _server$location;
341
342 const location = (_server$location = server === null || server === void 0 ? void 0 : server.location) !== null && _server$location !== void 0 ? _server$location : typeof window !== 'undefined' ? window.location : undefined;
343 const path = location ? location.pathname + location.search : undefined;
344
345 if (path) {
346 value = getStateFromPathRef.current(path, configRef.current);
347 }
348 }
349
350 const thenable = {
351 then(onfulfilled) {
352 return Promise.resolve(onfulfilled ? onfulfilled(value) : value);
353 },
354
355 catch() {
356 return thenable;
357 }
358
359 };
360 return thenable; // eslint-disable-next-line react-hooks/exhaustive-deps
361 }, []);
362 const previousIndexRef = React.useRef(undefined);
363 const previousStateRef = React.useRef(undefined);
364 const pendingPopStatePathRef = React.useRef(undefined);
365 React.useEffect(() => {
366 previousIndexRef.current = history.index;
367 return history.listen(() => {
368 var _previousIndexRef$cur;
369
370 const navigation = ref.current;
371
372 if (!navigation || !enabled) {
373 return;
374 }
375
376 const path = location.pathname + location.search;
377 const index = history.index;
378 const previousIndex = (_previousIndexRef$cur = previousIndexRef.current) !== null && _previousIndexRef$cur !== void 0 ? _previousIndexRef$cur : 0;
379 previousIndexRef.current = index;
380 pendingPopStatePathRef.current = path; // When browser back/forward is clicked, we first need to check if state object for this index exists
381 // If it does we'll reset to that state object
382 // Otherwise, we'll handle it like a regular deep link
383
384 const record = history.get(index);
385
386 if ((record === null || record === void 0 ? void 0 : record.path) === path && record !== null && record !== void 0 && record.state) {
387 navigation.resetRoot(record.state);
388 return;
389 }
390
391 const state = getStateFromPathRef.current(path, configRef.current); // We should only dispatch an action when going forward
392 // Otherwise the action will likely add items to history, which would mess things up
393
394 if (state) {
395 // Make sure that the routes in the state exist in the root navigator
396 // Otherwise there's an error in the linking configuration
397 const rootState = navigation.getRootState();
398
399 if (state.routes.some(r => !(rootState !== null && rootState !== void 0 && rootState.routeNames.includes(r.name)))) {
400 console.warn("The navigation state parsed from the URL contains routes not present in the root navigator. This usually means that the linking configuration doesn't match the navigation structure. See https://reactnavigation.org/docs/configuring-links for more details on how to specify a linking configuration.");
401 return;
402 }
403
404 if (index > previousIndex) {
405 const action = getActionFromStateRef.current(state, configRef.current);
406
407 if (action !== undefined) {
408 try {
409 navigation.dispatch(action);
410 } catch (e) {
411 // Ignore any errors from deep linking.
412 // This could happen in case of malformed links, navigation object not being initialized etc.
413 console.warn(`An error occurred when trying to handle the link '${path}': ${e.message}`);
414 }
415 } else {
416 navigation.resetRoot(state);
417 }
418 } else {
419 navigation.resetRoot(state);
420 }
421 } else {
422 // if current path didn't return any state, we should revert to initial state
423 navigation.resetRoot(state);
424 }
425 });
426 }, [enabled, history, ref]);
427 React.useEffect(() => {
428 var _ref$current;
429
430 if (!enabled) {
431 return;
432 }
433
434 if (ref.current) {
435 // We need to record the current metadata on the first render if they aren't set
436 // This will allow the initial state to be in the history entry
437 const state = ref.current.getRootState();
438
439 if (state) {
440 var _route$path;
441
442 const route = (0, _core.findFocusedRoute)(state);
443 const path = (_route$path = route === null || route === void 0 ? void 0 : route.path) !== null && _route$path !== void 0 ? _route$path : getPathFromStateRef.current(state, configRef.current);
444
445 if (previousStateRef.current === undefined) {
446 previousStateRef.current = state;
447 }
448
449 history.replace({
450 path,
451 state
452 });
453 }
454 }
455
456 const onStateChange = async () => {
457 var _route$path2;
458
459 const navigation = ref.current;
460
461 if (!navigation || !enabled) {
462 return;
463 }
464
465 const previousState = previousStateRef.current;
466 const state = navigation.getRootState();
467 const pendingPath = pendingPopStatePathRef.current;
468 const route = (0, _core.findFocusedRoute)(state);
469 const path = (_route$path2 = route === null || route === void 0 ? void 0 : route.path) !== null && _route$path2 !== void 0 ? _route$path2 : getPathFromStateRef.current(state, configRef.current);
470 previousStateRef.current = state;
471 pendingPopStatePathRef.current = undefined; // To detect the kind of state change, we need to:
472 // - Find the common focused navigation state in previous and current state
473 // - If only the route keys changed, compare history/routes.length to check if we go back/forward/replace
474 // - If no common focused navigation state found, it's a replace
475
476 const [previousFocusedState, focusedState] = findMatchingState(previousState, state);
477
478 if (previousFocusedState && focusedState && // We should only handle push/pop if path changed from what was in last `popstate`
479 // Otherwise it's likely a change triggered by `popstate`
480 path !== pendingPath) {
481 const historyDelta = (focusedState.history ? focusedState.history.length : focusedState.routes.length) - (previousFocusedState.history ? previousFocusedState.history.length : previousFocusedState.routes.length);
482
483 if (historyDelta > 0) {
484 // If history length is increased, we should pushState
485 // Note that path might not actually change here, for example, drawer open should pushState
486 history.push({
487 path,
488 state
489 });
490 } else if (historyDelta < 0) {
491 // If history length is decreased, i.e. entries were removed, we want to go back
492 const nextIndex = history.backIndex({
493 path
494 });
495 const currentIndex = history.index;
496
497 try {
498 if (nextIndex !== -1 && nextIndex < currentIndex) {
499 // An existing entry for this path exists and it's less than current index, go back to that
500 await history.go(nextIndex - currentIndex);
501 } else {
502 // We couldn't find an existing entry to go back to, so we'll go back by the delta
503 // This won't be correct if multiple routes were pushed in one go before
504 // Usually this shouldn't happen and this is a fallback for that
505 await history.go(historyDelta);
506 } // Store the updated state as well as fix the path if incorrect
507
508
509 history.replace({
510 path,
511 state
512 });
513 } catch (e) {// The navigation was interrupted
514 }
515 } else {
516 // If history length is unchanged, we want to replaceState
517 history.replace({
518 path,
519 state
520 });
521 }
522 } else {
523 // If no common navigation state was found, assume it's a replace
524 // This would happen if the user did a reset/conditionally changed navigators
525 history.replace({
526 path,
527 state
528 });
529 }
530 }; // We debounce onStateChange coz we don't want multiple state changes to be handled at one time
531 // This could happen since `history.go(n)` is asynchronous
532 // If `pushState` or `replaceState` were called before `history.go(n)` completes, it'll mess stuff up
533
534
535 return (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.addListener('state', series(onStateChange));
536 });
537 return {
538 getInitialState
539 };
540}
541//# sourceMappingURL=useLinking.js.map
\No newline at end of file