@tinkoff/router 0.1.79 → 0.1.80

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.
@@ -1,6 +1,12 @@
1
- import React from 'react';
1
+ import type { FunctionComponent, ReactNode } from 'react';
2
+ import type { Url } from '@tinkoff/url';
2
3
  import type { AbstractRouter } from '../../router/abstract';
3
- export declare const Provider: React.FunctionComponent<{
4
+ import type { NavigationRoute } from '../../types';
5
+ export declare const Provider: FunctionComponent<{
4
6
  router: AbstractRouter;
5
- children?: React.ReactNode;
7
+ serverState?: {
8
+ currentRoute: NavigationRoute;
9
+ currentUrl: Url;
10
+ };
11
+ children?: ReactNode;
6
12
  }>;
@@ -8,8 +8,9 @@ import each from '@tinkoff/utils/array/each';
8
8
  import find from '@tinkoff/utils/array/find';
9
9
  import findIndex from '@tinkoff/utils/array/findIndex';
10
10
  import { jsx } from 'react/jsx-runtime';
11
- import { createContext, useState, useContext, useCallback, isValidElement, cloneElement } from 'react';
12
- import { useIsomorphicLayoutEffect, useShallowEqual } from '@tinkoff/react-hooks';
11
+ import { useSyncExternalStore } from 'use-sync-external-store/shim';
12
+ import { createContext, useContext, useCallback, isValidElement, cloneElement } from 'react';
13
+ import { useShallowEqual } from '@tinkoff/react-hooks';
13
14
 
14
15
  const PARAMETER_DELIMITER = ':';
15
16
  const WILDCARD_REGEXP = /\*/;
@@ -173,6 +174,14 @@ class AbstractRouter {
173
174
  // same as getCurrentRoute
174
175
  return (_b = (_a = this.currentNavigation) === null || _a === void 0 ? void 0 : _a.url) !== null && _b !== void 0 ? _b : (_c = this.lastNavigation) === null || _c === void 0 ? void 0 : _c.url;
175
176
  }
177
+ getLastRoute() {
178
+ var _a;
179
+ return (_a = this.lastNavigation) === null || _a === void 0 ? void 0 : _a.to;
180
+ }
181
+ getLastUrl() {
182
+ var _a;
183
+ return (_a = this.lastNavigation) === null || _a === void 0 ? void 0 : _a.url;
184
+ }
176
185
  commitNavigation(navigation) {
177
186
  logger.debug({
178
187
  event: 'commit-navigation',
@@ -182,9 +191,9 @@ class AbstractRouter {
182
191
  // in case we came from history do not history back to prevent infinity recursive calls
183
192
  this.history.save(navigation);
184
193
  }
185
- this.runSyncHooks('change', navigation);
186
194
  this.lastNavigation = navigation;
187
195
  this.currentNavigation = null;
196
+ this.runSyncHooks('change', navigation);
188
197
  }
189
198
  async updateCurrentRoute(updateRouteOptions) {
190
199
  return this.internalUpdateCurrentRoute(updateRouteOptions, {});
@@ -1126,17 +1135,10 @@ const RouterContext = createContext(null);
1126
1135
  const RouteContext = createContext(null);
1127
1136
  const UrlContext = createContext(null);
1128
1137
 
1129
- const Provider = ({ router, children }) => {
1130
- const [state, setState] = useState({
1131
- route: router.getCurrentRoute(),
1132
- url: router.getCurrentUrl(),
1133
- });
1134
- useIsomorphicLayoutEffect(() => {
1135
- return router.registerSyncHook('change', ({ to, url }) => {
1136
- setState({ route: to, url });
1137
- });
1138
- }, [router]);
1139
- return (jsx(RouterContext.Provider, Object.assign({ value: router }, { children: jsx(RouteContext.Provider, Object.assign({ value: state.route }, { children: jsx(UrlContext.Provider, Object.assign({ value: state.url }, { children: children }), void 0) }), void 0) }), void 0));
1138
+ const Provider = ({ router, serverState, children }) => {
1139
+ const route = useSyncExternalStore((cb) => router.registerSyncHook('change', cb), () => router.getLastRoute(), serverState ? () => serverState.currentRoute : () => router.getLastRoute());
1140
+ const url = useSyncExternalStore((cb) => router.registerSyncHook('change', cb), () => router.getLastUrl(), serverState ? () => serverState.currentUrl : () => router.getLastUrl());
1141
+ return (jsx(RouterContext.Provider, Object.assign({ value: router }, { children: jsx(RouteContext.Provider, Object.assign({ value: route }, { children: jsx(UrlContext.Provider, Object.assign({ value: url }, { children: children }), void 0) }), void 0) }), void 0));
1140
1142
  };
1141
1143
  Provider.displayName = 'Provider';
1142
1144
 
package/lib/index.es.js CHANGED
@@ -8,8 +8,9 @@ import each from '@tinkoff/utils/array/each';
8
8
  import find from '@tinkoff/utils/array/find';
9
9
  import findIndex from '@tinkoff/utils/array/findIndex';
10
10
  import { jsx } from 'react/jsx-runtime';
11
- import { createContext, useState, useContext, useCallback, isValidElement, cloneElement } from 'react';
12
- import { useIsomorphicLayoutEffect, useShallowEqual } from '@tinkoff/react-hooks';
11
+ import { useSyncExternalStore } from 'use-sync-external-store/shim';
12
+ import { createContext, useContext, useCallback, isValidElement, cloneElement } from 'react';
13
+ import { useShallowEqual } from '@tinkoff/react-hooks';
13
14
 
14
15
  const PARAMETER_DELIMITER = ':';
15
16
  const WILDCARD_REGEXP = /\*/;
@@ -173,6 +174,14 @@ class AbstractRouter {
173
174
  // same as getCurrentRoute
174
175
  return (_b = (_a = this.currentNavigation) === null || _a === void 0 ? void 0 : _a.url) !== null && _b !== void 0 ? _b : (_c = this.lastNavigation) === null || _c === void 0 ? void 0 : _c.url;
175
176
  }
177
+ getLastRoute() {
178
+ var _a;
179
+ return (_a = this.lastNavigation) === null || _a === void 0 ? void 0 : _a.to;
180
+ }
181
+ getLastUrl() {
182
+ var _a;
183
+ return (_a = this.lastNavigation) === null || _a === void 0 ? void 0 : _a.url;
184
+ }
176
185
  commitNavigation(navigation) {
177
186
  logger.debug({
178
187
  event: 'commit-navigation',
@@ -182,9 +191,9 @@ class AbstractRouter {
182
191
  // in case we came from history do not history back to prevent infinity recursive calls
183
192
  this.history.save(navigation);
184
193
  }
185
- this.runSyncHooks('change', navigation);
186
194
  this.lastNavigation = navigation;
187
195
  this.currentNavigation = null;
196
+ this.runSyncHooks('change', navigation);
188
197
  }
189
198
  async updateCurrentRoute(updateRouteOptions) {
190
199
  return this.internalUpdateCurrentRoute(updateRouteOptions, {});
@@ -1054,17 +1063,10 @@ const RouterContext = createContext(null);
1054
1063
  const RouteContext = createContext(null);
1055
1064
  const UrlContext = createContext(null);
1056
1065
 
1057
- const Provider = ({ router, children }) => {
1058
- const [state, setState] = useState({
1059
- route: router.getCurrentRoute(),
1060
- url: router.getCurrentUrl(),
1061
- });
1062
- useIsomorphicLayoutEffect(() => {
1063
- return router.registerSyncHook('change', ({ to, url }) => {
1064
- setState({ route: to, url });
1065
- });
1066
- }, [router]);
1067
- return (jsx(RouterContext.Provider, Object.assign({ value: router }, { children: jsx(RouteContext.Provider, Object.assign({ value: state.route }, { children: jsx(UrlContext.Provider, Object.assign({ value: state.url }, { children: children }), void 0) }), void 0) }), void 0));
1066
+ const Provider = ({ router, serverState, children }) => {
1067
+ const route = useSyncExternalStore((cb) => router.registerSyncHook('change', cb), () => router.getLastRoute(), serverState ? () => serverState.currentRoute : () => router.getLastRoute());
1068
+ const url = useSyncExternalStore((cb) => router.registerSyncHook('change', cb), () => router.getLastUrl(), serverState ? () => serverState.currentUrl : () => router.getLastUrl());
1069
+ return (jsx(RouterContext.Provider, Object.assign({ value: router }, { children: jsx(RouteContext.Provider, Object.assign({ value: route }, { children: jsx(UrlContext.Provider, Object.assign({ value: url }, { children: children }), void 0) }), void 0) }), void 0));
1068
1070
  };
1069
1071
  Provider.displayName = 'Provider';
1070
1072
 
package/lib/index.js CHANGED
@@ -12,6 +12,7 @@ var each = require('@tinkoff/utils/array/each');
12
12
  var find = require('@tinkoff/utils/array/find');
13
13
  var findIndex = require('@tinkoff/utils/array/findIndex');
14
14
  var jsxRuntime = require('react/jsx-runtime');
15
+ var shim = require('use-sync-external-store/shim');
15
16
  var react = require('react');
16
17
  var reactHooks = require('@tinkoff/react-hooks');
17
18
 
@@ -188,6 +189,14 @@ class AbstractRouter {
188
189
  // same as getCurrentRoute
189
190
  return (_b = (_a = this.currentNavigation) === null || _a === void 0 ? void 0 : _a.url) !== null && _b !== void 0 ? _b : (_c = this.lastNavigation) === null || _c === void 0 ? void 0 : _c.url;
190
191
  }
192
+ getLastRoute() {
193
+ var _a;
194
+ return (_a = this.lastNavigation) === null || _a === void 0 ? void 0 : _a.to;
195
+ }
196
+ getLastUrl() {
197
+ var _a;
198
+ return (_a = this.lastNavigation) === null || _a === void 0 ? void 0 : _a.url;
199
+ }
191
200
  commitNavigation(navigation) {
192
201
  exports.logger.debug({
193
202
  event: 'commit-navigation',
@@ -197,9 +206,9 @@ class AbstractRouter {
197
206
  // in case we came from history do not history back to prevent infinity recursive calls
198
207
  this.history.save(navigation);
199
208
  }
200
- this.runSyncHooks('change', navigation);
201
209
  this.lastNavigation = navigation;
202
210
  this.currentNavigation = null;
211
+ this.runSyncHooks('change', navigation);
203
212
  }
204
213
  async updateCurrentRoute(updateRouteOptions) {
205
214
  return this.internalUpdateCurrentRoute(updateRouteOptions, {});
@@ -1069,17 +1078,10 @@ const RouterContext = react.createContext(null);
1069
1078
  const RouteContext = react.createContext(null);
1070
1079
  const UrlContext = react.createContext(null);
1071
1080
 
1072
- const Provider = ({ router, children }) => {
1073
- const [state, setState] = react.useState({
1074
- route: router.getCurrentRoute(),
1075
- url: router.getCurrentUrl(),
1076
- });
1077
- reactHooks.useIsomorphicLayoutEffect(() => {
1078
- return router.registerSyncHook('change', ({ to, url }) => {
1079
- setState({ route: to, url });
1080
- });
1081
- }, [router]);
1082
- return (jsxRuntime.jsx(RouterContext.Provider, Object.assign({ value: router }, { children: jsxRuntime.jsx(RouteContext.Provider, Object.assign({ value: state.route }, { children: jsxRuntime.jsx(UrlContext.Provider, Object.assign({ value: state.url }, { children: children }), void 0) }), void 0) }), void 0));
1081
+ const Provider = ({ router, serverState, children }) => {
1082
+ const route = shim.useSyncExternalStore((cb) => router.registerSyncHook('change', cb), () => router.getLastRoute(), serverState ? () => serverState.currentRoute : () => router.getLastRoute());
1083
+ const url = shim.useSyncExternalStore((cb) => router.registerSyncHook('change', cb), () => router.getLastUrl(), serverState ? () => serverState.currentUrl : () => router.getLastUrl());
1084
+ return (jsxRuntime.jsx(RouterContext.Provider, Object.assign({ value: router }, { children: jsxRuntime.jsx(RouteContext.Provider, Object.assign({ value: route }, { children: jsxRuntime.jsx(UrlContext.Provider, Object.assign({ value: url }, { children: children }), void 0) }), void 0) }), void 0));
1083
1085
  };
1084
1086
  Provider.displayName = 'Provider';
1085
1087
 
@@ -41,6 +41,8 @@ export declare abstract class AbstractRouter {
41
41
  start(): Promise<void>;
42
42
  getCurrentRoute(): import("../types").NavigationRoute;
43
43
  getCurrentUrl(): Url;
44
+ getLastRoute(): import("../types").NavigationRoute;
45
+ getLastUrl(): Url;
44
46
  protected commitNavigation(navigation: Navigation): void;
45
47
  updateCurrentRoute(updateRouteOptions: UpdateCurrentRouteOptions): Promise<void>;
46
48
  protected internalUpdateCurrentRoute(updateRouteOptions: UpdateCurrentRouteOptions, { history }: InternalOptions): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinkoff/router",
3
- "version": "0.1.79",
3
+ "version": "0.1.80",
4
4
  "description": "router",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -24,7 +24,8 @@
24
24
  "dependencies": {
25
25
  "@tinkoff/react-hooks": "0.0.27",
26
26
  "@tinkoff/url": "0.7.39",
27
- "@tinkoff/utils": "^2.1.2"
27
+ "@tinkoff/utils": "^2.1.2",
28
+ "use-sync-external-store": "^1.2.0"
28
29
  },
29
30
  "peerDependencies": {
30
31
  "react": ">=16.14.0",
@@ -32,6 +33,7 @@
32
33
  },
33
34
  "sideEffects": false,
34
35
  "devDependencies": {
36
+ "@types/use-sync-external-store": "^0.0.3",
35
37
  "object-sizeof": "^1.6.1"
36
38
  },
37
39
  "license": "Apache-2.0"