chayns-api 3.0.1 → 3.1.0-beta.1

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.
Files changed (142) hide show
  1. package/dist/cjs/calls/index.js +4 -1
  2. package/dist/cjs/calls/visibilityChangeListener.js +4 -4
  3. package/dist/cjs/components/ChaynsProvider.js +34 -6
  4. package/dist/cjs/components/withHydrationBoundary.js +2 -2
  5. package/dist/cjs/constants/index.js +0 -22
  6. package/dist/cjs/contexts/HistoryLayerContext.js +89 -0
  7. package/dist/cjs/contexts/index.js +38 -0
  8. package/dist/cjs/handler/history/FrameHistoryLayer.js +100 -0
  9. package/dist/cjs/handler/history/HistoryLayer.js +321 -0
  10. package/dist/cjs/handler/history/index.js +19 -0
  11. package/dist/cjs/hooks/history.js +454 -0
  12. package/dist/cjs/hooks/index.js +62 -1
  13. package/dist/cjs/host/ChaynsHost.js +113 -54
  14. package/dist/cjs/host/iframe/HostIframe.js +70 -5
  15. package/dist/cjs/host/module/ModuleHost.js +50 -44
  16. package/dist/cjs/index.js +139 -6
  17. package/dist/cjs/types/history.js +1 -0
  18. package/dist/cjs/umd.index.js +2 -2
  19. package/dist/cjs/utils/EventBus.js +33 -0
  20. package/dist/cjs/{util → utils}/appStorage.js +2 -2
  21. package/dist/cjs/utils/equality.js +19 -0
  22. package/dist/cjs/utils/history/BlockRegistry.js +153 -0
  23. package/dist/cjs/utils/history/NavigationQueue.js +389 -0
  24. package/dist/cjs/utils/history/layerTree.js +32 -0
  25. package/dist/cjs/utils/history/nativeBackHandling.js +61 -0
  26. package/dist/cjs/utils/history/navigationIndex.js +74 -0
  27. package/dist/cjs/utils/history/rootLayer.js +213 -0
  28. package/dist/cjs/utils/history/segments.js +15 -0
  29. package/dist/cjs/utils/history/stateProjector.js +156 -0
  30. package/dist/cjs/utils/history/url.js +47 -0
  31. package/dist/cjs/utils/history/window.js +9 -0
  32. package/dist/cjs/wrapper/AppWrapper.js +24 -24
  33. package/dist/cjs/wrapper/FrameWrapper.js +35 -2
  34. package/dist/cjs/wrapper/ModuleFederationWrapper.js +2 -0
  35. package/dist/cjs/wrapper/StaticChaynsApi.js +1 -1
  36. package/dist/esm/calls/index.js +2 -0
  37. package/dist/esm/calls/visibilityChangeListener.js +1 -1
  38. package/dist/esm/components/ChaynsProvider.js +34 -6
  39. package/dist/esm/components/withHydrationBoundary.js +1 -1
  40. package/dist/esm/constants/index.js +1 -3
  41. package/dist/esm/contexts/HistoryLayerContext.js +76 -0
  42. package/dist/esm/contexts/index.js +3 -0
  43. package/dist/esm/handler/history/FrameHistoryLayer.js +105 -0
  44. package/dist/esm/handler/history/HistoryLayer.js +321 -0
  45. package/dist/esm/handler/history/index.js +2 -0
  46. package/dist/esm/hooks/history.js +428 -0
  47. package/dist/esm/hooks/index.js +2 -1
  48. package/dist/esm/host/ChaynsHost.js +113 -54
  49. package/dist/esm/host/iframe/HostIframe.js +70 -5
  50. package/dist/esm/host/module/ModuleHost.js +50 -44
  51. package/dist/esm/index.js +15 -6
  52. package/dist/esm/types/history.js +1 -0
  53. package/dist/esm/umd.index.js +2 -2
  54. package/dist/esm/utils/EventBus.js +31 -0
  55. package/dist/esm/{util → utils}/appStorage.js +1 -1
  56. package/dist/esm/utils/equality.js +12 -0
  57. package/dist/esm/utils/history/BlockRegistry.js +151 -0
  58. package/dist/esm/utils/history/NavigationQueue.js +386 -0
  59. package/dist/esm/utils/history/layerTree.js +24 -0
  60. package/dist/esm/utils/history/nativeBackHandling.js +59 -0
  61. package/dist/esm/utils/history/navigationIndex.js +62 -0
  62. package/dist/esm/utils/history/rootLayer.js +205 -0
  63. package/dist/esm/utils/history/segments.js +7 -0
  64. package/dist/esm/utils/history/stateProjector.js +147 -0
  65. package/dist/esm/utils/history/url.js +40 -0
  66. package/dist/esm/utils/history/window.js +3 -0
  67. package/dist/esm/wrapper/AppWrapper.js +6 -6
  68. package/dist/esm/wrapper/FrameWrapper.js +35 -2
  69. package/dist/esm/wrapper/ModuleFederationWrapper.js +2 -0
  70. package/dist/esm/wrapper/StaticChaynsApi.js +2 -1
  71. package/dist/types/calls/index.d.ts +5 -0
  72. package/dist/types/components/ChaynsProvider.d.ts +21 -0
  73. package/dist/types/constants/index.d.ts +0 -2
  74. package/dist/types/contexts/HistoryLayerContext.d.ts +33 -0
  75. package/dist/types/contexts/index.d.ts +3 -0
  76. package/dist/types/handler/history/FrameHistoryLayer.d.ts +99 -0
  77. package/dist/types/handler/history/HistoryLayer.d.ts +117 -0
  78. package/dist/types/handler/history/index.d.ts +2 -0
  79. package/dist/types/hooks/history.d.ts +89 -0
  80. package/dist/types/hooks/index.d.ts +1 -0
  81. package/dist/types/host/ChaynsHost.d.ts +12 -0
  82. package/dist/types/host/iframe/HostIframe.d.ts +4 -0
  83. package/dist/types/host/module/ModuleHost.d.ts +4 -0
  84. package/dist/types/index.d.ts +15 -6
  85. package/dist/types/types/IChaynsReact.d.ts +3 -0
  86. package/dist/types/types/history.d.ts +74 -0
  87. package/dist/types/umd.index.d.ts +2 -2
  88. package/dist/types/utils/EventBus.d.ts +10 -0
  89. package/dist/types/{util → utils}/collectCssChunks.d.ts +1 -1
  90. package/dist/types/utils/equality.d.ts +2 -0
  91. package/dist/types/utils/history/BlockRegistry.d.ts +45 -0
  92. package/dist/types/utils/history/NavigationQueue.d.ts +118 -0
  93. package/dist/types/utils/history/layerTree.d.ts +10 -0
  94. package/dist/types/utils/history/nativeBackHandling.d.ts +47 -0
  95. package/dist/types/utils/history/navigationIndex.d.ts +17 -0
  96. package/dist/types/utils/history/rootLayer.d.ts +42 -0
  97. package/dist/types/utils/history/segments.d.ts +2 -0
  98. package/dist/types/utils/history/stateProjector.d.ts +24 -0
  99. package/dist/types/utils/history/url.d.ts +17 -0
  100. package/dist/types/utils/history/window.d.ts +1 -0
  101. package/dist/types/wrapper/FrameWrapper.d.ts +1 -0
  102. package/dist/types/wrapper/StaticChaynsApi.d.ts +1 -0
  103. package/package.json +2 -1
  104. /package/dist/cjs/{constants → contexts}/hydrationContext.js +0 -0
  105. /package/dist/cjs/{constants → contexts}/moduleContext.js +0 -0
  106. /package/dist/cjs/{helper/apiListenerHelper.js → utils/apiListener.js} +0 -0
  107. /package/dist/cjs/{util → utils}/appCall.js +0 -0
  108. /package/dist/cjs/{util → utils}/bindChaynsApi.js +0 -0
  109. /package/dist/cjs/{util → utils}/collectCssChunks.js +0 -0
  110. /package/dist/cjs/{util → utils}/deviceHelper.js +0 -0
  111. /package/dist/cjs/{util → utils}/heightHelper.js +0 -0
  112. /package/dist/cjs/{util → utils}/initModuleFederationSharing.js +0 -0
  113. /package/dist/cjs/{util → utils}/is.js +0 -0
  114. /package/dist/cjs/{util → utils}/postIframeForm.js +0 -0
  115. /package/dist/cjs/{util → utils}/transferNestedFunctions.js +0 -0
  116. /package/dist/cjs/{util → utils}/url.js +0 -0
  117. /package/dist/esm/{constants → contexts}/hydrationContext.js +0 -0
  118. /package/dist/esm/{constants → contexts}/moduleContext.js +0 -0
  119. /package/dist/esm/{helper/apiListenerHelper.js → utils/apiListener.js} +0 -0
  120. /package/dist/esm/{util → utils}/appCall.js +0 -0
  121. /package/dist/esm/{util → utils}/bindChaynsApi.js +0 -0
  122. /package/dist/esm/{util → utils}/collectCssChunks.js +0 -0
  123. /package/dist/esm/{util → utils}/deviceHelper.js +0 -0
  124. /package/dist/esm/{util → utils}/heightHelper.js +0 -0
  125. /package/dist/esm/{util → utils}/initModuleFederationSharing.js +0 -0
  126. /package/dist/esm/{util → utils}/is.js +0 -0
  127. /package/dist/esm/{util → utils}/postIframeForm.js +0 -0
  128. /package/dist/esm/{util → utils}/transferNestedFunctions.js +0 -0
  129. /package/dist/esm/{util → utils}/url.js +0 -0
  130. /package/dist/types/{constants → contexts}/hydrationContext.d.ts +0 -0
  131. /package/dist/types/{constants → contexts}/moduleContext.d.ts +0 -0
  132. /package/dist/types/{helper/apiListenerHelper.d.ts → utils/apiListener.d.ts} +0 -0
  133. /package/dist/types/{util → utils}/appCall.d.ts +0 -0
  134. /package/dist/types/{util → utils}/appStorage.d.ts +0 -0
  135. /package/dist/types/{util → utils}/bindChaynsApi.d.ts +0 -0
  136. /package/dist/types/{util → utils}/deviceHelper.d.ts +0 -0
  137. /package/dist/types/{util → utils}/heightHelper.d.ts +0 -0
  138. /package/dist/types/{util → utils}/initModuleFederationSharing.d.ts +0 -0
  139. /package/dist/types/{util → utils}/is.d.ts +0 -0
  140. /package/dist/types/{util → utils}/postIframeForm.d.ts +0 -0
  141. /package/dist/types/{util → utils}/transferNestedFunctions.d.ts +0 -0
  142. /package/dist/types/{util → utils}/url.d.ts +0 -0
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = void 0;
7
7
  var _moduleWrapper = require("../components/moduleWrapper");
