spinning-router 0.0.3 → 0.0.5

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.
@@ -18,48 +18,63 @@ const DefaultErrorPage_1 = require("./components/DefaultErrorPage");
18
18
  const DefaultNotFoundPage_1 = require("./components/DefaultNotFoundPage");
19
19
  const matchRoute_1 = require("./matchRoute");
20
20
  const SpinningRouter = ({ routes, errorPage = DefaultErrorPage_1.DefaultErrorPage, notFoundPage = DefaultNotFoundPage_1.DefaultNotFoundPage, loadingIndicator = DefaultLoadingIndicator_1.DefaultLoadingIndicator }) => {
21
- const [hash, setHash] = (0, react_1.useState)(() => location.hash.substring(1));
21
+ const [path, setPath] = (0, react_1.useState)(() => location.hash.substring(1));
22
+ const [match, setMatch] = (0, react_1.useState)("");
22
23
  const [element, setElement] = (0, react_1.useState)(undefined);
23
24
  const [overlay, setOverlay] = (0, react_1.useState)(undefined);
24
25
  const [parameters, setParameters] = (0, react_1.useState)({});
25
26
  const invokeRoute = (path) => __awaiter(void 0, void 0, void 0, function* () {
26
- setOverlay(loadingIndicator);
27
27
  try {
28
28
  const match = yield (0, matchRoute_1.matchRoute)(routes, path);
29
29
  if (match) {
30
+ setMatch(match.path);
30
31
  setElement(match.element);
31
32
  setParameters(match.parameters);
32
33
  }
33
34
  else {
35
+ setMatch("");
36
+ window.scrollTo(0, 0);
34
37
  setElement(notFoundPage);
35
38
  setParameters({});
36
39
  }
37
- window.scrollTo(0, 0);
38
40
  }
39
41
  catch (error) {
42
+ setMatch("");
40
43
  window.scrollTo(0, 0);
41
44
  setElement(errorPage({ error }));
42
45
  setParameters({});
43
46
  }
47
+ });
48
+ const invokeRouteWithSpinner = () => __awaiter(void 0, void 0, void 0, function* () {
49
+ const path = location.hash.substring(1);
50
+ setOverlay(loadingIndicator);
51
+ setPath(path);
52
+ try {
53
+ yield invokeRoute(path);
54
+ window.scrollTo(0, 0);
55
+ }
44
56
  finally {
45
57
  setOverlay(undefined);
46
58
  }
47
59
  });
48
60
  (0, react_1.useEffect)(() => {
49
- invokeRoute(hash);
61
+ invokeRouteWithSpinner();
50
62
  }, []);
51
63
  (0, react_1.useEffect)(() => {
52
64
  const updateHash = () => __awaiter(void 0, void 0, void 0, function* () {
53
- setHash(location.hash.substring(1));
65
+ yield invokeRouteWithSpinner();
66
+ });
67
+ const doSoftRefresh = () => __awaiter(void 0, void 0, void 0, function* () {
54
68
  yield invokeRoute(location.hash.substring(1));
55
69
  });
56
70
  window.addEventListener("hashchange", updateHash);
57
- window.addEventListener("softRefresh", updateHash);
71
+ window.addEventListener("softRefresh", doSoftRefresh);
58
72
  return () => {
59
73
  window.removeEventListener("hashchange", updateHash);
60
- window.removeEventListener("softRefresh", updateHash);
74
+ window.removeEventListener("softRefresh", doSoftRefresh);
61
75
  };
62
76
  }, []);
63
- return ((0, jsx_runtime_1.jsxs)(Location_1.Location.Provider, { value: { path: hash, match: "", parameters }, children: [element, overlay] }));
77
+ window.SpinningRouterLocation = { path, match, parameters };
78
+ return ((0, jsx_runtime_1.jsxs)(Location_1.Location.Provider, { value: Object.assign({}, window.SpinningRouterLocation), children: [element, overlay] }));
64
79
  };
65
80
  exports.SpinningRouter = SpinningRouter;
package/dist/link.js CHANGED
@@ -1,14 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.link = exports.unsafeLink = void 0;
4
- const Location_1 = require("./Location");
5
- const react_1 = require("react");
6
4
  const unsafeLink = (path, values) => {
7
5
  let result = [];
8
6
  for (let part of path.split("/")) {
9
7
  if (part.startsWith(":")) {
10
8
  const key = part.substring(1);
11
- let value = values && values[key] !== undefined ? values[key] : (0, react_1.useContext)(Location_1.Location).parameters[key];
9
+ let value = values && values[key] !== undefined ? values[key] : window.SpinningRouterLocation.parameters[key];
12
10
  result.push(encodeURIComponent(value));
13
11
  }
14
12
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spinning-router",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "n/a",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",