chayns-api 3.0.0-beta.4 → 3.1.0-beta.0

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 (140) hide show
  1. package/README.md +4 -11
  2. package/dist/cjs/calls/index.js +4 -1
  3. package/dist/cjs/calls/visibilityChangeListener.js +4 -4
  4. package/dist/cjs/components/ChaynsProvider.js +34 -6
  5. package/dist/cjs/components/withHydrationBoundary.js +2 -2
  6. package/dist/cjs/constants/index.js +0 -22
  7. package/dist/cjs/contexts/HistoryLayerContext.js +89 -0
  8. package/dist/cjs/contexts/index.js +38 -0
  9. package/dist/cjs/handler/history/FrameHistoryLayer.js +100 -0
  10. package/dist/cjs/handler/history/HistoryLayer.js +321 -0
  11. package/dist/cjs/handler/history/index.js +19 -0
  12. package/dist/cjs/hooks/history.js +454 -0
  13. package/dist/cjs/hooks/index.js +62 -1
  14. package/dist/cjs/host/ChaynsHost.js +113 -54
  15. package/dist/cjs/host/iframe/HostIframe.js +70 -5
  16. package/dist/cjs/host/module/ModuleHost.js +50 -44
  17. package/dist/cjs/index.js +139 -6
  18. package/dist/cjs/types/history.js +1 -0
  19. package/dist/cjs/umd.index.js +2 -2
  20. package/dist/cjs/utils/EventBus.js +33 -0
  21. package/dist/cjs/{util → utils}/appStorage.js +2 -2
  22. package/dist/cjs/utils/equality.js +19 -0
  23. package/dist/cjs/utils/history/BlockRegistry.js +110 -0
  24. package/dist/cjs/utils/history/NavigationQueue.js +388 -0
  25. package/dist/cjs/utils/history/layerTree.js +32 -0
  26. package/dist/cjs/utils/history/navigationIndex.js +42 -0
  27. package/dist/cjs/utils/history/rootLayer.js +175 -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 +23 -23
  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 +108 -0
  58. package/dist/esm/utils/history/NavigationQueue.js +385 -0
  59. package/dist/esm/utils/history/layerTree.js +24 -0
  60. package/dist/esm/utils/history/navigationIndex.js +33 -0
  61. package/dist/esm/utils/history/rootLayer.js +167 -0
  62. package/dist/esm/utils/history/segments.js +7 -0
  63. package/dist/esm/utils/history/stateProjector.js +147 -0
  64. package/dist/esm/utils/history/url.js +40 -0
  65. package/dist/esm/utils/history/window.js +3 -0
  66. package/dist/esm/wrapper/AppWrapper.js +5 -5
  67. package/dist/esm/wrapper/FrameWrapper.js +35 -2
  68. package/dist/esm/wrapper/ModuleFederationWrapper.js +2 -0
  69. package/dist/esm/wrapper/StaticChaynsApi.js +2 -1
  70. package/dist/types/calls/index.d.ts +5 -0
  71. package/dist/types/components/ChaynsProvider.d.ts +21 -0
  72. package/dist/types/constants/index.d.ts +0 -2
  73. package/dist/types/contexts/HistoryLayerContext.d.ts +33 -0
  74. package/dist/types/contexts/index.d.ts +3 -0
  75. package/dist/types/handler/history/FrameHistoryLayer.d.ts +99 -0
  76. package/dist/types/handler/history/HistoryLayer.d.ts +117 -0
  77. package/dist/types/handler/history/index.d.ts +2 -0
  78. package/dist/types/hooks/history.d.ts +89 -0
  79. package/dist/types/hooks/index.d.ts +1 -0
  80. package/dist/types/host/ChaynsHost.d.ts +12 -0
  81. package/dist/types/host/iframe/HostIframe.d.ts +4 -0
  82. package/dist/types/host/module/ModuleHost.d.ts +4 -0
  83. package/dist/types/index.d.ts +15 -6
  84. package/dist/types/types/IChaynsReact.d.ts +3 -0
  85. package/dist/types/types/history.d.ts +74 -0
  86. package/dist/types/umd.index.d.ts +2 -2
  87. package/dist/types/utils/EventBus.d.ts +10 -0
  88. package/dist/types/{util → utils}/collectCssChunks.d.ts +1 -1
  89. package/dist/types/utils/equality.d.ts +2 -0
  90. package/dist/types/utils/history/BlockRegistry.d.ts +38 -0
  91. package/dist/types/utils/history/NavigationQueue.d.ts +109 -0
  92. package/dist/types/utils/history/layerTree.d.ts +10 -0
  93. package/dist/types/utils/history/navigationIndex.d.ts +14 -0
  94. package/dist/types/utils/history/rootLayer.d.ts +42 -0
  95. package/dist/types/utils/history/segments.d.ts +2 -0
  96. package/dist/types/utils/history/stateProjector.d.ts +24 -0
  97. package/dist/types/utils/history/url.d.ts +17 -0
  98. package/dist/types/utils/history/window.d.ts +1 -0
  99. package/dist/types/wrapper/FrameWrapper.d.ts +1 -0
  100. package/dist/types/wrapper/StaticChaynsApi.d.ts +1 -0
  101. package/package.json +2 -1
  102. /package/dist/cjs/{constants → contexts}/hydrationContext.js +0 -0
  103. /package/dist/cjs/{constants → contexts}/moduleContext.js +0 -0
  104. /package/dist/cjs/{helper/apiListenerHelper.js → utils/apiListener.js} +0 -0
  105. /package/dist/cjs/{util → utils}/appCall.js +0 -0
  106. /package/dist/cjs/{util → utils}/bindChaynsApi.js +0 -0
  107. /package/dist/cjs/{util → utils}/collectCssChunks.js +0 -0
  108. /package/dist/cjs/{util → utils}/deviceHelper.js +0 -0
  109. /package/dist/cjs/{util → utils}/heightHelper.js +0 -0
  110. /package/dist/cjs/{util → utils}/initModuleFederationSharing.js +0 -0
  111. /package/dist/cjs/{util → utils}/is.js +0 -0
  112. /package/dist/cjs/{util → utils}/postIframeForm.js +0 -0
  113. /package/dist/cjs/{util → utils}/transferNestedFunctions.js +0 -0
  114. /package/dist/cjs/{util → utils}/url.js +0 -0
  115. /package/dist/esm/{constants → contexts}/hydrationContext.js +0 -0
  116. /package/dist/esm/{constants → contexts}/moduleContext.js +0 -0
  117. /package/dist/esm/{helper/apiListenerHelper.js → utils/apiListener.js} +0 -0
  118. /package/dist/esm/{util → utils}/appCall.js +0 -0
  119. /package/dist/esm/{util → utils}/bindChaynsApi.js +0 -0
  120. /package/dist/esm/{util → utils}/collectCssChunks.js +0 -0
  121. /package/dist/esm/{util → utils}/deviceHelper.js +0 -0
  122. /package/dist/esm/{util → utils}/heightHelper.js +0 -0
  123. /package/dist/esm/{util → utils}/initModuleFederationSharing.js +0 -0
  124. /package/dist/esm/{util → utils}/is.js +0 -0
  125. /package/dist/esm/{util → utils}/postIframeForm.js +0 -0
  126. /package/dist/esm/{util → utils}/transferNestedFunctions.js +0 -0
  127. /package/dist/esm/{util → utils}/url.js +0 -0
  128. /package/dist/types/{constants → contexts}/hydrationContext.d.ts +0 -0
  129. /package/dist/types/{constants → contexts}/moduleContext.d.ts +0 -0
  130. /package/dist/types/{helper/apiListenerHelper.d.ts → utils/apiListener.d.ts} +0 -0
  131. /package/dist/types/{util → utils}/appCall.d.ts +0 -0
  132. /package/dist/types/{util → utils}/appStorage.d.ts +0 -0
  133. /package/dist/types/{util → utils}/bindChaynsApi.d.ts +0 -0
  134. /package/dist/types/{util → utils}/deviceHelper.d.ts +0 -0
  135. /package/dist/types/{util → utils}/heightHelper.d.ts +0 -0
  136. /package/dist/types/{util → utils}/initModuleFederationSharing.d.ts +0 -0
  137. /package/dist/types/{util → utils}/is.d.ts +0 -0
  138. /package/dist/types/{util → utils}/postIframeForm.d.ts +0 -0
  139. /package/dist/types/{util → utils}/transferNestedFunctions.d.ts +0 -0
  140. /package/dist/types/{util → utils}/url.d.ts +0 -0
