1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = useLinking;
|
7 |
|
8 | var _core = require("@react-navigation/core");
|
9 |
|
10 | var _nonSecure = require("nanoid/non-secure");
|
11 |
|
12 | var React = _interopRequireWildcard(require("react"));
|
13 |
|
14 | var _ServerContext = _interopRequireDefault(require("./ServerContext"));
|
15 |
|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
17 |
|
18 | function _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 |
|
20 | function _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 |
|
22 | const createMemoryHistory = () => {
|
23 | let index = 0;
|
24 | let items = [];
|
25 |
|
26 |
|
27 | const pending = [];
|
28 |
|
29 | const interrupt = () => {
|
30 |
|
31 |
|
32 |
|
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 |
|
45 |
|
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 |
|
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)();
|
81 |
|
82 |
|
83 | items = items.slice(0, index + 1);
|
84 | items.push({
|
85 | path,
|
86 | state,
|
87 | id
|
88 | });
|
89 | index = items.length - 1;
|
90 |
|
91 |
|
92 |
|
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 |
|
116 |
|
117 | items.push({
|
118 | path,
|
119 | state,
|
120 | id
|
121 | });
|
122 | }
|
123 |
|
124 | window.history.replaceState({
|
125 | id
|
126 | }, '', path);
|
127 | },
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | go(n) {
|
135 | interrupt();
|
136 |
|
137 | if (n > 0) {
|
138 |
|
139 | n = Math.min(n, items.length - 1);
|
140 | } else if (n < 0) {
|
141 |
|
142 |
|
143 | n = index + n < 0 ? -index : n;
|
144 | }
|
145 |
|
146 | if (n === 0) {
|
147 | return;
|
148 | }
|
149 |
|
150 | index += n;
|
151 |
|
152 |
|
153 |
|
154 |
|
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 | }
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
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 | });
|
185 |
|
186 |
|
187 |
|
188 |
|
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 |
|
211 |
|
212 |
|
213 | listen(listener) {
|
214 | const onPopState = () => {
|
215 | if (pending.length) {
|
216 |
|
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 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 | const findMatchingState = (a, b) => {
|
237 | if (a === undefined || b === undefined || a.key !== b.key) {
|
238 | return [undefined, undefined];
|
239 | }
|
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;
|
248 |
|
249 |
|
250 |
|
251 |
|
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 |
|
261 |
|
262 |
|
263 |
|
264 | const series = cb => {
|
265 |
|
266 | let handling = false;
|
267 | let queue = [];
|
268 |
|
269 | const callback = async () => {
|
270 | try {
|
271 | if (handling) {
|
272 |
|
273 |
|
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 |
|
285 | const last = queue.pop();
|
286 | last === null || last === void 0 ? void 0 : last();
|
287 | }
|
288 | }
|
289 | };
|
290 |
|
291 | return callback;
|
292 | };
|
293 |
|
294 | let isUsingLinking = false;
|
295 |
|
296 | function 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);
|
320 |
|
321 |
|
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;
|
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;
|
381 |
|
382 |
|
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);
|
392 |
|
393 |
|
394 | if (state) {
|
395 |
|
396 |
|
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 |
|
412 |
|
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 |
|
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 |
|
436 |
|
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;
|
472 |
|
473 |
|
474 |
|
475 |
|
476 | const [previousFocusedState, focusedState] = findMatchingState(previousState, state);
|
477 |
|
478 | if (previousFocusedState && focusedState &&
|
479 |
|
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 |
|
485 |
|
486 | history.push({
|
487 | path,
|
488 | state
|
489 | });
|
490 | } else if (historyDelta < 0) {
|
491 |
|
492 | const nextIndex = history.backIndex({
|
493 | path
|
494 | });
|
495 | const currentIndex = history.index;
|
496 |
|
497 | try {
|
498 | if (nextIndex !== -1 && nextIndex < currentIndex) {
|
499 |
|
500 | await history.go(nextIndex - currentIndex);
|
501 | } else {
|
502 |
|
503 |
|
504 |
|
505 | await history.go(historyDelta);
|
506 | }
|
507 |
|
508 |
|
509 | history.replace({
|
510 | path,
|
511 | state
|
512 | });
|
513 | } catch (e) {
|
514 | }
|
515 | } else {
|
516 |
|
517 | history.replace({
|
518 | path,
|
519 | state
|
520 | });
|
521 | }
|
522 | } else {
|
523 |
|
524 |
|
525 | history.replace({
|
526 | path,
|
527 | state
|
528 | });
|
529 | }
|
530 | };
|
531 |
|
532 |
|
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 |
|
\ | No newline at end of file |