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