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,321 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.ChaynsHistoryLayer = void 0;
7
+ var _EventBus = require("../../utils/EventBus");
8
+ var _rootLayer = require("../../utils/history/rootLayer");
9
+ var _segments = require("../../utils/history/segments");
10
+ const RESERVED_STATE_KEYS = ['activeChild', 'childState', '__params', '__hash'];
11
+ class ChaynsHistoryLayer {
12
+ children = new Map();
13
+ activeChildId = null;
14
+ segmentCount = 0;
15
+ segments = [];
16
+ ownState = {};
17
+ _params = {};
18
+ _hash = undefined;
19
+ bus = new _EventBus.EventBus();
20
+ _bootstrapPool = null;
21
+ _bootstrapUrlResolver = null;
22
+ isDestroyed = false;
23
+ constructor(init) {
24
+ var _init$segmentCount, _init$segments;
25
+ this.id = init.id;
26
+ this.parent = init.parent;
27
+ this.depth = init.parent ? init.parent.depth + 1 : 0;
28
+ this.deps = init.deps;
29
+ this.segmentCount = (_init$segmentCount = init.segmentCount) !== null && _init$segmentCount !== void 0 ? _init$segmentCount : 0;
30
+ this.segments = (0, _segments.normalizeHistorySegments)((_init$segments = init.segments) !== null && _init$segments !== void 0 ? _init$segments : []);
31
+ }
32
+ getSegmentCount() {
33
+ return this.segmentCount;
34
+ }
35
+ setSegmentCount(n) {
36
+ if (n < 0 || !Number.isInteger(n)) {
37
+ return;
38
+ }
39
+ const prev = this.segmentCount;
40
+ if (n === prev) {
41
+ return;
42
+ }
43
+ this.segmentCount = n;
44
+ if (this._isInActiveChain()) {
45
+ if (n < prev) {
46
+ const excess = this.segments.splice(n);
47
+ if (excess.length > 0 && this.activeChildId) {
48
+ const child = this.children.get(this.activeChildId);
49
+ if (child) {
50
+ child._setOwnSegmentsSilent([...excess, ...child._getOwnSegments()]);
51
+ }
52
+ }
53
+ } else if (n > prev && this.segments.length === 0) {
54
+ const root = this.deps.getRoot();
55
+ const bootstrapSegs = root._consumeBootstrapSegments(n);
56
+ if (bootstrapSegs) {
57
+ this.segments = (0, _segments.normalizeHistorySegments)(bootstrapSegs);
58
+ }
59
+ }
60
+ void this.deps.getQueue().enqueue({
61
+ kind: 'setRoute',
62
+ layerId: this.id,
63
+ segments: [...this.segments],
64
+ opts: {},
65
+ _notifyEvenIfUnchanged: true,
66
+ _skipCommit: true
67
+ });
68
+ }
69
+ }
70
+ createChildLayer(id) {
71
+ if (this.children.has(id)) {
72
+ throw new Error(`[chaynsHistory] Child layer with id "${id}" already exists on layer "${this.id}".`);
73
+ }
74
+ const segments = (0, _rootLayer.resolveSegmentsFrom)(undefined, this.getCumulativeSegmentCount());
75
+ const child = new ChaynsHistoryLayer({
76
+ id,
77
+ parent: this,
78
+ deps: this.deps,
79
+ segments,
80
+ segmentCount: segments.length
81
+ });
82
+ this.children.set(id, child);
83
+ return child;
84
+ }
85
+ destroyChildLayer(id) {
86
+ const child = this.children.get(id);
87
+ if (!child) return;
88
+ child.markDestroyed();
89
+ this.children.delete(id);
90
+ if (this.activeChildId === id) {
91
+ void this.deps.getQueue().enqueue({
92
+ kind: 'setActiveChild',
93
+ layerId: this.id,
94
+ childId: null
95
+ });
96
+ }
97
+ }
98
+ getChildLayer(id) {
99
+ return this.children.get(id);
100
+ }
101
+ getActiveChildId() {
102
+ return this.activeChildId;
103
+ }
104
+ setActiveChild(id, init) {
105
+ if (this.isDestroyed) return Promise.resolve({
106
+ isOk: false,
107
+ reason: 'destroyed'
108
+ });
109
+ return this.deps.getQueue().enqueue({
110
+ kind: 'setActiveChild',
111
+ layerId: this.id,
112
+ childId: id,
113
+ init: init ? {
114
+ ...init,
115
+ route: init.route !== undefined ? ChaynsHistoryLayer.normalizeRoute(init.route) : undefined
116
+ } : undefined
117
+ });
118
+ }
119
+ getRoute() {
120
+ return [...this.segments];
121
+ }
122
+ setRoute(route, opts) {
123
+ if (this.isDestroyed) return;
124
+ void this.deps.getQueue().enqueue({
125
+ kind: 'setRoute',
126
+ layerId: this.id,
127
+ segments: ChaynsHistoryLayer.normalizeRoute(route),
128
+ opts: opts !== null && opts !== void 0 ? opts : {}
129
+ });
130
+ }
131
+ getParams() {
132
+ return {
133
+ ...this._params
134
+ };
135
+ }
136
+ setParams(params, opts) {
137
+ if (this.isDestroyed) return;
138
+ void this.deps.getQueue().enqueue({
139
+ kind: 'setParams',
140
+ layerId: this.id,
141
+ params: {
142
+ ...params
143
+ },
144
+ opts: opts !== null && opts !== void 0 ? opts : {}
145
+ });
146
+ }
147
+ getHash() {
148
+ var _this$_hash;
149
+ return (_this$_hash = this._hash) !== null && _this$_hash !== void 0 ? _this$_hash : '';
150
+ }
151
+ setHash(hash, opts) {
152
+ if (this.isDestroyed) return;
153
+ const normalized = hash.startsWith('#') ? hash.slice(1) : hash;
154
+ void this.deps.getQueue().enqueue({
155
+ kind: 'setHash',
156
+ layerId: this.id,
157
+ hash: normalized,
158
+ opts: opts !== null && opts !== void 0 ? opts : {}
159
+ });
160
+ }
161
+ getState() {
162
+ return {
163
+ ...this.ownState
164
+ };
165
+ }
166
+ setState(state, opts) {
167
+ if (this.isDestroyed) return;
168
+ const filtered = ChaynsHistoryLayer.filterReservedKeys(state);
169
+ void this.deps.getQueue().enqueue({
170
+ kind: 'setState',
171
+ layerId: this.id,
172
+ state: filtered,
173
+ opts: opts !== null && opts !== void 0 ? opts : {}
174
+ });
175
+ }
176
+ navigate(opts) {
177
+ if (this.isDestroyed) return Promise.resolve({
178
+ isOk: false,
179
+ reason: 'destroyed'
180
+ });
181
+ const {
182
+ route,
183
+ state,
184
+ params,
185
+ hash,
186
+ activeChild,
187
+ activeChildInit,
188
+ ...rest
189
+ } = opts;
190
+ return this.deps.getQueue().enqueue({
191
+ kind: 'navigate',
192
+ layerId: this.id,
193
+ route: route !== undefined ? ChaynsHistoryLayer.normalizeRoute(route) : undefined,
194
+ state: state ? ChaynsHistoryLayer.filterReservedKeys(state) : undefined,
195
+ params,
196
+ hash: hash !== undefined ? hash.startsWith('#') ? hash.slice(1) : hash : undefined,
197
+ activeChild,
198
+ activeChildInit: activeChildInit ? {
199
+ ...activeChildInit,
200
+ route: activeChildInit.route !== undefined ? ChaynsHistoryLayer.normalizeRoute(activeChildInit.route) : undefined
201
+ } : undefined,
202
+ opts: rest
203
+ });
204
+ }
205
+ addBlock(callback, opts = {}) {
206
+ return this.deps.getBlockRegistry().add(this, callback, opts);
207
+ }
208
+ addEventListener(type, handler) {
209
+ return this.bus.on(type, handler);
210
+ }
211
+ _getOwnState() {
212
+ return this.ownState;
213
+ }
214
+ _getOwnSegments() {
215
+ return this.segments;
216
+ }
217
+ _getChildren() {
218
+ return this.children;
219
+ }
220
+ _setOwnStateSilent(next) {
221
+ this.ownState = ChaynsHistoryLayer.filterReservedKeys(next);
222
+ }
223
+ _setOwnSegmentsSilent(next) {
224
+ this.segments = (0, _segments.normalizeHistorySegments)(next);
225
+ }
226
+ _getOwnParams() {
227
+ return this._params;
228
+ }
229
+ _setOwnParamsSilent(params) {
230
+ this._params = {
231
+ ...params
232
+ };
233
+ }
234
+ _getOwnHash() {
235
+ return this._hash;
236
+ }
237
+ _setOwnHashSilent(hash) {
238
+ this._hash = hash;
239
+ }
240
+ _setActiveChildSilent(id) {
241
+ this.activeChildId = id;
242
+ }
243
+ _emit(type) {
244
+ var _this$_hash2;
245
+ const event = {
246
+ type,
247
+ layerId: this.id,
248
+ segments: [...this.segments],
249
+ state: {
250
+ ...this.ownState
251
+ },
252
+ params: {
253
+ ...this._params
254
+ },
255
+ hash: (_this$_hash2 = this._hash) !== null && _this$_hash2 !== void 0 ? _this$_hash2 : ''
256
+ };
257
+ this.bus.emit(type, event);
258
+ }
259
+ _isInActiveChain() {
260
+ let node = this;
261
+ while (node && node.parent) {
262
+ if (node.parent.activeChildId !== node.id) return false;
263
+ node = node.parent;
264
+ }
265
+ return true;
266
+ }
267
+ _setBootstrapUrlResolver(resolver) {
268
+ this._bootstrapUrlResolver = resolver;
269
+ }
270
+ _setBootstrapPool(segs) {
271
+ this._bootstrapPool = [...segs];
272
+ }
273
+ _consumeBootstrapSegments(n) {
274
+ if (this._bootstrapPool === null && this._bootstrapUrlResolver) {
275
+ const pathname = this._bootstrapUrlResolver();
276
+ this._bootstrapPool = pathname.replace(/^\//, '').split('/').filter(Boolean);
277
+ this._bootstrapUrlResolver = null;
278
+ }
279
+ if (!this._bootstrapPool || this._bootstrapPool.length === 0 || n === 0) return null;
280
+ const taken = this._bootstrapPool.splice(0, n);
281
+ while (taken.length < n) taken.push('');
282
+ return taken;
283
+ }
284
+ _isDestroyed() {
285
+ return this.isDestroyed;
286
+ }
287
+ markDestroyed() {
288
+ this.isDestroyed = true;
289
+ try {
290
+ this.deps.getBlockRegistry().removeAllForLayer(this.id);
291
+ } catch {}
292
+ for (const child of this.children.values()) {
293
+ child.markDestroyed();
294
+ }
295
+ this.children.clear();
296
+ this.bus.clear();
297
+ }
298
+ getCumulativeSegmentCount() {
299
+ let count = this.segmentCount;
300
+ let node = this.parent;
301
+ while (node) {
302
+ count += node.segmentCount;
303
+ node = node.parent;
304
+ }
305
+ return count;
306
+ }
307
+ static normalizeRoute(route) {
308
+ return (0, _segments.normalizeHistoryRouteInput)(route);
309
+ }
310
+ static filterReservedKeys(input) {
311
+ const out = {};
312
+ for (const key of Object.keys(input)) {
313
+ if (RESERVED_STATE_KEYS.includes(key)) {
314
+ continue;
315
+ }
316
+ out[key] = input[key];
317
+ }
318
+ return out;
319
+ }
320
+ }
321
+ exports.ChaynsHistoryLayer = ChaynsHistoryLayer;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "ChaynsHistoryLayerClass", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _HistoryLayer.ChaynsHistoryLayer;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "FrameHistoryLayer", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _FrameHistoryLayer.FrameHistoryLayer;
16
+ }
17
+ });
18
+ var _HistoryLayer = require("./HistoryLayer");
19
+ var _FrameHistoryLayer = require("./FrameHistoryLayer");