@@ -0,0 +1,385 @@
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 { hasWindowHistory } from './window';
5
+ import { shallowEqualArr, shallowEqualObj } from '../equality';
6
+ export class NavigationQueue {
7
+ constructor(deps) {
8
+ _defineProperty(this, "queue", []);
9
+ _defineProperty(this, "isRunning", false);
10
+ _defineProperty(this, "deps", void 0);
11
+ this.deps = deps;
12
+ }
13
+ enqueue(op) {
14
+ return new Promise(resolve => {
15
+ this.queue.push({
16
+ op,
17
+ resolve
18
+ });
19
+ void this.tick();
20
+ });
21
+ }
22
+ async tick() {
23
+ if (this.isRunning) return;
24
+ this.isRunning = true;
25
+ try {
26
+ while (this.queue.length > 0) {
27
+ const entry = this.queue.shift();
28
+ try {
29
+ const result = await this.process(entry.op);
30
+ entry.resolve(result);
31
+ } catch (error) {
32
+ entry.resolve({
33
+ isOk: false,
34
+ reason: 'error',
35
+ error
36
+ });
37
+ }
38
+ }
39
+ } finally {
40
+ this.isRunning = false;
41
+ }
42
+ }
43
+ async process(op) {
44
+ switch (op.kind) {
45
+ case 'setRoute':
46
+ return this.processSetRoute(op);
47
+ case 'setState':
48
+ return this.processSetState(op);
49
+ case 'setParams':
50
+ return this.processSetParams(op);
51
+ case 'setHash':
52
+ return this.processSetHash(op);
53
+ case 'setActiveChild':
54
+ return this.processSetActiveChild(op);
55
+ case 'navigate':
56
+ return this.processNavigate(op);
57
+ case 'popstate':
58
+ return this.processPopstate(op);
59
+ default:
60
+ {
61
+ const _exhaustive = op;
62
+ return {
63
+ isOk: false,
64
+ reason: 'error',
65
+ error: new Error('Unknown op')
66
+ };
67
+ }
68
+ }
69
+ }
70
+ async processSetRoute(op) {
71
+ const layer = this.resolveActiveLayer(op.layerId);
72
+ if (!layer) return {
73
+ isOk: false,
74
+ reason: 'stale'
75
+ };
76
+ const allowed = await this.deps.checkBlocks(layer);
77
+ if (!allowed) return {
78
+ isOk: false,
79
+ reason: 'blocked'
80
+ };
81
+ const prevSegments = layer._getOwnSegments();
82
+ const prevParams = layer._getOwnParams();
83
+ const prevHash = layer._getOwnHash();
84
+ layer._setOwnSegmentsSilent(op.segments);
85
+ if (op.opts.params !== undefined) layer._setOwnParamsSilent(op.opts.params);
86
+ if (op.opts.hash !== undefined) layer._setOwnHashSilent(op.opts.hash);
87
+ const changed = op._notifyEvenIfUnchanged === true || !shallowEqualArr(prevSegments, op.segments) || op.opts.params !== undefined && !shallowEqualObj(prevParams, op.opts.params) || op.opts.hash !== undefined && prevHash !== op.opts.hash;
88
+ if (changed) {
89
+ if (op._skipCommit !== true) {
90
+ this.commit(op.opts.isReplace === true);
91
+ }
92
+ layer._emit('change');
93
+ }
94
+ return {
95
+ isOk: true
96
+ };
97
+ }
98
+ async processSetParams(op) {
99
+ const layer = this.resolveActiveLayer(op.layerId);
100
+ if (!layer) return {
101
+ isOk: false,
102
+ reason: 'stale'
103
+ };
104
+ const allowed = await this.deps.checkBlocks(layer);
105
+ if (!allowed) return {
106
+ isOk: false,
107
+ reason: 'blocked'
108
+ };
109
+ const prev = layer._getOwnParams();
110
+ layer._setOwnParamsSilent(op.params);
111
+ if (!shallowEqualObj(prev, op.params)) {
112
+ this.commit(op.opts.isReplace === true);
113
+ layer._emit('change');
114
+ }
115
+ return {
116
+ isOk: true
117
+ };
118
+ }
119
+ async processSetHash(op) {
120
+ const layer = this.resolveActiveLayer(op.layerId);
121
+ if (!layer) return {
122
+ isOk: false,
123
+ reason: 'stale'
124
+ };
125
+ const allowed = await this.deps.checkBlocks(layer);
126
+ if (!allowed) return {
127
+ isOk: false,
128
+ reason: 'blocked'
129
+ };
130
+ const prev = layer._getOwnHash();
131
+ layer._setOwnHashSilent(op.hash);
132
+ if (prev !== op.hash) {
133
+ this.commit(op.opts.isReplace === true);
134
+ layer._emit('change');
135
+ }
136
+ return {
137
+ isOk: true
138
+ };
139
+ }
140
+ async processSetState(op) {
141
+ const layer = this.resolveActiveLayer(op.layerId);
142
+ if (!layer) return {
143
+ isOk: false,
144
+ reason: 'stale'
145
+ };
146
+ const allowed = await this.deps.checkBlocks(layer);
147
+ if (!allowed) return {
148
+ isOk: false,
149
+ reason: 'blocked'
150
+ };
151
+ const previous = layer._getOwnState();
152
+ layer._setOwnStateSilent(op.state);
153
+ const changed = !shallowEqualObj(previous, op.state);
154
+ if (changed) {
155
+ this.commit(op.opts.isReplace === true);
156
+ layer._emit('change');
157
+ }
158
+ return {
159
+ isOk: true
160
+ };
161
+ }
162
+ async processSetActiveChild(op) {
163
+ const layer = this.resolveActiveLayer(op.layerId);
164
+ if (!layer) return {
165
+ isOk: false,
166
+ reason: 'stale'
167
+ };
168
+ const allowed = await this.deps.checkBlocks(layer);
169
+ if (!allowed) return {
170
+ isOk: false,
171
+ reason: 'blocked'
172
+ };
173
+ const previousUrl = this.deps.projectUrl();
174
+ const previousId = layer.getActiveChildId();
175
+ if (op.childId !== null && !layer.getChildLayer(op.childId)) {
176
+ layer.createChildLayer(op.childId);
177
+ }
178
+ layer._setActiveChildSilent(op.childId);
179
+ let childDataChanged = false;
180
+ if (op.childId && op.init) {
181
+ const child = layer.getChildLayer(op.childId);
182
+ if (child) {
183
+ if (op.init.route) {
184
+ child._setOwnSegmentsSilent(op.init.route);
185
+ childDataChanged = true;
186
+ }
187
+ if (op.init.state) {
188
+ child._setOwnStateSilent(op.init.state);
189
+ childDataChanged = true;
190
+ }
191
+ }
192
+ }
193
+ if (op.childId) {
194
+ const child = layer.getChildLayer(op.childId);
195
+ if (child && child._getOwnSegments().length === 0 && child.getSegmentCount() > 0) {
196
+ const root = this.deps.getRoot();
197
+ const bootstrapSegs = root._consumeBootstrapSegments(child.getSegmentCount());
198
+ if (bootstrapSegs) {
199
+ child._setOwnSegmentsSilent(bootstrapSegs);
200
+ childDataChanged = true;
201
+ }
202
+ }
203
+ }
204
+ if (previousId !== op.childId) {
205
+ const nextUrl = this.deps.projectUrl();
206
+ const shouldReplace = previousUrl === nextUrl;
207
+ this.commit(shouldReplace);
208
+ layer._emit('change');
209
+ }
210
+ if (childDataChanged && op.childId) {
211
+ const child = layer.getChildLayer(op.childId);
212
+ if (child) child._emit('change');
213
+ }
214
+ return {
215
+ isOk: true
216
+ };
217
+ }
218
+ async processNavigate(op) {
219
+ const layer = this.resolveActiveLayer(op.layerId);
220
+ if (!layer) return {
221
+ isOk: false,
222
+ reason: 'stale'
223
+ };
224
+ const allowed = await this.deps.checkBlocks(layer);
225
+ if (!allowed) return {
226
+ isOk: false,
227
+ reason: 'blocked'
228
+ };
229
+ const prevSeg = layer._getOwnSegments();
230
+ const prevState = layer._getOwnState();
231
+ const prevParams = layer._getOwnParams();
232
+ const prevHash = layer._getOwnHash();
233
+ const prevActiveChild = layer.getActiveChildId();
234
+ if (op.route) layer._setOwnSegmentsSilent(op.route);
235
+ if (op.state) layer._setOwnStateSilent(op.state);
236
+ if (op.params !== undefined) layer._setOwnParamsSilent(op.params);
237
+ if (op.hash !== undefined) layer._setOwnHashSilent(op.hash);
238
+ const segChanged = op.route ? !shallowEqualArr(prevSeg, op.route) : false;
239
+ const stateChanged = op.state ? !shallowEqualObj(prevState, op.state) : false;
240
+ const paramsChanged = op.params !== undefined ? !shallowEqualObj(prevParams, op.params) : false;
241
+ const hashChanged = op.hash !== undefined ? prevHash !== op.hash : false;
242
+ let activeChildChanged = false;
243
+ let childDataChanged = false;
244
+ if (op.activeChild !== undefined) {
245
+ if (op.activeChild !== null && !layer.getChildLayer(op.activeChild)) {
246
+ layer.createChildLayer(op.activeChild);
247
+ }
248
+ layer._setActiveChildSilent(op.activeChild);
249
+ activeChildChanged = prevActiveChild !== op.activeChild;
250
+ if (op.activeChild && op.activeChildInit) {
251
+ const child = layer.getChildLayer(op.activeChild);
252
+ if (child) {
253
+ if (op.activeChildInit.route) {
254
+ child._setOwnSegmentsSilent(op.activeChildInit.route);
255
+ childDataChanged = true;
256
+ }
257
+ if (op.activeChildInit.state) {
258
+ child._setOwnStateSilent(op.activeChildInit.state);
259
+ childDataChanged = true;
260
+ }
261
+ }
262
+ }
263
+ if (op.activeChild) {
264
+ const child = layer.getChildLayer(op.activeChild);
265
+ if (child && child._getOwnSegments().length === 0 && child.getSegmentCount() > 0) {
266
+ const root = this.deps.getRoot();
267
+ const bootstrapSegs = root._consumeBootstrapSegments(child.getSegmentCount());
268
+ if (bootstrapSegs) {
269
+ child._setOwnSegmentsSilent(bootstrapSegs);
270
+ childDataChanged = true;
271
+ }
272
+ }
273
+ }
274
+ }
275
+ if (segChanged || stateChanged || paramsChanged || hashChanged || activeChildChanged) {
276
+ this.commit(op.opts.isReplace === true);
277
+ layer._emit('change');
278
+ }
279
+ if (childDataChanged && op.activeChild) {
280
+ const child = layer.getChildLayer(op.activeChild);
281
+ if (child) child._emit('change');
282
+ }
283
+ return {
284
+ isOk: true
285
+ };
286
+ }
287
+ async processPopstate(op) {
288
+ const {
289
+ changedLayerIds
290
+ } = this.deps.diffIncomingState(op.rawState);
291
+ const currentProjectedUrl = this.deps.projectUrl();
292
+ const browserPathname = hasWindowHistory() ? window.location.pathname : '';
293
+ if (browserPathname && browserPathname !== new URL(currentProjectedUrl, 'http://x').pathname) {
294
+ changedLayerIds.add(this.deps.getRoot().id);
295
+ }
296
+ const target = this.resolveLowestCommonLayer(changedLayerIds);
297
+ if (target) {
298
+ const allowed = await this.deps.checkBlocks(target);
299
+ if (!allowed) {
300
+ await this.deps.silentGo(+1);
301
+ return {
302
+ isOk: false,
303
+ reason: 'blocked'
304
+ };
305
+ }
306
+ }
307
+ const {
308
+ changedLayerIds: applied
309
+ } = this.deps.applyIncomingState(op.rawState);
310
+ const {
311
+ changedLayerIds: urlChanged
312
+ } = this.deps.applyUrlSegments();
313
+ const allChanged = new Set([...applied, ...urlChanged]);
314
+ for (const id of allChanged) {
315
+ const layer = this.deps.findLayer(id);
316
+ if (layer) layer._emit('popstate');
317
+ }
318
+ return {
319
+ isOk: true
320
+ };
321
+ }
322
+ resolveActiveLayer(id) {
323
+ const layer = this.deps.findLayer(id);
324
+ if (!layer) {
325
+ return undefined;
326
+ }
327
+ if (layer._isDestroyed()) {
328
+ return undefined;
329
+ }
330
+ if (!layer._isInActiveChain()) {
331
+ return undefined;
332
+ }
333
+ return layer;
334
+ }
335
+ resolveLowestCommonLayer(ids) {
336
+ if (ids.size === 0) return undefined;
337
+ const ancestorLists = [];
338
+ for (const id of ids) {
339
+ const layer = this.deps.findLayer(id);
340
+ if (!layer) continue;
341
+ const ancestors = [];
342
+ let node = layer;
343
+ while (node) {
344
+ var _node$parent;
345
+ ancestors.unshift(node);
346
+ node = (_node$parent = node.parent) !== null && _node$parent !== void 0 ? _node$parent : null;
347
+ }
348
+ ancestorLists.push(ancestors);
349
+ }
350
+ if (ancestorLists.length === 0) return undefined;
351
+ if (ancestorLists.length === 1) return ancestorLists[0][ancestorLists[0].length - 1];
352
+ let lca;
353
+ const minLen = Math.min(...ancestorLists.map(a => a.length));
354
+ for (let d = 0; d < minLen; d++) {
355
+ const candidate = ancestorLists[0][d];
356
+ if (ancestorLists.every(a => {
357
+ var _a$d;
358
+ return ((_a$d = a[d]) === null || _a$d === void 0 ? void 0 : _a$d.id) === candidate.id;
359
+ })) {
360
+ lca = candidate;
361
+ } else {
362
+ break;
363
+ }
364
+ }
365
+ return lca;
366
+ }
367
+ commit(isReplace) {
368
+ if (!hasWindowHistory()) return;
369
+ const url = this.deps.projectUrl();
370
+ const state = this.deps.projectState();
371
+ const idx = isReplace ? this.deps.getCurrentIdx() : this.deps.incrementIdx();
372
+ const stateWithMeta = {
373
+ ...state,
374
+ __chaynsHistory: {
375
+ ...state.__chaynsHistory,
376
+ __idx: idx
377
+ }
378
+ };
379
+ if (isReplace) {
380
+ window.history.replaceState(stateWithMeta, '', url);
381
+ } else {
382
+ window.history.pushState(stateWithMeta, '', url);
383
+ }
384
+ }
385
+ }
@@ -0,0 +1,24 @@
1
+ export function getChaynsHistoryActiveChain(root) {
2
+ const chain = [root];
3
+ let current = root;
4
+ for (;;) {
5
+ const childId = current.getActiveChildId();
6
+ if (!childId) break;
7
+ const child = current.getChildLayer(childId);
8
+ if (!child) break;
9
+ chain.push(child);
10
+ current = child;
11
+ }
12
+ return chain;
13
+ }
14
+ export function findChaynsHistoryLayerById(root, id) {
15
+ if (root.id === id) return root;
16
+ for (const child of root._getChildren().values()) {
17
+ const found = findChaynsHistoryLayerById(child, id);
18
+ if (found) return found;
19
+ }
20
+ return undefined;
21
+ }
22
+ export function isInChaynsHistoryActiveChain(layer) {
23
+ return layer._isInActiveChain();
24
+ }
@@ -0,0 +1,33 @@
1
+ import { hasWindowHistory } from './window';
2
+ let currentIdx = 0;
3
+ let pendingSilentCount = 0;
4
+ let silentResolve = null;
5
+ export function incrementIdx() {
6
+ return ++currentIdx;
7
+ }
8
+ export function getCurrentIdx() {
9
+ return currentIdx;
10
+ }
11
+ export function silentGo(delta) {
12
+ if (!hasWindowHistory()) return Promise.resolve();
13
+ return new Promise(resolve => {
14
+ pendingSilentCount++;
15
+ window.history.go(delta);
16
+ const timeout = setTimeout(() => {
17
+ silentResolve = null;
18
+ resolve();
19
+ }, 2000);
20
+ silentResolve = () => {
21
+ clearTimeout(timeout);
22
+ resolve();
23
+ };
24
+ });
25
+ }
26
+ export function consumeSilent() {
27
+ if (pendingSilentCount <= 0) return false;
28
+ pendingSilentCount--;
29
+ const res = silentResolve;
30
+ silentResolve = null;
31
+ res === null || res === void 0 || res();
32
+ return true;
33
+ }
@@ -0,0 +1,167 @@
1
+ import { ChaynsHistoryLayer } from '../../handler/history/HistoryLayer';
2
+ import { NavigationQueue } from './NavigationQueue';
3
+ import { BlockRegistry } from './BlockRegistry';
4
+ import { findChaynsHistoryLayerById } from './layerTree';
5
+ import { projectToUrl, parseFromUrl } from './url';
6
+ import { projectToState, applyStateToTree, diffIncomingState, hasChaynsHistoryState } from './stateProjector';
7
+ import { silentGo, consumeSilent, incrementIdx, getCurrentIdx } from './navigationIndex';
8
+ import { hasWindowHistory } from './window';
9
+ import { shallowEqualArr } from '../equality';
10
+ import { getSite } from "../../calls";
11
+ import { normalizeHistorySegments } from './segments';
12
+ function getInitialPathname(overrideUrl) {
13
+ if (overrideUrl) {
14
+ try {
15
+ return new URL(overrideUrl).pathname;
16
+ } catch {}
17
+ return overrideUrl.startsWith('/') ? overrideUrl : `/${overrideUrl}`;
18
+ }
19
+ if (hasWindowHistory()) {
20
+ return window.location.pathname;
21
+ }
22
+ try {
23
+ const siteUrl = getSite().url;
24
+ if (siteUrl) return new URL(siteUrl).pathname;
25
+ } catch {}
26
+ return '/';
27
+ }
28
+ function resolveInitialSegments(overrideUrl, n) {
29
+ const pathname = getInitialPathname(overrideUrl);
30
+ const all = pathname.replace(/^\//, '').split('/').filter(Boolean);
31
+ const taken = all.slice(0, n);
32
+ while (taken.length < n) taken.push('');
33
+ return taken;
34
+ }
35
+ export function resolveSegmentsFrom(overrideUrl, startIndex) {
36
+ const pathname = getInitialPathname(overrideUrl);
37
+ const all = pathname.replace(/^\//, '').split('/').filter(Boolean);
38
+ return all.slice(startIndex);
39
+ }
40
+ export function initRootChaynsHistoryLayer(opts = {}) {
41
+ var _opts$segmentCount;
42
+ const blockRegistry = new BlockRegistry();
43
+ let rootLayer;
44
+ let queue;
45
+ const deps = {
46
+ getRoot: () => rootLayer,
47
+ getQueue: () => queue,
48
+ getBlockRegistry: () => blockRegistry
49
+ };
50
+ rootLayer = new ChaynsHistoryLayer({
51
+ id: 'root',
52
+ parent: null,
53
+ deps,
54
+ segmentCount: (_opts$segmentCount = opts.segmentCount) !== null && _opts$segmentCount !== void 0 ? _opts$segmentCount : 0,
55
+ segments: opts.segmentCount ? resolveInitialSegments(opts.url, opts.segmentCount) : []
56
+ });
57
+ queue = new NavigationQueue({
58
+ getRoot: () => rootLayer,
59
+ findLayer: id => findChaynsHistoryLayerById(rootLayer, id),
60
+ checkBlocks: target => blockRegistry.checkBlocks(target),
61
+ projectUrl: () => projectToUrl(rootLayer),
62
+ projectState: () => {
63
+ var _ref;
64
+ const existing = hasWindowHistory() ? {
65
+ ...((_ref = window.history.state) !== null && _ref !== void 0 ? _ref : {})
66
+ } : {};
67
+ delete existing.__chaynsHistory;
68
+ return projectToState(rootLayer, existing);
69
+ },
70
+ diffIncomingState: raw => diffIncomingState(rootLayer, raw),
71
+ applyIncomingState: raw => applyStateToTree(rootLayer, raw),
72
+ silentGo: delta => silentGo(delta),
73
+ getCurrentIdx: () => getCurrentIdx(),
74
+ incrementIdx: () => incrementIdx(),
75
+ applyUrlSegments: () => {
76
+ if (!hasWindowHistory()) return {
77
+ changedLayerIds: new Set()
78
+ };
79
+ const {
80
+ perLayerSegments
81
+ } = parseFromUrl(window.location.pathname, rootLayer);
82
+ const changed = new Set();
83
+ for (const [id, segs] of perLayerSegments) {
84
+ const layer = findChaynsHistoryLayerById(rootLayer, id);
85
+ if (!layer) continue;
86
+ const normalizedSegs = normalizeHistorySegments(segs);
87
+ const prev = layer._getOwnSegments();
88
+ if (!shallowEqualArr(prev, normalizedSegs)) {
89
+ layer._setOwnSegmentsSilent(normalizedSegs);
90
+ changed.add(id);
91
+ }
92
+ }
93
+ return {
94
+ changedLayerIds: changed
95
+ };
96
+ }
97
+ });
98
+ const existingState = hasWindowHistory() ? window.history.state : null;
99
+ if (!hasChaynsHistoryState(existingState)) {
100
+ var _opts$segmentCount2, _opts$url;
101
+ const segmentCount = (_opts$segmentCount2 = opts.segmentCount) !== null && _opts$segmentCount2 !== void 0 ? _opts$segmentCount2 : 0;
102
+ if (segmentCount > 0) {
103
+ const pathname = getInitialPathname(opts.url);
104
+ const all = pathname.replace(/^\//, '').split('/').filter(Boolean);
105
+ rootLayer._setBootstrapPool(all.slice(segmentCount));
106
+ } else {
107
+ rootLayer._setBootstrapUrlResolver(() => getInitialPathname(opts.url));
108
+ }
109
+ const rawUrl = (_opts$url = opts.url) !== null && _opts$url !== void 0 ? _opts$url : hasWindowHistory() ? window.location.href : null;
110
+ if (rawUrl) {
111
+ try {
112
+ const base = rawUrl.startsWith('http') ? rawUrl : `http://x${rawUrl.startsWith('/') ? rawUrl : '/' + rawUrl}`;
113
+ const parsed = new URL(base);
114
+ const params = {};
115
+ parsed.searchParams.forEach((v, k) => {
116
+ params[k] = v;
117
+ });
118
+ if (Object.keys(params).length > 0) rootLayer._setOwnParamsSilent(params);
119
+ const hash = parsed.hash.replace(/^#/, '');
120
+ if (hash) rootLayer._setOwnHashSilent(hash);
121
+ } catch {}
122
+ }
123
+ }
124
+ if (hasWindowHistory()) {
125
+ const existing = existingState;
126
+ if (!hasChaynsHistoryState(existing)) {
127
+ const foreign = {
128
+ ...(existing !== null && existing !== void 0 ? existing : {})
129
+ };
130
+ delete foreign.__chaynsHistory;
131
+ const initialState = projectToState(rootLayer, foreign);
132
+ const idx = incrementIdx();
133
+ window.history.replaceState({
134
+ ...initialState,
135
+ __chaynsHistory: {
136
+ ...initialState.__chaynsHistory,
137
+ __idx: idx
138
+ }
139
+ }, '', window.location.href);
140
+ } else {
141
+ applyStateToTree(rootLayer, existing);
142
+ }
143
+ window.addEventListener('popstate', event => {
144
+ if (consumeSilent()) return;
145
+ const raw = event.state;
146
+ if (!hasChaynsHistoryState(raw)) {} else {
147
+ void queue.enqueue({
148
+ kind: 'popstate',
149
+ rawState: raw
150
+ });
151
+ }
152
+ });
153
+ }
154
+ return {
155
+ rootLayer
156
+ };
157
+ }
158
+ let _rootLayerResult = null;
159
+ export function getOrInitRootChaynsHistoryLayer(url, segmentCount) {
160
+ if (!_rootLayerResult) {
161
+ _rootLayerResult = initRootChaynsHistoryLayer({
162
+ url,
163
+ segmentCount
164
+ });
165
+ }
166
+ return _rootLayerResult;
167
+ }
@@ -0,0 +1,7 @@
1
+ export const normalizeHistorySegments = segments => segments.filter(segment => segment.length > 0);
2
+ export const normalizeHistoryRouteInput = route => {
3
+ if (Array.isArray(route)) {
4
+ return normalizeHistorySegments([...route]);
5
+ }
6
+ return route.split('/').filter(Boolean);
7
+ };