@finesoft/front 0.1.3 → 0.1.11
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/README.md +153 -9
- package/dist/browser.cjs +1214 -0
- package/dist/browser.cjs.map +1 -0
- package/dist/browser.d.cts +751 -0
- package/dist/browser.d.ts +751 -0
- package/dist/browser.js +97 -0
- package/dist/browser.js.map +1 -0
- package/dist/chunk-OVGQ4NUA.js +1143 -0
- package/dist/chunk-OVGQ4NUA.js.map +1 -0
- package/dist/index.cjs +130 -34
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +16 -749
- package/dist/index.d.ts +16 -749
- package/dist/index.js +169 -1119
- package/dist/index.js.map +1 -1
- package/package.json +11 -6
|
@@ -0,0 +1,1143 @@
|
|
|
1
|
+
// ../core/src/actions/types.ts
|
|
2
|
+
var ACTION_KINDS = {
|
|
3
|
+
FLOW: "flow",
|
|
4
|
+
EXTERNAL_URL: "externalUrl",
|
|
5
|
+
COMPOUND: "compound"
|
|
6
|
+
};
|
|
7
|
+
function isFlowAction(action) {
|
|
8
|
+
return action.kind === ACTION_KINDS.FLOW;
|
|
9
|
+
}
|
|
10
|
+
function isExternalUrlAction(action) {
|
|
11
|
+
return action.kind === ACTION_KINDS.EXTERNAL_URL;
|
|
12
|
+
}
|
|
13
|
+
function isCompoundAction(action) {
|
|
14
|
+
return action.kind === ACTION_KINDS.COMPOUND;
|
|
15
|
+
}
|
|
16
|
+
function makeFlowAction(url, presentationContext) {
|
|
17
|
+
return { kind: ACTION_KINDS.FLOW, url, presentationContext };
|
|
18
|
+
}
|
|
19
|
+
function makeExternalUrlAction(url) {
|
|
20
|
+
return { kind: ACTION_KINDS.EXTERNAL_URL, url };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// ../core/src/actions/dispatcher.ts
|
|
24
|
+
var ActionDispatcher = class {
|
|
25
|
+
handlers = /* @__PURE__ */ new Map();
|
|
26
|
+
wiredActions = /* @__PURE__ */ new Set();
|
|
27
|
+
/** 注册指定 kind 的 handler(防止重复注册) */
|
|
28
|
+
onAction(kind, handler) {
|
|
29
|
+
if (this.wiredActions.has(kind)) {
|
|
30
|
+
console.warn(
|
|
31
|
+
`[ActionDispatcher] kind="${kind}" already registered, skipping`
|
|
32
|
+
);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
this.wiredActions.add(kind);
|
|
36
|
+
this.handlers.set(kind, handler);
|
|
37
|
+
}
|
|
38
|
+
/** 执行一个 Action(CompoundAction 递归展开) */
|
|
39
|
+
async perform(action) {
|
|
40
|
+
if (isCompoundAction(action)) {
|
|
41
|
+
for (const subAction of action.actions) {
|
|
42
|
+
await this.perform(subAction);
|
|
43
|
+
}
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const handler = this.handlers.get(action.kind);
|
|
47
|
+
if (!handler) {
|
|
48
|
+
console.warn(
|
|
49
|
+
`[ActionDispatcher] No handler for kind="${action.kind}"`
|
|
50
|
+
);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
await handler(action);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// ../core/src/intents/dispatcher.ts
|
|
58
|
+
var IntentDispatcher = class {
|
|
59
|
+
controllers = /* @__PURE__ */ new Map();
|
|
60
|
+
/** 注册一个 IntentController */
|
|
61
|
+
register(controller) {
|
|
62
|
+
this.controllers.set(controller.intentId, controller);
|
|
63
|
+
}
|
|
64
|
+
/** 分发 Intent 到对应 Controller */
|
|
65
|
+
async dispatch(intent, container) {
|
|
66
|
+
const controller = this.controllers.get(intent.id);
|
|
67
|
+
if (!controller) {
|
|
68
|
+
throw new Error(
|
|
69
|
+
`[IntentDispatcher] No controller for "${intent.id}". Registered: [${Array.from(this.controllers.keys()).join(", ")}]`
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
return controller.perform(intent, container);
|
|
73
|
+
}
|
|
74
|
+
/** 检查是否已注册某个 Intent */
|
|
75
|
+
has(intentId) {
|
|
76
|
+
return this.controllers.has(intentId);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// ../core/src/dependencies/container.ts
|
|
81
|
+
var Container = class {
|
|
82
|
+
registrations = /* @__PURE__ */ new Map();
|
|
83
|
+
/** 注册依赖(默认单例) */
|
|
84
|
+
register(key, factory, singleton = true) {
|
|
85
|
+
this.registrations.set(key, { factory, singleton });
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
/** 解析依赖 */
|
|
89
|
+
resolve(key) {
|
|
90
|
+
const reg = this.registrations.get(key);
|
|
91
|
+
if (!reg) {
|
|
92
|
+
throw new Error(`[Container] No registration for key: "${key}"`);
|
|
93
|
+
}
|
|
94
|
+
if (reg.singleton) {
|
|
95
|
+
if (reg.instance === void 0) {
|
|
96
|
+
reg.instance = reg.factory();
|
|
97
|
+
}
|
|
98
|
+
return reg.instance;
|
|
99
|
+
}
|
|
100
|
+
return reg.factory();
|
|
101
|
+
}
|
|
102
|
+
/** 检查是否已注册 */
|
|
103
|
+
has(key) {
|
|
104
|
+
return this.registrations.has(key);
|
|
105
|
+
}
|
|
106
|
+
/** 销毁容器,清除所有缓存 */
|
|
107
|
+
dispose() {
|
|
108
|
+
for (const reg of this.registrations.values()) {
|
|
109
|
+
reg.instance = void 0;
|
|
110
|
+
}
|
|
111
|
+
this.registrations.clear();
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
// ../core/src/logger/base.ts
|
|
116
|
+
var BaseLogger = class {
|
|
117
|
+
category;
|
|
118
|
+
constructor(category) {
|
|
119
|
+
this.category = category;
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// ../core/src/logger/local-storage-filter.ts
|
|
124
|
+
var LEVEL_TO_NUM = {
|
|
125
|
+
"*": 4,
|
|
126
|
+
debug: 4,
|
|
127
|
+
info: 3,
|
|
128
|
+
warn: 2,
|
|
129
|
+
error: 1,
|
|
130
|
+
off: 0,
|
|
131
|
+
"": 0
|
|
132
|
+
};
|
|
133
|
+
var cachedRules;
|
|
134
|
+
var cachedRaw;
|
|
135
|
+
function parseRules() {
|
|
136
|
+
if (typeof globalThis.localStorage === "undefined") {
|
|
137
|
+
return {};
|
|
138
|
+
}
|
|
139
|
+
let raw;
|
|
140
|
+
try {
|
|
141
|
+
raw = globalThis.localStorage.getItem("onyxLog");
|
|
142
|
+
} catch {
|
|
143
|
+
return {};
|
|
144
|
+
}
|
|
145
|
+
if (!raw) return {};
|
|
146
|
+
if (raw === cachedRaw && cachedRules) return cachedRules;
|
|
147
|
+
cachedRaw = raw;
|
|
148
|
+
const rules = {};
|
|
149
|
+
const parts = raw.split(",");
|
|
150
|
+
for (const part of parts) {
|
|
151
|
+
const [name, level] = part.trim().split("=");
|
|
152
|
+
if (!name || level === void 0) continue;
|
|
153
|
+
const num = LEVEL_TO_NUM[level.toLowerCase()] ?? void 0;
|
|
154
|
+
if (num === void 0) continue;
|
|
155
|
+
if (name === "*") {
|
|
156
|
+
rules.defaultLevel = num;
|
|
157
|
+
} else {
|
|
158
|
+
rules.named ??= {};
|
|
159
|
+
rules.named[name] = num;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
cachedRules = rules;
|
|
163
|
+
return rules;
|
|
164
|
+
}
|
|
165
|
+
function shouldLog(name, level) {
|
|
166
|
+
const rules = parseRules();
|
|
167
|
+
if (rules.defaultLevel === void 0 && !rules.named) {
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
const currentNum = LEVEL_TO_NUM[level] ?? 4;
|
|
171
|
+
if (rules.named?.[name] !== void 0) {
|
|
172
|
+
return currentNum <= rules.named[name];
|
|
173
|
+
}
|
|
174
|
+
if (rules.defaultLevel !== void 0) {
|
|
175
|
+
return currentNum <= rules.defaultLevel;
|
|
176
|
+
}
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
function resetFilterCache() {
|
|
180
|
+
cachedRules = void 0;
|
|
181
|
+
cachedRaw = void 0;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// ../core/src/logger/console.ts
|
|
185
|
+
var ConsoleLogger = class extends BaseLogger {
|
|
186
|
+
debug(...args) {
|
|
187
|
+
if (shouldLog(this.category, "debug")) {
|
|
188
|
+
console.debug(`[${this.category}]`, ...args);
|
|
189
|
+
}
|
|
190
|
+
return "";
|
|
191
|
+
}
|
|
192
|
+
info(...args) {
|
|
193
|
+
if (shouldLog(this.category, "info")) {
|
|
194
|
+
console.info(`[${this.category}]`, ...args);
|
|
195
|
+
}
|
|
196
|
+
return "";
|
|
197
|
+
}
|
|
198
|
+
warn(...args) {
|
|
199
|
+
if (shouldLog(this.category, "warn")) {
|
|
200
|
+
console.warn(`[${this.category}]`, ...args);
|
|
201
|
+
}
|
|
202
|
+
return "";
|
|
203
|
+
}
|
|
204
|
+
error(...args) {
|
|
205
|
+
console.error(`[${this.category}]`, ...args);
|
|
206
|
+
return "";
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
var ConsoleLoggerFactory = class {
|
|
210
|
+
loggerFor(category) {
|
|
211
|
+
return new ConsoleLogger(category);
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
// ../core/src/dependencies/make-dependencies.ts
|
|
216
|
+
var DEP_KEYS = {
|
|
217
|
+
LOGGER: "logger",
|
|
218
|
+
LOGGER_FACTORY: "loggerFactory",
|
|
219
|
+
NET: "net",
|
|
220
|
+
LOCALE: "locale",
|
|
221
|
+
STORAGE: "storage",
|
|
222
|
+
FEATURE_FLAGS: "featureFlags",
|
|
223
|
+
METRICS: "metrics",
|
|
224
|
+
FETCH: "fetch"
|
|
225
|
+
};
|
|
226
|
+
var DefaultLocale = class {
|
|
227
|
+
language = "en";
|
|
228
|
+
storefront = "us";
|
|
229
|
+
setActiveLocale(language, storefront) {
|
|
230
|
+
this.language = language;
|
|
231
|
+
this.storefront = storefront;
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
var MemoryStorage = class {
|
|
235
|
+
store = /* @__PURE__ */ new Map();
|
|
236
|
+
get(key) {
|
|
237
|
+
return this.store.get(key);
|
|
238
|
+
}
|
|
239
|
+
set(key, value) {
|
|
240
|
+
this.store.set(key, value);
|
|
241
|
+
}
|
|
242
|
+
delete(key) {
|
|
243
|
+
this.store.delete(key);
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
var DefaultFeatureFlags = class {
|
|
247
|
+
flags;
|
|
248
|
+
constructor(flags = {}) {
|
|
249
|
+
this.flags = flags;
|
|
250
|
+
}
|
|
251
|
+
isEnabled(key) {
|
|
252
|
+
return this.flags[key] === true;
|
|
253
|
+
}
|
|
254
|
+
getString(key) {
|
|
255
|
+
const v = this.flags[key];
|
|
256
|
+
return typeof v === "string" ? v : void 0;
|
|
257
|
+
}
|
|
258
|
+
getNumber(key) {
|
|
259
|
+
const v = this.flags[key];
|
|
260
|
+
return typeof v === "number" ? v : void 0;
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
var ConsoleMetrics = class {
|
|
264
|
+
recordPageView(page, fields) {
|
|
265
|
+
console.info(`[Metrics:PageView] ${page}`, fields ?? "");
|
|
266
|
+
}
|
|
267
|
+
recordEvent(name, fields) {
|
|
268
|
+
console.info(`[Metrics:Event] ${name}`, fields ?? "");
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
function makeDependencies(container, options = {}) {
|
|
272
|
+
const {
|
|
273
|
+
fetch: fetchFn = globalThis.fetch?.bind(globalThis),
|
|
274
|
+
language = "en",
|
|
275
|
+
storefront = "us",
|
|
276
|
+
featureFlags = {}
|
|
277
|
+
} = options;
|
|
278
|
+
const loggerFactory = new ConsoleLoggerFactory();
|
|
279
|
+
container.register(
|
|
280
|
+
DEP_KEYS.LOGGER_FACTORY,
|
|
281
|
+
() => loggerFactory
|
|
282
|
+
);
|
|
283
|
+
container.register(
|
|
284
|
+
DEP_KEYS.LOGGER,
|
|
285
|
+
() => loggerFactory.loggerFor("framework")
|
|
286
|
+
);
|
|
287
|
+
container.register(DEP_KEYS.NET, () => ({
|
|
288
|
+
fetch: (url, opts) => fetchFn(url, opts)
|
|
289
|
+
}));
|
|
290
|
+
container.register(DEP_KEYS.LOCALE, () => {
|
|
291
|
+
const locale = new DefaultLocale();
|
|
292
|
+
locale.setActiveLocale(language, storefront);
|
|
293
|
+
return locale;
|
|
294
|
+
});
|
|
295
|
+
container.register(DEP_KEYS.STORAGE, () => new MemoryStorage());
|
|
296
|
+
container.register(
|
|
297
|
+
DEP_KEYS.FEATURE_FLAGS,
|
|
298
|
+
() => new DefaultFeatureFlags(featureFlags)
|
|
299
|
+
);
|
|
300
|
+
container.register(
|
|
301
|
+
DEP_KEYS.METRICS,
|
|
302
|
+
() => new ConsoleMetrics()
|
|
303
|
+
);
|
|
304
|
+
container.register(DEP_KEYS.FETCH, () => fetchFn);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// ../core/src/router/router.ts
|
|
308
|
+
var Router = class {
|
|
309
|
+
routes = [];
|
|
310
|
+
/** 添加路由规则 */
|
|
311
|
+
add(pattern, intentId) {
|
|
312
|
+
const paramNames = [];
|
|
313
|
+
const regexStr = pattern.replace(
|
|
314
|
+
/\/:(\w+)(\?)?/g,
|
|
315
|
+
(_, name, optional) => {
|
|
316
|
+
paramNames.push(name);
|
|
317
|
+
return optional ? "(?:/([^/]+))?" : "/([^/]+)";
|
|
318
|
+
}
|
|
319
|
+
);
|
|
320
|
+
this.routes.push({
|
|
321
|
+
pattern,
|
|
322
|
+
intentId,
|
|
323
|
+
regex: new RegExp(`^${regexStr}/?$`),
|
|
324
|
+
paramNames
|
|
325
|
+
});
|
|
326
|
+
return this;
|
|
327
|
+
}
|
|
328
|
+
/** 解析 URL → RouteMatch */
|
|
329
|
+
resolve(urlOrPath) {
|
|
330
|
+
const path = this.extractPath(urlOrPath);
|
|
331
|
+
const queryParams = this.extractQueryParams(urlOrPath);
|
|
332
|
+
for (const route of this.routes) {
|
|
333
|
+
const match = path.match(route.regex);
|
|
334
|
+
if (match) {
|
|
335
|
+
const params = {};
|
|
336
|
+
route.paramNames.forEach((name, index) => {
|
|
337
|
+
const value = match[index + 1];
|
|
338
|
+
if (value) params[name] = value;
|
|
339
|
+
});
|
|
340
|
+
for (const [k, v] of Object.entries(queryParams)) {
|
|
341
|
+
if (!(k in params)) params[k] = v;
|
|
342
|
+
}
|
|
343
|
+
return {
|
|
344
|
+
intent: { id: route.intentId, params },
|
|
345
|
+
action: makeFlowAction(urlOrPath)
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
return null;
|
|
350
|
+
}
|
|
351
|
+
/** 获取所有已注册的路由 */
|
|
352
|
+
getRoutes() {
|
|
353
|
+
return this.routes.map((r) => `${r.pattern} \u2192 ${r.intentId}`);
|
|
354
|
+
}
|
|
355
|
+
extractPath(url) {
|
|
356
|
+
try {
|
|
357
|
+
const parsed = new URL(url, "http://localhost");
|
|
358
|
+
return parsed.pathname;
|
|
359
|
+
} catch {
|
|
360
|
+
return url.split("?")[0].split("#")[0];
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
extractQueryParams(url) {
|
|
364
|
+
try {
|
|
365
|
+
const parsed = new URL(url, "http://localhost");
|
|
366
|
+
const params = {};
|
|
367
|
+
parsed.searchParams.forEach((v, k) => {
|
|
368
|
+
params[k] = v;
|
|
369
|
+
});
|
|
370
|
+
return params;
|
|
371
|
+
} catch {
|
|
372
|
+
return {};
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
// ../core/src/logger/composite.ts
|
|
378
|
+
var CompositeLoggerFactory = class {
|
|
379
|
+
constructor(factories) {
|
|
380
|
+
this.factories = factories;
|
|
381
|
+
}
|
|
382
|
+
loggerFor(name) {
|
|
383
|
+
return new CompositeLogger(
|
|
384
|
+
this.factories.map((f) => f.loggerFor(name))
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
var CompositeLogger = class {
|
|
389
|
+
constructor(loggers) {
|
|
390
|
+
this.loggers = loggers;
|
|
391
|
+
}
|
|
392
|
+
debug(...args) {
|
|
393
|
+
return this.callAll("debug", args);
|
|
394
|
+
}
|
|
395
|
+
info(...args) {
|
|
396
|
+
return this.callAll("info", args);
|
|
397
|
+
}
|
|
398
|
+
warn(...args) {
|
|
399
|
+
return this.callAll("warn", args);
|
|
400
|
+
}
|
|
401
|
+
error(...args) {
|
|
402
|
+
return this.callAll("error", args);
|
|
403
|
+
}
|
|
404
|
+
callAll(method, args) {
|
|
405
|
+
for (const logger of this.loggers) {
|
|
406
|
+
logger[method](...args);
|
|
407
|
+
}
|
|
408
|
+
return "";
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
// ../core/src/prefetched-intents/stable-stringify.ts
|
|
413
|
+
function stableStringify(obj) {
|
|
414
|
+
if (obj === null || obj === void 0) return String(obj);
|
|
415
|
+
if (typeof obj !== "object") return JSON.stringify(obj);
|
|
416
|
+
if (Array.isArray(obj)) {
|
|
417
|
+
return "[" + obj.map(stableStringify).join(",") + "]";
|
|
418
|
+
}
|
|
419
|
+
const keys = Object.keys(obj).sort();
|
|
420
|
+
const parts = keys.filter((k) => obj[k] !== void 0).map(
|
|
421
|
+
(k) => JSON.stringify(k) + ":" + stableStringify(obj[k])
|
|
422
|
+
);
|
|
423
|
+
return "{" + parts.join(",") + "}";
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// ../core/src/prefetched-intents/prefetched-intents.ts
|
|
427
|
+
var PrefetchedIntents = class _PrefetchedIntents {
|
|
428
|
+
intents;
|
|
429
|
+
constructor(intents) {
|
|
430
|
+
this.intents = intents;
|
|
431
|
+
}
|
|
432
|
+
/** 从 PrefetchedIntent 数组创建缓存实例 */
|
|
433
|
+
static fromArray(items) {
|
|
434
|
+
const map = /* @__PURE__ */ new Map();
|
|
435
|
+
for (const item of items) {
|
|
436
|
+
if (item.intent && item.data !== void 0) {
|
|
437
|
+
const key = stableStringify(item.intent);
|
|
438
|
+
map.set(key, item.data);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
return new _PrefetchedIntents(map);
|
|
442
|
+
}
|
|
443
|
+
/** 创建空缓存实例 */
|
|
444
|
+
static empty() {
|
|
445
|
+
return new _PrefetchedIntents(/* @__PURE__ */ new Map());
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* 获取缓存的 Intent 结果(一次性使用)。
|
|
449
|
+
* 命中后从缓存中删除。
|
|
450
|
+
*/
|
|
451
|
+
get(intent) {
|
|
452
|
+
const key = stableStringify(intent);
|
|
453
|
+
const data = this.intents.get(key);
|
|
454
|
+
if (data !== void 0) {
|
|
455
|
+
this.intents.delete(key);
|
|
456
|
+
return data;
|
|
457
|
+
}
|
|
458
|
+
return void 0;
|
|
459
|
+
}
|
|
460
|
+
/** 检查缓存中是否有某个 Intent 的数据 */
|
|
461
|
+
has(intent) {
|
|
462
|
+
return this.intents.has(stableStringify(intent));
|
|
463
|
+
}
|
|
464
|
+
/** 缓存中的条目数 */
|
|
465
|
+
get size() {
|
|
466
|
+
return this.intents.size;
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
|
|
470
|
+
// ../core/src/framework.ts
|
|
471
|
+
var Framework = class _Framework {
|
|
472
|
+
container;
|
|
473
|
+
intentDispatcher;
|
|
474
|
+
actionDispatcher;
|
|
475
|
+
router;
|
|
476
|
+
prefetchedIntents;
|
|
477
|
+
constructor(container, prefetchedIntents) {
|
|
478
|
+
this.container = container;
|
|
479
|
+
this.intentDispatcher = new IntentDispatcher();
|
|
480
|
+
this.actionDispatcher = new ActionDispatcher();
|
|
481
|
+
this.router = new Router();
|
|
482
|
+
this.prefetchedIntents = prefetchedIntents;
|
|
483
|
+
}
|
|
484
|
+
/** 创建并初始化 Framework 实例 */
|
|
485
|
+
static create(config = {}) {
|
|
486
|
+
const container = new Container();
|
|
487
|
+
makeDependencies(container, config);
|
|
488
|
+
const fw = new _Framework(
|
|
489
|
+
container,
|
|
490
|
+
config.prefetchedIntents ?? PrefetchedIntents.empty()
|
|
491
|
+
);
|
|
492
|
+
config.setupRoutes?.(fw.router);
|
|
493
|
+
return fw;
|
|
494
|
+
}
|
|
495
|
+
/** 分发 Intent — 获取页面数据 */
|
|
496
|
+
async dispatch(intent) {
|
|
497
|
+
const logger = this.container.resolve(DEP_KEYS.LOGGER);
|
|
498
|
+
const cached = this.prefetchedIntents.get(intent);
|
|
499
|
+
if (cached !== void 0) {
|
|
500
|
+
logger.debug(
|
|
501
|
+
`[Framework] re-using prefetched intent response for: ${intent.id}`,
|
|
502
|
+
intent.params
|
|
503
|
+
);
|
|
504
|
+
return cached;
|
|
505
|
+
}
|
|
506
|
+
logger.debug(
|
|
507
|
+
`[Framework] dispatch intent: ${intent.id}`,
|
|
508
|
+
intent.params
|
|
509
|
+
);
|
|
510
|
+
return this.intentDispatcher.dispatch(intent, this.container);
|
|
511
|
+
}
|
|
512
|
+
/** 执行 Action — 处理用户交互 */
|
|
513
|
+
async perform(action) {
|
|
514
|
+
const logger = this.container.resolve(DEP_KEYS.LOGGER);
|
|
515
|
+
logger.debug(`[Framework] perform action: ${action.kind}`);
|
|
516
|
+
return this.actionDispatcher.perform(action);
|
|
517
|
+
}
|
|
518
|
+
/** 路由 URL — 将 URL 解析为 Intent + Action */
|
|
519
|
+
routeUrl(url) {
|
|
520
|
+
return this.router.resolve(url);
|
|
521
|
+
}
|
|
522
|
+
/** 记录页面访问事件 */
|
|
523
|
+
didEnterPage(page) {
|
|
524
|
+
const metrics = this.container.resolve(
|
|
525
|
+
DEP_KEYS.METRICS
|
|
526
|
+
);
|
|
527
|
+
metrics.recordPageView(page.pageType, {
|
|
528
|
+
pageId: page.id,
|
|
529
|
+
title: page.title
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
/** 注册 Action 处理器 */
|
|
533
|
+
onAction(kind, handler) {
|
|
534
|
+
this.actionDispatcher.onAction(kind, handler);
|
|
535
|
+
}
|
|
536
|
+
/** 注册 Intent Controller */
|
|
537
|
+
registerIntent(controller) {
|
|
538
|
+
this.intentDispatcher.register(controller);
|
|
539
|
+
}
|
|
540
|
+
/** 销毁 Framework 实例 */
|
|
541
|
+
dispose() {
|
|
542
|
+
this.container.dispose();
|
|
543
|
+
}
|
|
544
|
+
};
|
|
545
|
+
|
|
546
|
+
// ../core/src/http/client.ts
|
|
547
|
+
var HttpError = class extends Error {
|
|
548
|
+
constructor(status, statusText, body) {
|
|
549
|
+
super(`HTTP ${status}: ${statusText}`);
|
|
550
|
+
this.status = status;
|
|
551
|
+
this.statusText = statusText;
|
|
552
|
+
this.body = body;
|
|
553
|
+
this.name = "HttpError";
|
|
554
|
+
}
|
|
555
|
+
};
|
|
556
|
+
var HttpClient = class {
|
|
557
|
+
baseUrl;
|
|
558
|
+
defaultHeaders;
|
|
559
|
+
fetchFn;
|
|
560
|
+
constructor(config) {
|
|
561
|
+
this.baseUrl = config.baseUrl;
|
|
562
|
+
this.defaultHeaders = config.defaultHeaders ?? {};
|
|
563
|
+
this.fetchFn = config.fetch ?? globalThis.fetch.bind(globalThis);
|
|
564
|
+
}
|
|
565
|
+
/** GET 请求,返回解析后的 JSON */
|
|
566
|
+
async get(path, params) {
|
|
567
|
+
return this.request("GET", path, { params });
|
|
568
|
+
}
|
|
569
|
+
/** POST 请求,自动序列化 body 为 JSON */
|
|
570
|
+
async post(path, body, params) {
|
|
571
|
+
return this.request("POST", path, { body, params });
|
|
572
|
+
}
|
|
573
|
+
/** PUT 请求 */
|
|
574
|
+
async put(path, body, params) {
|
|
575
|
+
return this.request("PUT", path, { body, params });
|
|
576
|
+
}
|
|
577
|
+
/** DELETE 请求 */
|
|
578
|
+
async del(path, params) {
|
|
579
|
+
return this.request("DELETE", path, { params });
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* 底层请求方法 — 子类可覆写以自定义行为
|
|
583
|
+
*
|
|
584
|
+
* 自动处理:
|
|
585
|
+
* - URL 拼接 (baseUrl + path + params)
|
|
586
|
+
* - 默认 headers 合并
|
|
587
|
+
* - JSON body 序列化
|
|
588
|
+
* - 响应 JSON 解析
|
|
589
|
+
* - 非 2xx 状态码抛出 HttpError
|
|
590
|
+
*/
|
|
591
|
+
async request(method, path, options) {
|
|
592
|
+
const url = this.buildUrl(path, options?.params);
|
|
593
|
+
const headers = {
|
|
594
|
+
...this.defaultHeaders,
|
|
595
|
+
...options?.headers
|
|
596
|
+
};
|
|
597
|
+
const init = { method, headers };
|
|
598
|
+
if (options?.body !== void 0) {
|
|
599
|
+
headers["Content-Type"] = headers["Content-Type"] ?? "application/json";
|
|
600
|
+
init.body = JSON.stringify(options.body);
|
|
601
|
+
}
|
|
602
|
+
const response = await this.fetchFn(url, init);
|
|
603
|
+
if (!response.ok) {
|
|
604
|
+
const body = await response.text().catch(() => void 0);
|
|
605
|
+
throw new HttpError(response.status, response.statusText, body);
|
|
606
|
+
}
|
|
607
|
+
return response.json();
|
|
608
|
+
}
|
|
609
|
+
/** 构建完整 URL — 子类可覆写以自定义 URL 拼接逻辑 */
|
|
610
|
+
buildUrl(path, params) {
|
|
611
|
+
const base = this.baseUrl.endsWith("/") ? this.baseUrl.slice(0, -1) : this.baseUrl;
|
|
612
|
+
const normalizedPath = path.startsWith("/") ? path : `/${path}`;
|
|
613
|
+
const url = new URL(`${base}${normalizedPath}`, "http://placeholder");
|
|
614
|
+
if (params) {
|
|
615
|
+
for (const [k, v] of Object.entries(params)) {
|
|
616
|
+
url.searchParams.set(k, v);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
if (this.baseUrl.startsWith("http")) {
|
|
620
|
+
return url.toString();
|
|
621
|
+
}
|
|
622
|
+
return `${url.pathname}${url.search}`;
|
|
623
|
+
}
|
|
624
|
+
};
|
|
625
|
+
|
|
626
|
+
// ../core/src/intents/base-controller.ts
|
|
627
|
+
var BaseController = class {
|
|
628
|
+
/**
|
|
629
|
+
* 错误回退 — 子类可选覆写
|
|
630
|
+
*
|
|
631
|
+
* 当 execute() 抛出异常时调用。
|
|
632
|
+
* 默认行为: 重新抛出原始错误。
|
|
633
|
+
*
|
|
634
|
+
* @param params - Intent 参数
|
|
635
|
+
* @param error - execute() 抛出的错误
|
|
636
|
+
* @returns 回退数据
|
|
637
|
+
*/
|
|
638
|
+
fallback(params, error) {
|
|
639
|
+
throw error;
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* IntentController.perform() 实现
|
|
643
|
+
*
|
|
644
|
+
* 自动 try/catch → fallback 模式。
|
|
645
|
+
*/
|
|
646
|
+
async perform(intent, container) {
|
|
647
|
+
const params = intent.params ?? {};
|
|
648
|
+
try {
|
|
649
|
+
return await this.execute(params, container);
|
|
650
|
+
} catch (e) {
|
|
651
|
+
return this.fallback(
|
|
652
|
+
params,
|
|
653
|
+
e instanceof Error ? e : new Error(String(e))
|
|
654
|
+
);
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
};
|
|
658
|
+
|
|
659
|
+
// ../core/src/data/mapper.ts
|
|
660
|
+
function pipe(...mappers) {
|
|
661
|
+
return (input) => mappers.reduce((acc, mapper) => mapper(acc), input);
|
|
662
|
+
}
|
|
663
|
+
function pipeAsync(...mappers) {
|
|
664
|
+
return async (input) => {
|
|
665
|
+
let acc = input;
|
|
666
|
+
for (const mapper of mappers) {
|
|
667
|
+
acc = await mapper(acc);
|
|
668
|
+
}
|
|
669
|
+
return acc;
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
function mapEach(mapper) {
|
|
673
|
+
return (items) => items.map(mapper);
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
// ../core/src/bootstrap/define-routes.ts
|
|
677
|
+
function defineRoutes(framework, definitions) {
|
|
678
|
+
const registeredIntents = /* @__PURE__ */ new Set();
|
|
679
|
+
for (const def of definitions) {
|
|
680
|
+
if (def.controller && !registeredIntents.has(def.intentId)) {
|
|
681
|
+
framework.registerIntent(def.controller);
|
|
682
|
+
registeredIntents.add(def.intentId);
|
|
683
|
+
}
|
|
684
|
+
framework.router.add(def.path, def.intentId);
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
// ../core/src/utils/lru-map.ts
|
|
689
|
+
var LruMap = class {
|
|
690
|
+
map = /* @__PURE__ */ new Map();
|
|
691
|
+
capacity;
|
|
692
|
+
constructor(capacity) {
|
|
693
|
+
this.capacity = capacity;
|
|
694
|
+
}
|
|
695
|
+
get(key) {
|
|
696
|
+
const value = this.map.get(key);
|
|
697
|
+
if (value !== void 0) {
|
|
698
|
+
this.map.delete(key);
|
|
699
|
+
this.map.set(key, value);
|
|
700
|
+
}
|
|
701
|
+
return value;
|
|
702
|
+
}
|
|
703
|
+
set(key, value) {
|
|
704
|
+
if (this.map.has(key)) {
|
|
705
|
+
this.map.delete(key);
|
|
706
|
+
} else if (this.map.size >= this.capacity) {
|
|
707
|
+
const oldest = this.map.keys().next().value;
|
|
708
|
+
if (oldest !== void 0) {
|
|
709
|
+
this.map.delete(oldest);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
this.map.set(key, value);
|
|
713
|
+
}
|
|
714
|
+
has(key) {
|
|
715
|
+
return this.map.has(key);
|
|
716
|
+
}
|
|
717
|
+
delete(key) {
|
|
718
|
+
return this.map.delete(key);
|
|
719
|
+
}
|
|
720
|
+
get size() {
|
|
721
|
+
return this.map.size;
|
|
722
|
+
}
|
|
723
|
+
clear() {
|
|
724
|
+
this.map.clear();
|
|
725
|
+
}
|
|
726
|
+
};
|
|
727
|
+
|
|
728
|
+
// ../core/src/utils/optional.ts
|
|
729
|
+
function isSome(value) {
|
|
730
|
+
return value !== null && value !== void 0;
|
|
731
|
+
}
|
|
732
|
+
function isNone(value) {
|
|
733
|
+
return value === null || value === void 0;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
// ../core/src/utils/url.ts
|
|
737
|
+
function removeScheme(url) {
|
|
738
|
+
return url.replace(/^https?:\/\//, "");
|
|
739
|
+
}
|
|
740
|
+
function removeHost(url) {
|
|
741
|
+
try {
|
|
742
|
+
const parsed = new URL(url);
|
|
743
|
+
return parsed.pathname + parsed.search + parsed.hash;
|
|
744
|
+
} catch {
|
|
745
|
+
return url;
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
function removeQueryParams(url) {
|
|
749
|
+
return url.split("?")[0];
|
|
750
|
+
}
|
|
751
|
+
function getBaseUrl(url) {
|
|
752
|
+
return url.split("?")[0].split("#")[0];
|
|
753
|
+
}
|
|
754
|
+
function buildUrl(path, params) {
|
|
755
|
+
if (!params) return path;
|
|
756
|
+
const searchParams = new URLSearchParams();
|
|
757
|
+
for (const [key, value] of Object.entries(params)) {
|
|
758
|
+
if (value !== void 0) {
|
|
759
|
+
searchParams.set(key, value);
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
const qs = searchParams.toString();
|
|
763
|
+
return qs ? `${path}?${qs}` : path;
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
// ../core/src/utils/uuid.ts
|
|
767
|
+
function generateUuid() {
|
|
768
|
+
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
769
|
+
return crypto.randomUUID();
|
|
770
|
+
}
|
|
771
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
772
|
+
const r = Math.random() * 16 | 0;
|
|
773
|
+
const v = c === "x" ? r : r & 3 | 8;
|
|
774
|
+
return v.toString(16);
|
|
775
|
+
});
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
// ../browser/src/action-handlers/external-url-action.ts
|
|
779
|
+
function registerExternalUrlHandler(deps) {
|
|
780
|
+
const { framework, log } = deps;
|
|
781
|
+
framework.onAction(
|
|
782
|
+
ACTION_KINDS.EXTERNAL_URL,
|
|
783
|
+
(action) => {
|
|
784
|
+
log.debug(`ExternalUrlAction \u2192 ${action.url}`);
|
|
785
|
+
window.open(action.url, "_blank", "noopener,noreferrer");
|
|
786
|
+
}
|
|
787
|
+
);
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
// ../browser/src/utils/try-scroll.ts
|
|
791
|
+
var MAX_TRIES = 100;
|
|
792
|
+
var FUDGE = 16;
|
|
793
|
+
var pendingFrame = null;
|
|
794
|
+
function tryScroll(log, getScrollableElement, scrollY) {
|
|
795
|
+
if (pendingFrame !== null) {
|
|
796
|
+
cancelAnimationFrame(pendingFrame);
|
|
797
|
+
pendingFrame = null;
|
|
798
|
+
}
|
|
799
|
+
let tries = 0;
|
|
800
|
+
pendingFrame = requestAnimationFrame(function attempt() {
|
|
801
|
+
if (++tries >= MAX_TRIES) {
|
|
802
|
+
log.warn(
|
|
803
|
+
`tryScroll: gave up after ${MAX_TRIES} frames, target=${scrollY}`
|
|
804
|
+
);
|
|
805
|
+
pendingFrame = null;
|
|
806
|
+
return;
|
|
807
|
+
}
|
|
808
|
+
const el = getScrollableElement();
|
|
809
|
+
if (!el) {
|
|
810
|
+
log.warn(
|
|
811
|
+
"could not restore scroll: the scrollable element is missing"
|
|
812
|
+
);
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
const { scrollHeight, offsetHeight } = el;
|
|
816
|
+
const canScroll = scrollY + offsetHeight <= scrollHeight + FUDGE;
|
|
817
|
+
if (!canScroll) {
|
|
818
|
+
log.info("page is not tall enough for scroll yet", {
|
|
819
|
+
scrollHeight,
|
|
820
|
+
offsetHeight
|
|
821
|
+
});
|
|
822
|
+
pendingFrame = requestAnimationFrame(attempt);
|
|
823
|
+
return;
|
|
824
|
+
}
|
|
825
|
+
el.scrollTop = scrollY;
|
|
826
|
+
log.info("scroll restored to", scrollY);
|
|
827
|
+
pendingFrame = null;
|
|
828
|
+
});
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
// ../browser/src/utils/history.ts
|
|
832
|
+
var HISTORY_SIZE_LIMIT = 10;
|
|
833
|
+
var History = class {
|
|
834
|
+
entries;
|
|
835
|
+
log;
|
|
836
|
+
getScrollablePageElement;
|
|
837
|
+
currentStateId;
|
|
838
|
+
constructor(log, options, sizeLimit = HISTORY_SIZE_LIMIT) {
|
|
839
|
+
this.entries = new LruMap(sizeLimit);
|
|
840
|
+
this.log = log;
|
|
841
|
+
this.getScrollablePageElement = options.getScrollablePageElement;
|
|
842
|
+
}
|
|
843
|
+
replaceState(state, url) {
|
|
844
|
+
const id = generateUuid();
|
|
845
|
+
window.history.replaceState({ id }, "", url);
|
|
846
|
+
this.currentStateId = id;
|
|
847
|
+
this.entries.set(id, { state, scrollY: 0 });
|
|
848
|
+
this.scrollTop = 0;
|
|
849
|
+
this.log.info("replaceState", state, url, id);
|
|
850
|
+
}
|
|
851
|
+
pushState(state, url) {
|
|
852
|
+
const id = generateUuid();
|
|
853
|
+
window.history.pushState({ id }, "", url);
|
|
854
|
+
this.currentStateId = id;
|
|
855
|
+
this.entries.set(id, { state, scrollY: 0 });
|
|
856
|
+
this.scrollTop = 0;
|
|
857
|
+
this.log.info("pushState", state, url, id);
|
|
858
|
+
}
|
|
859
|
+
beforeTransition() {
|
|
860
|
+
const { state } = window.history;
|
|
861
|
+
if (!state) return;
|
|
862
|
+
const oldEntry = this.entries.get(state.id);
|
|
863
|
+
if (!oldEntry) {
|
|
864
|
+
this.log.info(
|
|
865
|
+
"current history state evicted from LRU, not saving scroll position"
|
|
866
|
+
);
|
|
867
|
+
return;
|
|
868
|
+
}
|
|
869
|
+
const { scrollTop } = this;
|
|
870
|
+
this.entries.set(state.id, { ...oldEntry, scrollY: scrollTop });
|
|
871
|
+
this.log.info("saving scroll position", scrollTop);
|
|
872
|
+
}
|
|
873
|
+
onPopState(listener) {
|
|
874
|
+
window.addEventListener("popstate", (event) => {
|
|
875
|
+
this.currentStateId = event.state?.id;
|
|
876
|
+
if (!this.currentStateId) {
|
|
877
|
+
this.log.warn(
|
|
878
|
+
"encountered a null event.state.id in onPopState event:",
|
|
879
|
+
window.location.href
|
|
880
|
+
);
|
|
881
|
+
}
|
|
882
|
+
this.log.info("popstate", this.entries, this.currentStateId);
|
|
883
|
+
const entry = this.currentStateId ? this.entries.get(this.currentStateId) : void 0;
|
|
884
|
+
listener(window.location.href, entry?.state);
|
|
885
|
+
if (!entry) {
|
|
886
|
+
return;
|
|
887
|
+
}
|
|
888
|
+
const { scrollY } = entry;
|
|
889
|
+
this.log.info("restoring scroll to", scrollY);
|
|
890
|
+
tryScroll(this.log, () => this.getScrollablePageElement(), scrollY);
|
|
891
|
+
});
|
|
892
|
+
}
|
|
893
|
+
updateState(update) {
|
|
894
|
+
if (!this.currentStateId) {
|
|
895
|
+
this.log.warn(
|
|
896
|
+
"failed: encountered a null currentStateId inside updateState"
|
|
897
|
+
);
|
|
898
|
+
return;
|
|
899
|
+
}
|
|
900
|
+
const currentState = this.entries.get(this.currentStateId);
|
|
901
|
+
const newState = update(currentState?.state);
|
|
902
|
+
this.log.info("updateState", newState, this.currentStateId);
|
|
903
|
+
this.entries.set(this.currentStateId, {
|
|
904
|
+
...currentState,
|
|
905
|
+
state: newState
|
|
906
|
+
});
|
|
907
|
+
}
|
|
908
|
+
get scrollTop() {
|
|
909
|
+
return this.getScrollablePageElement()?.scrollTop || 0;
|
|
910
|
+
}
|
|
911
|
+
set scrollTop(scrollTop) {
|
|
912
|
+
const element = this.getScrollablePageElement();
|
|
913
|
+
if (element) {
|
|
914
|
+
element.scrollTop = scrollTop;
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
};
|
|
918
|
+
|
|
919
|
+
// ../browser/src/action-handlers/flow-action.ts
|
|
920
|
+
function registerFlowActionHandler(deps) {
|
|
921
|
+
const { framework, log, callbacks, updateApp } = deps;
|
|
922
|
+
let isFirstPage = true;
|
|
923
|
+
const history = new History(log, {
|
|
924
|
+
getScrollablePageElement: () => document.getElementById("scrollable-page-override") || document.getElementById("scrollable-page") || document.documentElement
|
|
925
|
+
});
|
|
926
|
+
framework.onAction(ACTION_KINDS.FLOW, async (action) => {
|
|
927
|
+
const flowAction = action;
|
|
928
|
+
const url = flowAction.url;
|
|
929
|
+
log.debug(`FlowAction \u2192 ${url}`);
|
|
930
|
+
if (flowAction.presentationContext === "modal") {
|
|
931
|
+
const match2 = framework.routeUrl(url);
|
|
932
|
+
if (match2) {
|
|
933
|
+
const page = await framework.dispatch(
|
|
934
|
+
match2.intent
|
|
935
|
+
);
|
|
936
|
+
callbacks.onModal(page);
|
|
937
|
+
}
|
|
938
|
+
return;
|
|
939
|
+
}
|
|
940
|
+
const shouldReplace = isFirstPage;
|
|
941
|
+
const match = framework.routeUrl(url);
|
|
942
|
+
if (!match) {
|
|
943
|
+
log.warn(`FlowAction: no route for ${url}`);
|
|
944
|
+
return;
|
|
945
|
+
}
|
|
946
|
+
const pagePromise = framework.dispatch(
|
|
947
|
+
match.intent
|
|
948
|
+
);
|
|
949
|
+
await Promise.race([
|
|
950
|
+
pagePromise,
|
|
951
|
+
new Promise((r) => setTimeout(r, 500))
|
|
952
|
+
]).catch(() => {
|
|
953
|
+
});
|
|
954
|
+
history.beforeTransition();
|
|
955
|
+
updateApp({
|
|
956
|
+
page: pagePromise.then((page) => {
|
|
957
|
+
const canonicalURL = url;
|
|
958
|
+
if (shouldReplace) {
|
|
959
|
+
history.replaceState({ page }, canonicalURL);
|
|
960
|
+
} else {
|
|
961
|
+
history.pushState({ page }, canonicalURL);
|
|
962
|
+
}
|
|
963
|
+
callbacks.onNavigate(
|
|
964
|
+
new URL(canonicalURL, window.location.origin).pathname
|
|
965
|
+
);
|
|
966
|
+
didEnterPage(page);
|
|
967
|
+
return page;
|
|
968
|
+
}),
|
|
969
|
+
isFirstPage
|
|
970
|
+
});
|
|
971
|
+
isFirstPage = false;
|
|
972
|
+
});
|
|
973
|
+
history.onPopState(async (url, cachedState) => {
|
|
974
|
+
log.debug(`popstate \u2192 ${url}, cached=${!!cachedState}`);
|
|
975
|
+
callbacks.onNavigate(new URL(url).pathname);
|
|
976
|
+
if (cachedState) {
|
|
977
|
+
const { page } = cachedState;
|
|
978
|
+
didEnterPage(page);
|
|
979
|
+
updateApp({ page, isFirstPage });
|
|
980
|
+
return;
|
|
981
|
+
}
|
|
982
|
+
const parsed = new URL(url);
|
|
983
|
+
const routeMatch = framework.routeUrl(parsed.pathname + parsed.search);
|
|
984
|
+
if (!routeMatch) {
|
|
985
|
+
log.error(
|
|
986
|
+
"received popstate without data, but URL was unroutable:",
|
|
987
|
+
url
|
|
988
|
+
);
|
|
989
|
+
didEnterPage(null);
|
|
990
|
+
updateApp({
|
|
991
|
+
page: Promise.reject(new Error("404")),
|
|
992
|
+
isFirstPage
|
|
993
|
+
});
|
|
994
|
+
return;
|
|
995
|
+
}
|
|
996
|
+
const pagePromise = framework.dispatch(
|
|
997
|
+
routeMatch.intent
|
|
998
|
+
);
|
|
999
|
+
await Promise.race([
|
|
1000
|
+
pagePromise,
|
|
1001
|
+
new Promise((r) => setTimeout(r, 500))
|
|
1002
|
+
]).catch(() => {
|
|
1003
|
+
});
|
|
1004
|
+
updateApp({
|
|
1005
|
+
page: pagePromise.then((page) => {
|
|
1006
|
+
didEnterPage(page);
|
|
1007
|
+
return page;
|
|
1008
|
+
}),
|
|
1009
|
+
isFirstPage
|
|
1010
|
+
});
|
|
1011
|
+
});
|
|
1012
|
+
function didEnterPage(page) {
|
|
1013
|
+
(async () => {
|
|
1014
|
+
try {
|
|
1015
|
+
if (page) {
|
|
1016
|
+
await framework.didEnterPage(page);
|
|
1017
|
+
}
|
|
1018
|
+
} catch (e) {
|
|
1019
|
+
log.error("didEnterPage error:", e);
|
|
1020
|
+
}
|
|
1021
|
+
})();
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
// ../browser/src/action-handlers/register.ts
|
|
1026
|
+
function registerActionHandlers(deps) {
|
|
1027
|
+
const { framework, log, callbacks, updateApp } = deps;
|
|
1028
|
+
registerFlowActionHandler({
|
|
1029
|
+
framework,
|
|
1030
|
+
log,
|
|
1031
|
+
callbacks,
|
|
1032
|
+
updateApp
|
|
1033
|
+
});
|
|
1034
|
+
registerExternalUrlHandler({ framework, log });
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
// ../browser/src/server-data.ts
|
|
1038
|
+
var SERVER_DATA_ID = "serialized-server-data";
|
|
1039
|
+
function deserializeServerData() {
|
|
1040
|
+
const script = document.getElementById(SERVER_DATA_ID);
|
|
1041
|
+
if (!script?.textContent) return void 0;
|
|
1042
|
+
script.parentNode?.removeChild(script);
|
|
1043
|
+
try {
|
|
1044
|
+
return JSON.parse(script.textContent);
|
|
1045
|
+
} catch {
|
|
1046
|
+
return void 0;
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
function createPrefetchedIntentsFromDom() {
|
|
1050
|
+
const data = deserializeServerData();
|
|
1051
|
+
if (!data || !Array.isArray(data)) {
|
|
1052
|
+
return PrefetchedIntents.empty();
|
|
1053
|
+
}
|
|
1054
|
+
return PrefetchedIntents.fromArray(data);
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
// ../browser/src/start-app.ts
|
|
1058
|
+
async function startBrowserApp(config) {
|
|
1059
|
+
const {
|
|
1060
|
+
bootstrap,
|
|
1061
|
+
defaultLocale = "en",
|
|
1062
|
+
mountId = "app",
|
|
1063
|
+
mount,
|
|
1064
|
+
callbacks
|
|
1065
|
+
} = config;
|
|
1066
|
+
const prefetchedIntents = createPrefetchedIntentsFromDom();
|
|
1067
|
+
const framework = Framework.create({ prefetchedIntents });
|
|
1068
|
+
bootstrap(framework);
|
|
1069
|
+
const loggerFactory = framework.container.resolve(
|
|
1070
|
+
DEP_KEYS.LOGGER_FACTORY
|
|
1071
|
+
);
|
|
1072
|
+
const log = loggerFactory.loggerFor("browser");
|
|
1073
|
+
const initialAction = framework.routeUrl(
|
|
1074
|
+
window.location.pathname + window.location.search
|
|
1075
|
+
);
|
|
1076
|
+
const locale = document.documentElement.lang || defaultLocale;
|
|
1077
|
+
const target = document.getElementById(mountId);
|
|
1078
|
+
const updateApp = mount(target, { framework, locale });
|
|
1079
|
+
registerActionHandlers({
|
|
1080
|
+
framework,
|
|
1081
|
+
log,
|
|
1082
|
+
callbacks,
|
|
1083
|
+
updateApp
|
|
1084
|
+
});
|
|
1085
|
+
if (initialAction) {
|
|
1086
|
+
await framework.perform(initialAction.action);
|
|
1087
|
+
} else {
|
|
1088
|
+
updateApp({
|
|
1089
|
+
page: Promise.reject(new Error("404")),
|
|
1090
|
+
isFirstPage: true
|
|
1091
|
+
});
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
export {
|
|
1096
|
+
ACTION_KINDS,
|
|
1097
|
+
isFlowAction,
|
|
1098
|
+
isExternalUrlAction,
|
|
1099
|
+
isCompoundAction,
|
|
1100
|
+
makeFlowAction,
|
|
1101
|
+
makeExternalUrlAction,
|
|
1102
|
+
ActionDispatcher,
|
|
1103
|
+
IntentDispatcher,
|
|
1104
|
+
Container,
|
|
1105
|
+
BaseLogger,
|
|
1106
|
+
shouldLog,
|
|
1107
|
+
resetFilterCache,
|
|
1108
|
+
ConsoleLogger,
|
|
1109
|
+
ConsoleLoggerFactory,
|
|
1110
|
+
DEP_KEYS,
|
|
1111
|
+
makeDependencies,
|
|
1112
|
+
Router,
|
|
1113
|
+
CompositeLoggerFactory,
|
|
1114
|
+
CompositeLogger,
|
|
1115
|
+
stableStringify,
|
|
1116
|
+
PrefetchedIntents,
|
|
1117
|
+
Framework,
|
|
1118
|
+
HttpError,
|
|
1119
|
+
HttpClient,
|
|
1120
|
+
BaseController,
|
|
1121
|
+
pipe,
|
|
1122
|
+
pipeAsync,
|
|
1123
|
+
mapEach,
|
|
1124
|
+
defineRoutes,
|
|
1125
|
+
LruMap,
|
|
1126
|
+
isSome,
|
|
1127
|
+
isNone,
|
|
1128
|
+
removeScheme,
|
|
1129
|
+
removeHost,
|
|
1130
|
+
removeQueryParams,
|
|
1131
|
+
getBaseUrl,
|
|
1132
|
+
buildUrl,
|
|
1133
|
+
generateUuid,
|
|
1134
|
+
registerExternalUrlHandler,
|
|
1135
|
+
tryScroll,
|
|
1136
|
+
History,
|
|
1137
|
+
registerFlowActionHandler,
|
|
1138
|
+
registerActionHandlers,
|
|
1139
|
+
deserializeServerData,
|
|
1140
|
+
createPrefetchedIntentsFromDom,
|
|
1141
|
+
startBrowserApp
|
|
1142
|
+
};
|
|
1143
|
+
//# sourceMappingURL=chunk-OVGQ4NUA.js.map
|