8
8
  var _IChaynsReact = require("../types/IChaynsReact");
9
- var _deviceHelper = _interopRequireDefault(require("../util/deviceHelper"));
9
+ var _deviceHelper = _interopRequireDefault(require("../utils/deviceHelper"));
10
10
  var _AppWrapper = require("./AppWrapper");
11
11
  var _FrameWrapper = require("./FrameWrapper");
12
12
  var _ModuleFederationWrapper = require("./ModuleFederationWrapper");
@@ -1,4 +1,5 @@
1
1
  import { moduleWrapper } from '../components/moduleWrapper';
2
+ import { getCurrentChaynsHistoryLayer } from '../contexts/HistoryLayerContext';
2
3
  export const addGeoLocationListener = (...args) => moduleWrapper.current.functions.addGeoLocationListener(...args);
3
4
  export const addScrollListener = (...args) => moduleWrapper.current.functions.addScrollListener(...args);
4
5
  export const addVisibilityChangeListener = (...args) => moduleWrapper.current.functions.addVisibilityChangeListener(...args);
@@ -54,6 +55,7 @@ export const createDialog = config => moduleWrapper.current.functions.createDial
54
55
  export const setOverlay = (...args) => moduleWrapper.current.functions.setOverlay(...args);
55
56
  export const addAnonymousAccount = () => moduleWrapper.current.functions.addAnonymousAccount();
