UNPKG

20.1 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = useLinking;
7
8var React = _interopRequireWildcard(require("react"));
9
10var _core = require("@react-navigation/core");
11
12var _nonSecure = require("nanoid/non-secure");
13
14var _ServerContext = _interopRequireDefault(require("./ServerContext"));
15
16function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
18function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
19
20function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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 enabled = true,
298 config,
299 getStateFromPath = _core.getStateFromPath,
300 getPathFromState = _core.getPathFromState,
301 getActionFromState = _core.getActionFromState
302}) {
303 React.useEffect(() => {
304 if (enabled !== false && isUsingLinking) {
305 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());
306 } else {
307 isUsingLinking = enabled !== false;
308 }
309
310 return () => {
311 isUsingLinking = false;
312 };
313 });
314 const [history] = React.useState(createMemoryHistory); // We store these options in ref to avoid re-creating getInitialState and re-subscribing listeners
315 // This lets user avoid wrapping the items in `React.useCallback` or `React.useMemo`
316 // Not re-creating `getInitialState` is important coz it makes it easier for the user to use in an effect
317
318 const enabledRef = React.useRef(enabled);
319 const configRef = React.useRef(config);
320 const getStateFromPathRef = React.useRef(getStateFromPath);
321 const getActionFromStateRef = React.useRef(getActionFromState);
322 const getPathFromStateRef = React.useRef(getPathFromState);
323 React.useEffect(() => {
324 enabledRef.current = enabled;
325 configRef.current = config;
326 getStateFromPathRef.current = getStateFromPath;
327 getActionFromStateRef.current = getActionFromState;
328 getPathFromStateRef.current = getPathFromState;
329 });
330 const server = React.useContext(_ServerContext.default);
331 const getInitialState = React.useCallback(() => {
332 let value;
333
334 if (enabledRef.current) {
335 var _server$location;
336
337 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;
338 const path = location ? location.pathname + location.search : undefined;
339
340 if (path) {
341 value = getStateFromPathRef.current(path, configRef.current);
342 }
343 }
344
345 const thenable = {
346 then(onfulfilled) {
347 return Promise.resolve(onfulfilled ? onfulfilled(value) : value);
348 },
349
350 catch() {
351 return thenable;
352 }
353
354 };
355 return thenable; // eslint-disable-next-line react-hooks/exhaustive-deps
356 }, []);
357 const previousIndexRef = React.useRef(undefined);
358 const previousStateRef = React.useRef(undefined);
359 const pendingPopStatePathRef = React.useRef(undefined);
360 React.useEffect(() => {
361 previousIndexRef.current = history.index;
362 return history.listen(() => {
363 var _previousIndexRef$cur;
364
365 const navigation = ref.current;
366
367 if (!navigation || !enabled) {
368 return;
369 }
370
371 const path = location.pathname + location.search;
372 const index = history.index;
373 const previousIndex = (_previousIndexRef$cur = previousIndexRef.current) !== null && _previousIndexRef$cur !== void 0 ? _previousIndexRef$cur : 0;
374 previousIndexRef.current = index;
375 pendingPopStatePathRef.current = path; // When browser back/forward is clicked, we first need to check if state object for this index exists
376 // If it does we'll reset to that state object
377 // Otherwise, we'll handle it like a regular deep link
378
379 const record = history.get(index);
380
381 if ((record === null || record === void 0 ? void 0 : record.path) === path && record !== null && record !== void 0 && record.state) {
382 navigation.resetRoot(record.state);
383 return;
384 }
385
386 const state = getStateFromPathRef.current(path, configRef.current); // We should only dispatch an action when going forward
387 // Otherwise the action will likely add items to history, which would mess things up
388
389 if (state) {
390 // Make sure that the routes in the state exist in the root navigator
391 // Otherwise there's an error in the linking configuration
392 const rootState = navigation.getRootState();
393
394 if (state.routes.some(r => !(rootState !== null && rootState !== void 0 && rootState.routeNames.includes(r.name)))) {
395 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.");
396 return;
397 }
398
399 if (index > previousIndex) {
400 const action = getActionFromStateRef.current(state, configRef.current);
401
402 if (action !== undefined) {
403 try {
404 navigation.dispatch(action);
405 } catch (e) {
406 // Ignore any errors from deep linking.
407 // This could happen in case of malformed links, navigation object not being initialized etc.
408 console.warn("An error occurred when trying to handle the link '".concat(path, "': ").concat(e.message));
409 }
410 } else {
411 navigation.resetRoot(state);
412 }
413 } else {
414 navigation.resetRoot(state);
415 }
416 } else {
417 // if current path didn't return any state, we should revert to initial state
418 navigation.resetRoot(state);
419 }
420 });
421 }, [enabled, history, ref]);
422 React.useEffect(() => {
423 var _ref$current;
424
425 if (!enabled) {
426 return;
427 }
428
429 if (ref.current) {
430 // We need to record the current metadata on the first render if they aren't set
431 // This will allow the initial state to be in the history entry
432 const state = ref.current.getRootState();
433
434 if (state) {
435 const path = getPathFromStateRef.current(state, configRef.current);
436
437 if (previousStateRef.current === undefined) {
438 previousStateRef.current = state;
439 }
440
441 history.replace({
442 path,
443 state
444 });
445 }
446 }
447
448 const onStateChange = async () => {
449 const navigation = ref.current;
450
451 if (!navigation || !enabled) {
452 return;
453 }
454
455 const previousState = previousStateRef.current;
456 const state = navigation.getRootState();
457 const pendingPath = pendingPopStatePathRef.current;
458 const path = getPathFromStateRef.current(state, configRef.current);
459 previousStateRef.current = state;
460 pendingPopStatePathRef.current = undefined; // To detect the kind of state change, we need to:
461 // - Find the common focused navigation state in previous and current state
462 // - If only the route keys changed, compare history/routes.length to check if we go back/forward/replace
463 // - If no common focused navigation state found, it's a replace
464
465 const [previousFocusedState, focusedState] = findMatchingState(previousState, state);
466
467 if (previousFocusedState && focusedState && // We should only handle push/pop if path changed from what was in last `popstate`
468 // Otherwise it's likely a change triggered by `popstate`
469 path !== pendingPath) {
470 const historyDelta = (focusedState.history ? focusedState.history.length : focusedState.routes.length) - (previousFocusedState.history ? previousFocusedState.history.length : previousFocusedState.routes.length);
471
472 if (historyDelta > 0) {
473 // If history length is increased, we should pushState
474 // Note that path might not actually change here, for example, drawer open should pushState
475 history.push({
476 path,
477 state
478 });
479 } else if (historyDelta < 0) {
480 // If history length is decreased, i.e. entries were removed, we want to go back
481 const nextIndex = history.backIndex({
482 path
483 });
484 const currentIndex = history.index;
485
486 try {
487 if (nextIndex !== -1 && nextIndex < currentIndex) {
488 // An existing entry for this path exists and it's less than current index, go back to that
489 await history.go(nextIndex - currentIndex);
490 } else {
491 // We couldn't find an existing entry to go back to, so we'll go back by the delta
492 // This won't be correct if multiple routes were pushed in one go before
493 // Usually this shouldn't happen and this is a fallback for that
494 await history.go(historyDelta);
495 } // Store the updated state as well as fix the path if incorrect
496
497
498 history.replace({
499 path,
500 state
501 });
502 } catch (e) {// The navigation was interrupted
503 }
504 } else {
505 // If history length is unchanged, we want to replaceState
506 history.replace({
507 path,
508 state
509 });
510 }
511 } else {
512 // If no common navigation state was found, assume it's a replace
513 // This would happen if the user did a reset/conditionally changed navigators
514 history.replace({
515 path,
516 state
517 });
518 }
519 }; // We debounce onStateChange coz we don't want multiple state changes to be handled at one time
520 // This could happen since `history.go(n)` is asynchronous
521 // If `pushState` or `replaceState` were called before `history.go(n)` completes, it'll mess stuff up
522
523
524 return (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.addListener('state', series(onStateChange));
525 });
526 return {
527 getInitialState
528 };
529}
530//# sourceMappingURL=useLinking.js.map
\No newline at end of file