1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = useLinking;
|
7 |
|
8 | var React = _interopRequireWildcard(require("react"));
|
9 |
|
10 | var _core = require("@react-navigation/core");
|
11 |
|
12 | var _nonSecure = require("nanoid/non-secure");
|
13 |
|
14 | var _ServerContext = _interopRequireDefault(require("./ServerContext"));
|
15 |
|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
17 |
|
18 | function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
19 |
|
20 | function _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 |
|
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 | 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);
|
315 |
|
316 |
|
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;
|
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;
|
376 |
|
377 |
|
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);
|
387 |
|
388 |
|
389 | if (state) {
|
390 |
|
391 |
|
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 |
|
407 |
|
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 |
|
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 |
|
431 |
|
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;
|
461 |
|
462 |
|
463 |
|
464 |
|
465 | const [previousFocusedState, focusedState] = findMatchingState(previousState, state);
|
466 |
|
467 | if (previousFocusedState && focusedState &&
|
468 |
|
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 |
|
474 |
|
475 | history.push({
|
476 | path,
|
477 | state
|
478 | });
|
479 | } else if (historyDelta < 0) {
|
480 |
|
481 | const nextIndex = history.backIndex({
|
482 | path
|
483 | });
|
484 | const currentIndex = history.index;
|
485 |
|
486 | try {
|
487 | if (nextIndex !== -1 && nextIndex < currentIndex) {
|
488 |
|
489 | await history.go(nextIndex - currentIndex);
|
490 | } else {
|
491 |
|
492 |
|
493 |
|
494 | await history.go(historyDelta);
|
495 | }
|
496 |
|
497 |
|
498 | history.replace({
|
499 | path,
|
500 | state
|
501 | });
|
502 | } catch (e) {
|
503 | }
|
504 | } else {
|
505 |
|
506 | history.replace({
|
507 | path,
|
508 | state
|
509 | });
|
510 | }
|
511 | } else {
|
512 |
|
513 |
|
514 | history.replace({
|
515 | path,
|
516 | state
|
517 | });
|
518 | }
|
519 | };
|
520 |
|
521 |
|
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 |
|
\ | No newline at end of file |