@tinkoff/router 0.2.7 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/components/react/context.browser.js +7 -0
- package/lib/components/react/context.es.js +7 -0
- package/lib/components/react/context.js +13 -0
- package/lib/components/react/provider.browser.js +12 -0
- package/lib/components/react/provider.es.js +12 -0
- package/lib/components/react/provider.js +16 -0
- package/lib/components/react/useNavigate.browser.js +17 -0
- package/lib/components/react/useNavigate.es.js +17 -0
- package/lib/components/react/useNavigate.js +21 -0
- package/lib/components/react/useRoute.browser.js +8 -0
- package/lib/components/react/useRoute.es.js +8 -0
- package/lib/components/react/useRoute.js +12 -0
- package/lib/components/react/useRouter.browser.js +8 -0
- package/lib/components/react/useRouter.es.js +8 -0
- package/lib/components/react/useRouter.js +12 -0
- package/lib/components/react/useUrl.browser.js +8 -0
- package/lib/components/react/useUrl.es.js +8 -0
- package/lib/components/react/useUrl.js +12 -0
- package/lib/history/base.browser.js +11 -0
- package/lib/history/base.es.js +11 -0
- package/lib/history/base.js +15 -0
- package/lib/history/client.browser.js +121 -0
- package/lib/history/client.es.js +121 -0
- package/lib/history/client.js +125 -0
- package/lib/history/server.es.js +15 -0
- package/lib/history/server.js +19 -0
- package/lib/history/wrapper.browser.js +72 -0
- package/lib/history/wrapper.es.js +72 -0
- package/lib/history/wrapper.js +80 -0
- package/lib/index.browser.js +12 -1169
- package/lib/index.es.js +12 -1097
- package/lib/index.js +36 -1124
- package/lib/logger.browser.js +15 -0
- package/lib/logger.es.js +15 -0
- package/lib/logger.js +23 -0
- package/lib/router/abstract.browser.js +395 -0
- package/lib/router/abstract.es.js +395 -0
- package/lib/router/abstract.js +404 -0
- package/lib/router/browser.browser.js +166 -0
- package/lib/router/client.browser.js +116 -0
- package/lib/router/client.es.js +116 -0
- package/lib/router/client.js +120 -0
- package/lib/router/clientNoSpa.browser.js +17 -0
- package/lib/router/clientNoSpa.es.js +17 -0
- package/lib/router/clientNoSpa.js +21 -0
- package/lib/router/server.es.js +85 -0
- package/lib/router/server.js +89 -0
- package/lib/tree/constants.browser.js +6 -0
- package/lib/tree/constants.es.js +6 -0
- package/lib/tree/constants.js +13 -0
- package/lib/tree/tree.browser.js +148 -0
- package/lib/tree/tree.es.js +148 -0
- package/lib/tree/tree.js +158 -0
- package/lib/tree/utils.browser.js +77 -0
- package/lib/tree/utils.es.js +77 -0
- package/lib/tree/utils.js +90 -0
- package/lib/utils.browser.js +35 -0
- package/lib/utils.es.js +35 -0
- package/lib/utils.js +48 -0
- package/package.json +5 -6
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var react = require('react');
|
|
6
|
+
|
|
7
|
+
const RouterContext = react.createContext(null);
|
|
8
|
+
const RouteContext = react.createContext(null);
|
|
9
|
+
const UrlContext = react.createContext(null);
|
|
10
|
+
|
|
11
|
+
exports.RouteContext = RouteContext;
|
|
12
|
+
exports.RouterContext = RouterContext;
|
|
13
|
+
exports.UrlContext = UrlContext;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import { useSyncExternalStore } from 'use-sync-external-store/shim';
|
|
3
|
+
import { RouterContext, RouteContext, UrlContext } from './context.browser.js';
|
|
4
|
+
|
|
5
|
+
const Provider = ({ router, serverState, children }) => {
|
|
6
|
+
const route = useSyncExternalStore((cb) => router.registerSyncHook('change', cb), () => router.getLastRoute(), serverState ? () => serverState.currentRoute : () => router.getLastRoute());
|
|
7
|
+
const url = useSyncExternalStore((cb) => router.registerSyncHook('change', cb), () => router.getLastUrl(), serverState ? () => serverState.currentUrl : () => router.getLastUrl());
|
|
8
|
+
return (jsx(RouterContext.Provider, { value: router, children: jsx(RouteContext.Provider, { value: route, children: jsx(UrlContext.Provider, { value: url, children: children }) }) }));
|
|
9
|
+
};
|
|
10
|
+
Provider.displayName = 'Provider';
|
|
11
|
+
|
|
12
|
+
export { Provider };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import { useSyncExternalStore } from 'use-sync-external-store/shim';
|
|
3
|
+
import { RouterContext, RouteContext, UrlContext } from './context.es.js';
|
|
4
|
+
|
|
5
|
+
const Provider = ({ router, serverState, children }) => {
|
|
6
|
+
const route = useSyncExternalStore((cb) => router.registerSyncHook('change', cb), () => router.getLastRoute(), serverState ? () => serverState.currentRoute : () => router.getLastRoute());
|
|
7
|
+
const url = useSyncExternalStore((cb) => router.registerSyncHook('change', cb), () => router.getLastUrl(), serverState ? () => serverState.currentUrl : () => router.getLastUrl());
|
|
8
|
+
return (jsx(RouterContext.Provider, { value: router, children: jsx(RouteContext.Provider, { value: route, children: jsx(UrlContext.Provider, { value: url, children: children }) }) }));
|
|
9
|
+
};
|
|
10
|
+
Provider.displayName = 'Provider';
|
|
11
|
+
|
|
12
|
+
export { Provider };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
6
|
+
var shim = require('use-sync-external-store/shim');
|
|
7
|
+
var context = require('./context.js');
|
|
8
|
+
|
|
9
|
+
const Provider = ({ router, serverState, children }) => {
|
|
10
|
+
const route = shim.useSyncExternalStore((cb) => router.registerSyncHook('change', cb), () => router.getLastRoute(), serverState ? () => serverState.currentRoute : () => router.getLastRoute());
|
|
11
|
+
const url = shim.useSyncExternalStore((cb) => router.registerSyncHook('change', cb), () => router.getLastUrl(), serverState ? () => serverState.currentUrl : () => router.getLastUrl());
|
|
12
|
+
return (jsxRuntime.jsx(context.RouterContext.Provider, { value: router, children: jsxRuntime.jsx(context.RouteContext.Provider, { value: route, children: jsxRuntime.jsx(context.UrlContext.Provider, { value: url, children: children }) }) }));
|
|
13
|
+
};
|
|
14
|
+
Provider.displayName = 'Provider';
|
|
15
|
+
|
|
16
|
+
exports.Provider = Provider;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useCallback } from 'react';
|
|
2
|
+
import { useShallowEqual } from '@tinkoff/react-hooks';
|
|
3
|
+
import { useRouter } from './useRouter.browser.js';
|
|
4
|
+
|
|
5
|
+
const convertToNavigateOptions = (options) => {
|
|
6
|
+
return typeof options === 'string' ? { url: options } : options;
|
|
7
|
+
};
|
|
8
|
+
const useNavigate = (rootOptions) => {
|
|
9
|
+
const router = useRouter();
|
|
10
|
+
const rootOpts = useShallowEqual(convertToNavigateOptions(rootOptions));
|
|
11
|
+
return useCallback((specificOptions) => {
|
|
12
|
+
const opts = rootOpts !== null && rootOpts !== void 0 ? rootOpts : convertToNavigateOptions(specificOptions);
|
|
13
|
+
return router.navigate(opts);
|
|
14
|
+
}, [rootOpts, router]);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export { useNavigate };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useCallback } from 'react';
|
|
2
|
+
import { useShallowEqual } from '@tinkoff/react-hooks';
|
|
3
|
+
import { useRouter } from './useRouter.es.js';
|
|
4
|
+
|
|
5
|
+
const convertToNavigateOptions = (options) => {
|
|
6
|
+
return typeof options === 'string' ? { url: options } : options;
|
|
7
|
+
};
|
|
8
|
+
const useNavigate = (rootOptions) => {
|
|
9
|
+
const router = useRouter();
|
|
10
|
+
const rootOpts = useShallowEqual(convertToNavigateOptions(rootOptions));
|
|
11
|
+
return useCallback((specificOptions) => {
|
|
12
|
+
const opts = rootOpts !== null && rootOpts !== void 0 ? rootOpts : convertToNavigateOptions(specificOptions);
|
|
13
|
+
return router.navigate(opts);
|
|
14
|
+
}, [rootOpts, router]);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export { useNavigate };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var react = require('react');
|
|
6
|
+
var reactHooks = require('@tinkoff/react-hooks');
|
|
7
|
+
var useRouter = require('./useRouter.js');
|
|
8
|
+
|
|
9
|
+
const convertToNavigateOptions = (options) => {
|
|
10
|
+
return typeof options === 'string' ? { url: options } : options;
|
|
11
|
+
};
|
|
12
|
+
const useNavigate = (rootOptions) => {
|
|
13
|
+
const router = useRouter.useRouter();
|
|
14
|
+
const rootOpts = reactHooks.useShallowEqual(convertToNavigateOptions(rootOptions));
|
|
15
|
+
return react.useCallback((specificOptions) => {
|
|
16
|
+
const opts = rootOpts !== null && rootOpts !== void 0 ? rootOpts : convertToNavigateOptions(specificOptions);
|
|
17
|
+
return router.navigate(opts);
|
|
18
|
+
}, [rootOpts, router]);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
exports.useNavigate = useNavigate;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var react = require('react');
|
|
6
|
+
var context = require('./context.js');
|
|
7
|
+
|
|
8
|
+
const useRoute = () => {
|
|
9
|
+
return react.useContext(context.RouteContext);
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
exports.useRoute = useRoute;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var react = require('react');
|
|
6
|
+
var context = require('./context.js');
|
|
7
|
+
|
|
8
|
+
const useRouter = () => {
|
|
9
|
+
return react.useContext(context.RouterContext);
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
exports.useRouter = useRouter;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var react = require('react');
|
|
6
|
+
var context = require('./context.js');
|
|
7
|
+
|
|
8
|
+
const useUrl = () => {
|
|
9
|
+
return react.useContext(context.UrlContext);
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
exports.useUrl = useUrl;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
class History {
|
|
6
|
+
init(navigation) { }
|
|
7
|
+
listen(listener) {
|
|
8
|
+
this.listener = listener;
|
|
9
|
+
}
|
|
10
|
+
setTree(tree) {
|
|
11
|
+
this.tree = tree;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
exports.History = History;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { History } from './base.browser.js';
|
|
2
|
+
import { wrapHistory } from './wrapper.browser.js';
|
|
3
|
+
|
|
4
|
+
const isHistoryState = (state) => {
|
|
5
|
+
return state && typeof state.key === 'string';
|
|
6
|
+
};
|
|
7
|
+
const generateKey = (navigation) => {
|
|
8
|
+
const { to } = navigation;
|
|
9
|
+
if (to) {
|
|
10
|
+
return `${to.name}_${to.path}`;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const generateState = (navigation, currentState) => {
|
|
14
|
+
const key = generateKey(navigation);
|
|
15
|
+
let { type } = navigation;
|
|
16
|
+
if (navigation.replace && currentState) {
|
|
17
|
+
type = currentState.type === type ? type : 'navigate';
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
key,
|
|
21
|
+
type,
|
|
22
|
+
navigateState: navigation.navigateState,
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
class ClientHistory extends History {
|
|
26
|
+
constructor() {
|
|
27
|
+
super();
|
|
28
|
+
this.currentIndex = 0;
|
|
29
|
+
this.historyWrapper = wrapHistory({
|
|
30
|
+
onNavigate: ({ url, replace, navigateState }) => {
|
|
31
|
+
this.listener({
|
|
32
|
+
url,
|
|
33
|
+
replace,
|
|
34
|
+
navigateState,
|
|
35
|
+
});
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
init(navigation) {
|
|
40
|
+
var _a;
|
|
41
|
+
this.currentState = isHistoryState((_a = window.history) === null || _a === void 0 ? void 0 : _a.state)
|
|
42
|
+
? window.history.state
|
|
43
|
+
: generateState(navigation);
|
|
44
|
+
this.historyWrapper.init(this.currentState);
|
|
45
|
+
this.historyWrapper.subscribe(async ({ path, state }) => {
|
|
46
|
+
var _a, _b;
|
|
47
|
+
try {
|
|
48
|
+
let navigationType;
|
|
49
|
+
let navigateState;
|
|
50
|
+
if (isHistoryState(state)) {
|
|
51
|
+
const { key: prevKey, type: prevType } = this.currentState;
|
|
52
|
+
const { key, type } = state;
|
|
53
|
+
this.currentState = state;
|
|
54
|
+
navigateState = state.navigateState;
|
|
55
|
+
if (key === prevKey &&
|
|
56
|
+
(type === 'updateCurrentRoute' || prevType === 'updateCurrentRoute')) {
|
|
57
|
+
navigationType = 'updateCurrentRoute';
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
navigationType = 'navigate';
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
// if it is not HistoryState than it is probably not a state from @tinkoff/router so reset it
|
|
65
|
+
this.currentIndex = 0;
|
|
66
|
+
}
|
|
67
|
+
await this.listener({
|
|
68
|
+
type: navigationType,
|
|
69
|
+
history: true,
|
|
70
|
+
url: path,
|
|
71
|
+
navigateState,
|
|
72
|
+
});
|
|
73
|
+
(_a = this.goPromiseResolve) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
(_b = this.goPromiseReject) === null || _b === void 0 ? void 0 : _b.call(this, err);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
save(navigation) {
|
|
81
|
+
const { replace, url } = navigation;
|
|
82
|
+
if (!replace) {
|
|
83
|
+
this.currentIndex++;
|
|
84
|
+
}
|
|
85
|
+
this.currentState = generateState(navigation, this.currentState);
|
|
86
|
+
this.historyWrapper.navigate({
|
|
87
|
+
path: url.path,
|
|
88
|
+
replace,
|
|
89
|
+
state: this.currentState,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
go(to, options) {
|
|
93
|
+
var _a;
|
|
94
|
+
const index = this.currentIndex + to;
|
|
95
|
+
if (index < 0) {
|
|
96
|
+
if (options === null || options === void 0 ? void 0 : options.historyFallback) {
|
|
97
|
+
return this.listener({
|
|
98
|
+
url: options.historyFallback,
|
|
99
|
+
type: 'navigate',
|
|
100
|
+
history: false,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
const historyFallbackRoute = (_a = this.tree) === null || _a === void 0 ? void 0 : _a.getHistoryFallback(window.location.pathname);
|
|
104
|
+
if (historyFallbackRoute) {
|
|
105
|
+
return this.listener({
|
|
106
|
+
url: historyFallbackRoute.actualPath,
|
|
107
|
+
type: 'navigate',
|
|
108
|
+
history: false,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const promise = new Promise((resolve, reject) => {
|
|
113
|
+
this.goPromiseResolve = resolve;
|
|
114
|
+
this.goPromiseReject = reject;
|
|
115
|
+
});
|
|
116
|
+
this.historyWrapper.history(to);
|
|
117
|
+
return promise;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export { ClientHistory };
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { History } from './base.es.js';
|
|
2
|
+
import { wrapHistory } from './wrapper.es.js';
|
|
3
|
+
|
|
4
|
+
const isHistoryState = (state) => {
|
|
5
|
+
return state && typeof state.key === 'string';
|
|
6
|
+
};
|
|
7
|
+
const generateKey = (navigation) => {
|
|
8
|
+
const { to } = navigation;
|
|
9
|
+
if (to) {
|
|
10
|
+
return `${to.name}_${to.path}`;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const generateState = (navigation, currentState) => {
|
|
14
|
+
const key = generateKey(navigation);
|
|
15
|
+
let { type } = navigation;
|
|
16
|
+
if (navigation.replace && currentState) {
|
|
17
|
+
type = currentState.type === type ? type : 'navigate';
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
key,
|
|
21
|
+
type,
|
|
22
|
+
navigateState: navigation.navigateState,
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
class ClientHistory extends History {
|
|
26
|
+
constructor() {
|
|
27
|
+
super();
|
|
28
|
+
this.currentIndex = 0;
|
|
29
|
+
this.historyWrapper = wrapHistory({
|
|
30
|
+
onNavigate: ({ url, replace, navigateState }) => {
|
|
31
|
+
this.listener({
|
|
32
|
+
url,
|
|
33
|
+
replace,
|
|
34
|
+
navigateState,
|
|
35
|
+
});
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
init(navigation) {
|
|
40
|
+
var _a;
|
|
41
|
+
this.currentState = isHistoryState((_a = window.history) === null || _a === void 0 ? void 0 : _a.state)
|
|
42
|
+
? window.history.state
|
|
43
|
+
: generateState(navigation);
|
|
44
|
+
this.historyWrapper.init(this.currentState);
|
|
45
|
+
this.historyWrapper.subscribe(async ({ path, state }) => {
|
|
46
|
+
var _a, _b;
|
|
47
|
+
try {
|
|
48
|
+
let navigationType;
|
|
49
|
+
let navigateState;
|
|
50
|
+
if (isHistoryState(state)) {
|
|
51
|
+
const { key: prevKey, type: prevType } = this.currentState;
|
|
52
|
+
const { key, type } = state;
|
|
53
|
+
this.currentState = state;
|
|
54
|
+
navigateState = state.navigateState;
|
|
55
|
+
if (key === prevKey &&
|
|
56
|
+
(type === 'updateCurrentRoute' || prevType === 'updateCurrentRoute')) {
|
|
57
|
+
navigationType = 'updateCurrentRoute';
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
navigationType = 'navigate';
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
// if it is not HistoryState than it is probably not a state from @tinkoff/router so reset it
|
|
65
|
+
this.currentIndex = 0;
|
|
66
|
+
}
|
|
67
|
+
await this.listener({
|
|
68
|
+
type: navigationType,
|
|
69
|
+
history: true,
|
|
70
|
+
url: path,
|
|
71
|
+
navigateState,
|
|
72
|
+
});
|
|
73
|
+
(_a = this.goPromiseResolve) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
(_b = this.goPromiseReject) === null || _b === void 0 ? void 0 : _b.call(this, err);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
save(navigation) {
|
|
81
|
+
const { replace, url } = navigation;
|
|
82
|
+
if (!replace) {
|
|
83
|
+
this.currentIndex++;
|
|
84
|
+
}
|
|
85
|
+
this.currentState = generateState(navigation, this.currentState);
|
|
86
|
+
this.historyWrapper.navigate({
|
|
87
|
+
path: url.path,
|
|
88
|
+
replace,
|
|
89
|
+
state: this.currentState,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
go(to, options) {
|
|
93
|
+
var _a;
|
|
94
|
+
const index = this.currentIndex + to;
|
|
95
|
+
if (index < 0) {
|
|
96
|
+
if (options === null || options === void 0 ? void 0 : options.historyFallback) {
|
|
97
|
+
return this.listener({
|
|
98
|
+
url: options.historyFallback,
|
|
99
|
+
type: 'navigate',
|
|
100
|
+
history: false,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
const historyFallbackRoute = (_a = this.tree) === null || _a === void 0 ? void 0 : _a.getHistoryFallback(window.location.pathname);
|
|
104
|
+
if (historyFallbackRoute) {
|
|
105
|
+
return this.listener({
|
|
106
|
+
url: historyFallbackRoute.actualPath,
|
|
107
|
+
type: 'navigate',
|
|
108
|
+
history: false,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const promise = new Promise((resolve, reject) => {
|
|
113
|
+
this.goPromiseResolve = resolve;
|
|
114
|
+
this.goPromiseReject = reject;
|
|
115
|
+
});
|
|
116
|
+
this.historyWrapper.history(to);
|
|
117
|
+
return promise;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export { ClientHistory };
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var base = require('./base.js');
|
|
6
|
+
var wrapper = require('./wrapper.js');
|
|
7
|
+
|
|
8
|
+
const isHistoryState = (state) => {
|
|
9
|
+
return state && typeof state.key === 'string';
|
|
10
|
+
};
|
|
11
|
+
const generateKey = (navigation) => {
|
|
12
|
+
const { to } = navigation;
|
|
13
|
+
if (to) {
|
|
14
|
+
return `${to.name}_${to.path}`;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const generateState = (navigation, currentState) => {
|
|
18
|
+
const key = generateKey(navigation);
|
|
19
|
+
let { type } = navigation;
|
|
20
|
+
if (navigation.replace && currentState) {
|
|
21
|
+
type = currentState.type === type ? type : 'navigate';
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
key,
|
|
25
|
+
type,
|
|
26
|
+
navigateState: navigation.navigateState,
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
class ClientHistory extends base.History {
|
|
30
|
+
constructor() {
|
|
31
|
+
super();
|
|
32
|
+
this.currentIndex = 0;
|
|
33
|
+
this.historyWrapper = wrapper.wrapHistory({
|
|
34
|
+
onNavigate: ({ url, replace, navigateState }) => {
|
|
35
|
+
this.listener({
|
|
36
|
+
url,
|
|
37
|
+
replace,
|
|
38
|
+
navigateState,
|
|
39
|
+
});
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
init(navigation) {
|
|
44
|
+
var _a;
|
|
45
|
+
this.currentState = isHistoryState((_a = window.history) === null || _a === void 0 ? void 0 : _a.state)
|
|
46
|
+
? window.history.state
|
|
47
|
+
: generateState(navigation);
|
|
48
|
+
this.historyWrapper.init(this.currentState);
|
|
49
|
+
this.historyWrapper.subscribe(async ({ path, state }) => {
|
|
50
|
+
var _a, _b;
|
|
51
|
+
try {
|
|
52
|
+
let navigationType;
|
|
53
|
+
let navigateState;
|
|
54
|
+
if (isHistoryState(state)) {
|
|
55
|
+
const { key: prevKey, type: prevType } = this.currentState;
|
|
56
|
+
const { key, type } = state;
|
|
57
|
+
this.currentState = state;
|
|
58
|
+
navigateState = state.navigateState;
|
|
59
|
+
if (key === prevKey &&
|
|
60
|
+
(type === 'updateCurrentRoute' || prevType === 'updateCurrentRoute')) {
|
|
61
|
+
navigationType = 'updateCurrentRoute';
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
navigationType = 'navigate';
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
// if it is not HistoryState than it is probably not a state from @tinkoff/router so reset it
|
|
69
|
+
this.currentIndex = 0;
|
|
70
|
+
}
|
|
71
|
+
await this.listener({
|
|
72
|
+
type: navigationType,
|
|
73
|
+
history: true,
|
|
74
|
+
url: path,
|
|
75
|
+
navigateState,
|
|
76
|
+
});
|
|
77
|
+
(_a = this.goPromiseResolve) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
(_b = this.goPromiseReject) === null || _b === void 0 ? void 0 : _b.call(this, err);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
save(navigation) {
|
|
85
|
+
const { replace, url } = navigation;
|
|
86
|
+
if (!replace) {
|
|
87
|
+
this.currentIndex++;
|
|
88
|
+
}
|
|
89
|
+
this.currentState = generateState(navigation, this.currentState);
|
|
90
|
+
this.historyWrapper.navigate({
|
|
91
|
+
path: url.path,
|
|
92
|
+
replace,
|
|
93
|
+
state: this.currentState,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
go(to, options) {
|
|
97
|
+
var _a;
|
|
98
|
+
const index = this.currentIndex + to;
|
|
99
|
+
if (index < 0) {
|
|
100
|
+
if (options === null || options === void 0 ? void 0 : options.historyFallback) {
|
|
101
|
+
return this.listener({
|
|
102
|
+
url: options.historyFallback,
|
|
103
|
+
type: 'navigate',
|
|
104
|
+
history: false,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
const historyFallbackRoute = (_a = this.tree) === null || _a === void 0 ? void 0 : _a.getHistoryFallback(window.location.pathname);
|
|
108
|
+
if (historyFallbackRoute) {
|
|
109
|
+
return this.listener({
|
|
110
|
+
url: historyFallbackRoute.actualPath,
|
|
111
|
+
type: 'navigate',
|
|
112
|
+
history: false,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const promise = new Promise((resolve, reject) => {
|
|
117
|
+
this.goPromiseResolve = resolve;
|
|
118
|
+
this.goPromiseReject = reject;
|
|
119
|
+
});
|
|
120
|
+
this.historyWrapper.history(to);
|
|
121
|
+
return promise;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
exports.ClientHistory = ClientHistory;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { logger } from '../logger.es.js';
|
|
2
|
+
import { History } from './base.es.js';
|
|
3
|
+
|
|
4
|
+
class ServerHistory extends History {
|
|
5
|
+
save() { }
|
|
6
|
+
go() {
|
|
7
|
+
logger.warn({
|
|
8
|
+
event: 'history.server',
|
|
9
|
+
message: 'Trying to change history on server',
|
|
10
|
+
});
|
|
11
|
+
return Promise.resolve();
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { ServerHistory };
|