56
57
  export const getUser = () => moduleWrapper.current.values.user;
58
+ export const getChaynsHistoryLayer = () => getCurrentChaynsHistoryLayer();
57
59
  export const getSite = () => moduleWrapper.current.values.site;
58
60
  export const getCurrentPage = () => moduleWrapper.current.values.currentPage;
59
61
  export const getDevice = () => moduleWrapper.current.values.device;
@@ -1,4 +1,4 @@
1
- import { addApiListener, dispatchApiEvent, removeApiListener } from '../helper/apiListenerHelper';
1
+ import { addApiListener, dispatchApiEvent, removeApiListener } from '../utils/apiListener';
2
2
  const key = 'visibilityChangeListener';
3
3
  const handleVisibilityChange = () => {
4
4
  dispatchApiEvent(key, {
@@ -3,13 +3,15 @@ import htmlEscape from 'htmlescape';
3
3
  import React, { useEffect, useRef, useState } from 'react';
4
4
  import { useIsomorphicLayoutEffect } from '../hooks/useIsomorphicLayoutEffect';
5
5
  import { AppName } from '../types/IChaynsReact';
6
- import getDeviceInfo from '../util/deviceHelper';
6
+ import getDeviceInfo from '../utils/deviceHelper';
7
7
  import { AppWrapper } from '../wrapper/AppWrapper';
8
8
  import { FrameWrapper } from '../wrapper/FrameWrapper';
9
9
  import { ModuleFederationWrapper } from '../wrapper/ModuleFederationWrapper';
10
10
  import { SsrWrapper } from '../wrapper/SsrWrapper';
11
11
  import { ChaynsContext } from './ChaynsContext';
12
12
  import { addModuleWrapper, moduleWrapper, removeModuleWrapper } from './moduleWrapper';
13
+ import { ChaynsHistoryLayerProvider, useChaynsHistoryLayerContext } from '../contexts/HistoryLayerContext';
14
+ import { getOrInitRootChaynsHistoryLayer } from '../utils/history/rootLayer';
13
15
  const isServer = typeof window === 'undefined';
14
16
  const InitialDataProvider = React.memo(t0 => {
15
17
  const $ = _c(6);
@@ -59,11 +61,18 @@ const ChaynsProvider = ({
59
61
  customFunctions,
60
62
  renderedByServer,
61
63
  isModule,
62
- chaynsApiId
64
+ chaynsApiId,
65
+ historyLayer,
66
+ history,
67
+ isHistoryDisabled,
68
+ segmentCount
63
69
  }) => {
64
- var _ref, _crypto, _customWrapper$curren, _customWrapper$curren2;
70
+ var _ref, _crypto, _customWrapper$curren, _customWrapper$curren3, _customWrapper$curren4;
65
71
  const customWrapper = useRef(null);
66
72
  const idRef = useRef((_ref = chaynsApiId !== null && chaynsApiId !== void 0 ? chaynsApiId : (_crypto = crypto) === null || _crypto === void 0 ? void 0 : _crypto.randomUUID()) !== null && _ref !== void 0 ? _ref : Math.random().toString());
73
+ const contextLayer = useChaynsHistoryLayerContext();
74
+ const parentLayerRef = useRef(contextLayer);
75
+ const rootLayerRef = useRef(null);
67
76
  if (!customWrapper.current) {
68
77
  if (isModule) {
69
78
  if (data && functions) {
@@ -91,9 +100,10 @@ const ChaynsProvider = ({
91
100
  customWrapper.current.chaynsApiId = idRef.current;
92
101
  }
93
102
  }
103
+ const [effectiveLayer, setEffectiveLayer] = useState(historyLayer !== null && historyLayer !== void 0 ? historyLayer : null);
94
104
  const [isInitialized, setIsInitialized] = useState(!!((_customWrapper$curren = customWrapper.current) !== null && _customWrapper$curren !== void 0 && _customWrapper$curren.values));
95
105
  useEffect(() => {
96
- void (async () => {
106
+ void (async (_customWrapper$curren2, _ref2) => {
97
107
  await customWrapper.current.init();
98
108
  customWrapper.current.addDataListener(({
99
109
  type,
@@ -101,6 +111,15 @@ const ChaynsProvider = ({
101
111
  }) => {
102
112
  customWrapper.current.emitChange();
103
113
  });
114
+ parentLayerRef.current = (_customWrapper$curren2 = customWrapper.current.functions.getHistoryLayer()) !== null && _customWrapper$curren2 !== void 0 ? _customWrapper$curren2 : contextLayer;
115
+ if (!rootLayerRef.current && !historyLayer && !parentLayerRef.current) {
116
+ rootLayerRef.current = getOrInitRootChaynsHistoryLayer(history === null || history === void 0 ? void 0 : history.url, history === null || history === void 0 ? void 0 : history.segmentCount).rootLayer;
117
+ }
118
+ const layer = (_ref2 = historyLayer !== null && historyLayer !== void 0 ? historyLayer : parentLayerRef.current) !== null && _ref2 !== void 0 ? _ref2 : rootLayerRef.current;
119
+ if (typeof segmentCount === 'number' && layer.getSegmentCount() !== segmentCount) {
120
+ layer.setSegmentCount(segmentCount);
121
+ }
122
+ setEffectiveLayer(layer);
104
123
  if (!isInitialized) {
105
124
  setIsInitialized(true);
106
125
  }
@@ -125,10 +144,19 @@ const ChaynsProvider = ({
125
144
  removeModuleWrapper(id, customWrapper.current);
126
145
  };
127
146
  }, []);
147
+ let isDisabled = Boolean((_customWrapper$curren3 = customWrapper.current.values) === null || _customWrapper$curren3 === void 0 ? void 0 : _customWrapper$curren3.isHistoryDisabled);
148
+ if (typeof isHistoryDisabled === 'boolean' && !isDisabled) {
149
+ isDisabled = isHistoryDisabled;
150
+ }
151
+ if ((historyLayer === null || historyLayer === void 0 ? void 0 : historyLayer.id) === 'root') {
152
+ isDisabled = false;
153
+ }
128
154
  return React.createElement(React.Fragment, null, isInitialized && React.createElement(ChaynsContext.Provider, {
129
155
  value: customWrapper.current
130
- }, children), React.createElement(InitialDataProvider, {
131
- data: (_customWrapper$curren2 = customWrapper.current) === null || _customWrapper$curren2 === void 0 ? void 0 : _customWrapper$curren2.values,
156
+ }, effectiveLayer && !isDisabled ? React.createElement(ChaynsHistoryLayerProvider, {
157
+ layer: effectiveLayer
158
+ }, children) : children), React.createElement(InitialDataProvider, {
159
+ data: (_customWrapper$curren4 = customWrapper.current) === null || _customWrapper$curren4 === void 0 ? void 0 : _customWrapper$curren4.values,
132
160
  renderedByServer: renderedByServer
133
161
  }));
134
162
  };
@@ -1,6 +1,6 @@
1
1
  function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
2
2
  import React, { useContext, useEffect, useState } from 'react';
3
- import { HydrationContext } from '../constants';
3
+ import { HydrationContext } from '../contexts';
4
4
  function withHydrationBoundary(Component, initializer, useHydrationId, useProps) {
5
5
  const stores = {};
6
6
  return ({
@@ -1,3 +1 @@
1
- export * from './hydrationContext';
2
- export * from './DefaultLoginDialogOptions';
3
- export * from './moduleContext';
1
+ export * from './DefaultLoginDialogOptions';
@@ -0,0 +1,76 @@
1
+ import { c as _c } from "react-compiler-runtime";
2
+ import React, { createContext, useContext } from 'react';
3
+ import { useIsomorphicLayoutEffect } from '../hooks/useIsomorphicLayoutEffect';
4
+ const _layerStack = [];
5
+ export function pushChaynsHistoryLayer(layer) {
6
+ _layerStack.push(layer);
7
+ }
8
+ export function popChaynsHistoryLayer(layer) {
9
+ const index = _layerStack.lastIndexOf(layer);
10
+ if (index > -1) {
11
+ _layerStack.splice(index, 1);
12
+ }
13
+ }
14
+ export function getCurrentChaynsHistoryLayer() {
15
+ return _layerStack.length > 0 ? _layerStack[_layerStack.length - 1] : null;
16
+ }
17
+ const ChaynsHistoryLayerContext = createContext(null);
18
+ ChaynsHistoryLayerContext.displayName = 'ChaynsHistoryLayerContext';
19
+ export const ChaynsHistoryLayerProvider = t0 => {
20
+ const $ = _c(6);
21
+ const {
22
+ layer,
23
+ children
24
+ } = t0;
25
+ let t1;
26
+ let t2;
27
+ if ($[0] !== layer) {
28
+ t1 = () => {
29
+ pushChaynsHistoryLayer(layer);
30
+ return () => popChaynsHistoryLayer(layer);
31
+ };
32
+ t2 = [layer];
33
+ $[0] = layer;
34
+ $[1] = t1;
35
+ $[2] = t2;
36
+ } else {
37
+ t1 = $[1];
38
+ t2 = $[2];
39
+ }
40
+ useIsomorphicLayoutEffect(t1, t2);
41
+ let t3;
42
+ if ($[3] !== children || $[4] !== layer) {
43
+ t3 = React.createElement(ChaynsHistoryLayerContext.Provider, {
44
+ value: layer
45
+ }, children);
46
+ $[3] = children;
47
+ $[4] = layer;
48
+ $[5] = t3;
49
+ } else {
50
+ t3 = $[5];
51
+ }
52
+ return t3;
53
+ };
54
+ export const ChaynsHistoryLayerOverrideProvider = t0 => {
55
+ const $ = _c(3);
56
+ const {
57
+ layer,
58
+ children
59
+ } = t0;
60
+ let t1;
61
+ if ($[0] !== children || $[1] !== layer) {
62
+ t1 = React.createElement(ChaynsHistoryLayerContext.Provider, {
63
+ value: layer
64
+ }, children);
65
+ $[0] = children;
66
+ $[1] = layer;
67
+ $[2] = t1;
68
+ } else {
69
+ t1 = $[2];
70
+ }
71
+ return t1;
72
+ };
73
+ export function useChaynsHistoryLayerContext() {
74
+ return useContext(ChaynsHistoryLayerContext);
75
+ }
76
+ export default ChaynsHistoryLayerContext;
@@ -0,0 +1,3 @@
1
+ export * from './hydrationContext';
2
+ export * from './moduleContext';
3
+ export * from './HistoryLayerContext';
@@ -0,0 +1,105 @@
1
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
2
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
3
+ function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
4
+ import { EventBus } from '../../utils/EventBus';
5
+ import { shallowEqualArr } from '../../utils/equality';
6
+ import { normalizeHistoryRouteInput, normalizeHistorySegments } from '../../utils/history/segments';
7
+ export class FrameHistoryLayer {
8
+ constructor(bridge, initial) {
9
+ _defineProperty(this, "id", void 0);
10
+ _defineProperty(this, "depth", void 0);
11
+ _defineProperty(this, "_segments", void 0);
12
+ _defineProperty(this, "_params", void 0);
13
+ _defineProperty(this, "_hash", void 0);
14
+ _defineProperty(this, "_state", void 0);
15
+ _defineProperty(this, "_activeChildId", void 0);
16
+ _defineProperty(this, "_segmentCount", void 0);
17
+ _defineProperty(this, "bus", new EventBus());
18
+ _defineProperty(this, "bridge", void 0);
19
+ this.id = initial.id;
20
+ this.depth = initial.depth;
21
+ this._segments = normalizeHistorySegments(initial.segments);
22
+ this._params = initial.params;
23
+ this._hash = initial.hash;
24
+ this._state = initial.state;
25
+ this._activeChildId = initial.activeChildId;
26
+ this._segmentCount = initial.segmentCount;
27
+ this.bridge = bridge;
28
+ }
29
+ getSegmentCount() {
30
+ return this._segmentCount;
31
+ }
32
+ setSegmentCount(n) {
33
+ if (n === this._segmentCount) {
34
+ return;
35
+ }
36
+ this._segmentCount = n;
37
+ void this.bridge.setSegmentCount(n);
38
+ }
39
+ createChildLayer(_id) {
40
+ throw new Error('[chaynsHistory] FrameHistoryLayer does not support createChildLayer. ' + 'Manage sub-routing within the iframe with a local initRootChaynsHistoryLayer.');
41
+ }
42
+ destroyChildLayer(_id) {}
43
+ setActiveChild(id, init) {
44
+ return this.bridge.setActiveChild(id, init);
45
+ }
46
+ getActiveChildId() {
47
+ return this._activeChildId;
48
+ }
49
+ getChildLayer(_id) {
50
+ return undefined;
51
+ }
52
+ getRoute() {
53
+ return [...this._segments];
54
+ }
55
+ setRoute(route, opts) {
56
+ const normalizedRoute = normalizeHistoryRouteInput(route);
57
+ if (shallowEqualArr(this._segments, normalizedRoute)) {
58
+ return;
59
+ }
60
+ void this.bridge.setRoute(normalizedRoute, opts);
61
+ }
62
+ getParams() {
63
+ return {
64
+ ...this._params
65
+ };
66
+ }
67
+ setParams(params, opts) {
68
+ void this.bridge.setParams(params, opts);
69
+ }
70
+ getHash() {
71
+ return this._hash;
72
+ }
73
+ setHash(hash, opts) {
74
+ void this.bridge.setHash(hash, opts);
75
+ }
76
+ getState() {
77
+ return this._state;
78
+ }
79
+ setState(state, opts) {
80
+ void this.bridge.setState(state, opts);
81
+ }
82
+ navigate(opts) {
83
+ return this.bridge.navigate(opts);
84
+ }
85
+ addBlock(callback, opts) {
86
+ let removeFn = null;
87
+ void this.bridge.addBlock(callback, opts).then(fn => {
88
+ removeFn = fn;
89
+ });
90
+ return () => {
91
+ var _removeFn;
92
+ return (_removeFn = removeFn) === null || _removeFn === void 0 ? void 0 : _removeFn();
93
+ };
94
+ }
95
+ addEventListener(type, handler) {
96
+ return this.bus.on(type, handler);
97
+ }
98
+ _applyAndEmit(e) {
99
+ this._segments = normalizeHistorySegments(e.segments);
100
+ this._params = e.params;
101
+ this._hash = e.hash;
102
+ this._state = e.state;
103
+ this.bus.emit(e.type, e);
104
+ }
105
+ }
@@ -0,0 +1,321 @@
1
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
2
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
3
+ function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
4
+ import { EventBus } from '../../utils/EventBus';
5
+ import { resolveSegmentsFrom } from '../../utils/history/rootLayer';
6
+ import { normalizeHistoryRouteInput, normalizeHistorySegments } from '../../utils/history/segments';
7
+ const RESERVED_STATE_KEYS = ['activeChild', 'childState', '__params', '__hash'];
8
+ export class ChaynsHistoryLayer {
9
+ constructor(init) {
10
+ var _init$segmentCount, _init$segments;
11
+ _defineProperty(this, "id", void 0);
12
+ _defineProperty(this, "depth", void 0);
13
+ _defineProperty(this, "parent", void 0);
14
+ _defineProperty(this, "children", new Map());
15
+ _defineProperty(this, "activeChildId", null);
16
+ _defineProperty(this, "segmentCount", 0);
17
+ _defineProperty(this, "segments", []);
18
+ _defineProperty(this, "ownState", {});
19
+ _defineProperty(this, "_params", {});
20
+ _defineProperty(this, "_hash", undefined);
21
+ _defineProperty(this, "bus", new EventBus());
22
+ _defineProperty(this, "deps", void 0);
23
+ _defineProperty(this, "_bootstrapPool", null);
24
+ _defineProperty(this, "_bootstrapUrlResolver", null);
25
+ _defineProperty(this, "isDestroyed", false);
26
+ this.id = init.id;
27
+ this.parent = init.parent;
28
+ this.depth = init.parent ? init.parent.depth + 1 : 0;
29
+ this.deps = init.deps;
30
+ this.segmentCount = (_init$segmentCount = init.segmentCount) !== null && _init$segmentCount !== void 0 ? _init$segmentCount : 0;
31
+ this.segments = normalizeHistorySegments((_init$segments = init.segments) !== null && _init$segments !== void 0 ? _init$segments : []);
32
+ }
33
+ getSegmentCount() {
34
+ return this.segmentCount;
35
+ }
36
+ setSegmentCount(n) {
37
+ if (n < 0 || !Number.isInteger(n)) {
38
+ return;
39
+ }
40
+ const prev = this.segmentCount;
41
+ if (n === prev) {
42
+ return;
43
+ }
44
+ this.segmentCount = n;
45
+ if (this._isInActiveChain()) {
46
+ if (n < prev) {
47
+ const excess = this.segments.splice(n);
48
+ if (excess.length > 0 && this.activeChildId) {
49
+ const child = this.children.get(this.activeChildId);
50
+ if (child) {
51
+ child._setOwnSegmentsSilent([...excess, ...child._getOwnSegments()]);
52
+ }
53
+ }
54
+ } else if (n > prev && this.segments.length === 0) {
55
+ const root = this.deps.getRoot();
56
+ const bootstrapSegs = root._consumeBootstrapSegments(n);
57
+ if (bootstrapSegs) {
58
+ this.segments = normalizeHistorySegments(bootstrapSegs);
59
+ }
60
+ }
61
+ void this.deps.getQueue().enqueue({
62
+ kind: 'setRoute',
63
+ layerId: this.id,
64
+ segments: [...this.segments],
65
+ opts: {},
66
+ _notifyEvenIfUnchanged: true,
67
+ _skipCommit: true
68
+ });
69
+ }
70
+ }
71
+ createChildLayer(id) {
72
+ if (this.children.has(id)) {
73
+ throw new Error(`[chaynsHistory] Child layer with id "${id}" already exists on layer "${this.id}".`);
74
+ }
75
+ const segments = resolveSegmentsFrom(undefined, this.getCumulativeSegmentCount());
76
+ const child = new ChaynsHistoryLayer({
77
+ id,
78
+ parent: this,
79
+ deps: this.deps,
80
+ segments,
81
+ segmentCount: segments.length
82
+ });
83
+ this.children.set(id, child);
84
+ return child;
85
+ }
86
+ destroyChildLayer(id) {
87
+ const child = this.children.get(id);
88
+ if (!child) return;
89
+ child.markDestroyed();
90
+ this.children.delete(id);
91
+ if (this.activeChildId === id) {
92
+ void this.deps.getQueue().enqueue({
93
+ kind: 'setActiveChild',
94
+ layerId: this.id,
95
+ childId: null
96
+ });
97
+ }
98
+ }
99
+ getChildLayer(id) {
100
+ return this.children.get(id);
101
+ }
102
+ getActiveChildId() {
103
+ return this.activeChildId;
104
+ }
105
+ setActiveChild(id, init) {
106
+ if (this.isDestroyed) return Promise.resolve({
107
+ isOk: false,
108
+ reason: 'destroyed'
109
+ });
110
+ return this.deps.getQueue().enqueue({
111
+ kind: 'setActiveChild',
112
+ layerId: this.id,
113
+ childId: id,
114
+ init: init ? {
115
+ ...init,
116
+ route: init.route !== undefined ? ChaynsHistoryLayer.normalizeRoute(init.route) : undefined
117
+ } : undefined
118
+ });
119
+ }
120
+ getRoute() {
121
+ return [...this.segments];
122
+ }
123
+ setRoute(route, opts) {
124
+ if (this.isDestroyed) return;
125
+ void this.deps.getQueue().enqueue({
126
+ kind: 'setRoute',
127
+ layerId: this.id,
128
+ segments: ChaynsHistoryLayer.normalizeRoute(route),
129
+ opts: opts !== null && opts !== void 0 ? opts : {}
130
+ });
131
+ }
132
+ getParams() {
133
+ return {
134
+ ...this._params
135
+ };
136
+ }
137
+ setParams(params, opts) {
138
+ if (this.isDestroyed) return;
139
+ void this.deps.getQueue().enqueue({
140
+ kind: 'setParams',
141
+ layerId: this.id,
142
+ params: {
143
+ ...params
144
+ },
145
+ opts: opts !== null && opts !== void 0 ? opts : {}
146
+ });
147
+ }
148
+ getHash() {
149
+ var _this$_hash;
150
+ return (_this$_hash = this._hash) !== null && _this$_hash !== void 0 ? _this$_hash : '';
151
+ }
152
+ setHash(hash, opts) {
153
+ if (this.isDestroyed) return;
154
+ const normalized = hash.startsWith('#') ? hash.slice(1) : hash;
155
+ void this.deps.getQueue().enqueue({
156
+ kind: 'setHash',
157
+ layerId: this.id,
158
+ hash: normalized,
159
+ opts: opts !== null && opts !== void 0 ? opts : {}
160
+ });
161
+ }
162
+ getState() {
163
+ return {
164
+ ...this.ownState
165
+ };
166
+ }
167
+ setState(state, opts) {
168
+ if (this.isDestroyed) return;
169
+ const filtered = ChaynsHistoryLayer.filterReservedKeys(state);
170
+ void this.deps.getQueue().enqueue({
171
+ kind: 'setState',
172
+ layerId: this.id,
173
+ state: filtered,
174
+ opts: opts !== null && opts !== void 0 ? opts : {}
175
+ });
176
+ }
177
+ navigate(opts) {
178
+ if (this.isDestroyed) return Promise.resolve({
179
+ isOk: false,
180
+ reason: 'destroyed'
181
+ });
182
+ const {
183
+ route,
184
+ state,
185
+ params,
186
+ hash,
187
+ activeChild,
188
+ activeChildInit,
189
+ ...rest
190
+ } = opts;
191
+ return this.deps.getQueue().enqueue({
192
+ kind: 'navigate',
193
+ layerId: this.id,
194
+ route: route !== undefined ? ChaynsHistoryLayer.normalizeRoute(route) : undefined,
195
+ state: state ? ChaynsHistoryLayer.filterReservedKeys(state) : undefined,
196
+ params,
197
+ hash: hash !== undefined ? hash.startsWith('#') ? hash.slice(1) : hash : undefined,
198
+ activeChild,
199
+ activeChildInit: activeChildInit ? {
200
+ ...activeChildInit,
201
+ route: activeChildInit.route !== undefined ? ChaynsHistoryLayer.normalizeRoute(activeChildInit.route) : undefined
202
+ } : undefined,
203
+ opts: rest
204
+ });
205
+ }
206
+ addBlock(callback, opts = {}) {
207
+ return this.deps.getBlockRegistry().add(this, callback, opts);
208
+ }
209
+ addEventListener(type, handler) {
210
+ return this.bus.on(type, handler);
211
+ }
212
+ _getOwnState() {
213
+ return this.ownState;
214
+ }
215
+ _getOwnSegments() {
216
+ return this.segments;
217
+ }
218
+ _getChildren() {
219
+ return this.children;
220
+ }
221
+ _setOwnStateSilent(next) {
222
+ this.ownState = ChaynsHistoryLayer.filterReservedKeys(next);
223
+ }
224
+ _setOwnSegmentsSilent(next) {
225
+ this.segments = normalizeHistorySegments(next);
226
+ }
227
+ _getOwnParams() {
228
+ return this._params;
229
+ }
230
+ _setOwnParamsSilent(params) {
231
+ this._params = {
232
+ ...params
233
+ };
234
+ }
235
+ _getOwnHash() {
236
+ return this._hash;
237
+ }
238
+ _setOwnHashSilent(hash) {
239
+ this._hash = hash;
240
+ }
241
+ _setActiveChildSilent(id) {
242
+ this.activeChildId = id;
243
+ }
244
+ _emit(type) {
245
+ var _this$_hash2;
246
+ const event = {
247
+ type,
248
+ layerId: this.id,
249
+ segments: [...this.segments],
250
+ state: {
251
+ ...this.ownState
252
+ },
253
+ params: {
254
+ ...this._params
255
+ },
256
+ hash: (_this$_hash2 = this._hash) !== null && _this$_hash2 !== void 0 ? _this$_hash2 : ''
257
+ };
258
+ this.bus.emit(type, event);
259
+ }
260
+ _isInActiveChain() {
261
+ let node = this;
262
+ while (node && node.parent) {
263
+ if (node.parent.activeChildId !== node.id) return false;
264
+ node = node.parent;
265
+ }
266
+ return true;
267
+ }
268
+ _setBootstrapUrlResolver(resolver) {
269
+ this._bootstrapUrlResolver = resolver;
270
+ }
271
+ _setBootstrapPool(segs) {
272
+ this._bootstrapPool = [...segs];
273
+ }
274
+ _consumeBootstrapSegments(n) {
275
+ if (this._bootstrapPool === null && this._bootstrapUrlResolver) {
276
+ const pathname = this._bootstrapUrlResolver();
277
+ this._bootstrapPool = pathname.replace(/^\//, '').split('/').filter(Boolean);
278
+ this._bootstrapUrlResolver = null;
279
+ }
280
+ if (!this._bootstrapPool || this._bootstrapPool.length === 0 || n === 0) return null;
281
+ const taken = this._bootstrapPool.splice(0, n);
282
+ while (taken.length < n) taken.push('');
283
+ return taken;
284
+ }
285
+ _isDestroyed() {
286
+ return this.isDestroyed;
287
+ }
288
+ markDestroyed() {
289
+ this.isDestroyed = true;
290
+ try {
291
+ this.deps.getBlockRegistry().removeAllForLayer(this.id);
292
+ } catch {}
293
+ for (const child of this.children.values()) {
294
+ child.markDestroyed();
295
+ }
296
+ this.children.clear();
297
+ this.bus.clear();
298
+ }
299
+ getCumulativeSegmentCount() {
300
+ let count = this.segmentCount;
301
+ let node = this.parent;
302
+ while (node) {
303
+ count += node.segmentCount;
304
+ node = node.parent;
305
+ }
306
+ return count;
307
+ }
308
+ static normalizeRoute(route) {
309
+ return normalizeHistoryRouteInput(route);
310
+ }
311
+ static filterReservedKeys(input) {
312
+ const out = {};
313
+ for (const key of Object.keys(input)) {
314
+ if (RESERVED_STATE_KEYS.includes(key)) {
315
+ continue;
316
+ }
317
+ out[key] = input[key];
318
+ }
319
+ return out;
320
+ }
321
+ }
@@ -0,0 +1,2 @@
1
+ export { ChaynsHistoryLayer as ChaynsHistoryLayerClass } from './HistoryLayer';
2
+ export { FrameHistoryLayer } from './FrameHistoryLayer';