slicejs-web-framework 3.5.3 → 3.5.4
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.
|
@@ -415,25 +415,43 @@ export default class Router {
|
|
|
415
415
|
async onRouteChange() {
|
|
416
416
|
if (this.routeChangeTimeout) {
|
|
417
417
|
clearTimeout(this.routeChangeTimeout);
|
|
418
|
+
// This call supersedes a still-pending one; resolve the superseded
|
|
419
|
+
// promise so anything awaiting it doesn't hang — the new call does the
|
|
420
|
+
// real work.
|
|
421
|
+
if (this._routeChangeResolve) {
|
|
422
|
+
this._routeChangeResolve();
|
|
423
|
+
this._routeChangeResolve = null;
|
|
424
|
+
}
|
|
418
425
|
}
|
|
419
426
|
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
427
|
+
// Resolve only AFTER the debounced route handling finishes, so a caller
|
|
428
|
+
// that awaits this (e.g. _performNavigation) sees slice.router.activeRoute
|
|
429
|
+
// already updated before it emits router:change. (Previously the awaited
|
|
430
|
+
// promise resolved the instant the timer was scheduled, so router:change
|
|
431
|
+
// fired ~10ms before activeRoute was set.)
|
|
432
|
+
return new Promise((resolve) => {
|
|
433
|
+
this._routeChangeResolve = resolve;
|
|
434
|
+
this.routeChangeTimeout = setTimeout(async () => {
|
|
435
|
+
try {
|
|
436
|
+
const path = window.location.pathname;
|
|
437
|
+
const routeContainersFlag = await this.renderRoutesComponentsInPage();
|
|
424
438
|
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
439
|
+
if (routeContainersFlag) {
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
428
442
|
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
443
|
+
const { route, params } = this.matchRoute(path);
|
|
444
|
+
if (route) {
|
|
445
|
+
await this.handleRoute(route, params);
|
|
446
|
+
}
|
|
447
|
+
} catch (error) {
|
|
448
|
+
slice.logger.error('Router', `Route change failed for ${window.location.pathname}`, error);
|
|
449
|
+
} finally {
|
|
450
|
+
this._routeChangeResolve = null;
|
|
451
|
+
resolve();
|
|
432
452
|
}
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
}
|
|
436
|
-
}, 10);
|
|
453
|
+
}, 10);
|
|
454
|
+
});
|
|
437
455
|
}
|
|
438
456
|
|
|
439
457
|
/**
|
package/Slice/Slice.js
CHANGED
|
@@ -316,8 +316,27 @@ export default class Slice {
|
|
|
316
316
|
return null;
|
|
317
317
|
}
|
|
318
318
|
|
|
319
|
+
// Dev guard: update() called during init() runs BEFORE the component is
|
|
320
|
+
// registered, so the framework's update() wrapper (serialization +
|
|
321
|
+
// liveness) isn't installed yet and the call misbehaves silently. Warn
|
|
322
|
+
// instead. Restored right after init() so registerComponent →
|
|
323
|
+
// installUpdate wraps the real method, not this guard.
|
|
324
|
+
let _rawUpdate = null;
|
|
325
|
+
if (typeof componentInstance.update === 'function') {
|
|
326
|
+
_rawUpdate = componentInstance.update;
|
|
327
|
+
componentInstance.update = function (...args) {
|
|
328
|
+
slice.logger.logWarning(
|
|
329
|
+
componentInstance.constructor?.name || 'Component',
|
|
330
|
+
'update() was called during init() (before registration). Run your render logic directly from init(); the update() wrapper (serialization + liveness) is not active until the component is registered.'
|
|
331
|
+
);
|
|
332
|
+
return _rawUpdate.apply(this, args);
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
|
|
319
336
|
if (componentInstance.init) await componentInstance.init();
|
|
320
337
|
|
|
338
|
+
if (_rawUpdate) componentInstance.update = _rawUpdate;
|
|
339
|
+
|
|
321
340
|
if (slice.debuggerConfig.enabled && isVisual) {
|
|
322
341
|
this.debugger.attachDebugMode(componentInstance);
|
|
323
342
|
}
|
|
@@ -549,6 +568,20 @@ async function init() {
|
|
|
549
568
|
document.body.appendChild(window.slice.leakInspector);
|
|
550
569
|
}
|
|
551
570
|
|
|
571
|
+
// ContextManager delivers setState → watch through EventManager, so context
|
|
572
|
+
// reactivity requires events. We don't auto-enable events (in a production
|
|
573
|
+
// build EventManager is only bundled when events.enabled was true, so it may
|
|
574
|
+
// not have shipped) nor throw (a boot-time throw would take down the whole
|
|
575
|
+
// app, inconsistent with how "EventManager disabled" is handled below). Log a
|
|
576
|
+
// loud error and continue; `slice build` hard-fails on this same misconfig so
|
|
577
|
+
// a broken bundle never ships.
|
|
578
|
+
if (sliceConfig.context?.enabled && !sliceConfig.events?.enabled) {
|
|
579
|
+
window.slice.logger.logError(
|
|
580
|
+
'Slice',
|
|
581
|
+
'context.enabled requires events.enabled — ContextManager reactivity (setState → watch/bind) is delivered through EventManager. With events disabled, watchers will not fire. Set "events": { "enabled": true } in sliceConfig.json.'
|
|
582
|
+
);
|
|
583
|
+
}
|
|
584
|
+
|
|
552
585
|
if (sliceConfig.events?.enabled) {
|
|
553
586
|
const EventManagerModule = window.slice.frameworkClasses?.EventManager
|
|
554
587
|
|| await window.slice.getClass(`${slice.paths.structuralComponentFolderPath}/EventManager/EventManager.js`);
|
package/package.json
CHANGED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Regression test for GOTCHAS §3: router:change must not be observable before
|
|
2
|
+
// slice.router.activeRoute is updated.
|
|
3
|
+
//
|
|
4
|
+
// Router is a plain class with no module-level DOM/global access and no imports,
|
|
5
|
+
// so onRouteChange() can be exercised directly on a stubbed instance. The fix
|
|
6
|
+
// makes onRouteChange() resolve only AFTER the debounced handleRoute() runs
|
|
7
|
+
// (which is where activeRoute is set), so a caller that awaits it — like
|
|
8
|
+
// _performNavigation — emits router:change with activeRoute already current.
|
|
9
|
+
//
|
|
10
|
+
// Run: node --test tests/router-emit-ordering.test.js
|
|
11
|
+
|
|
12
|
+
import { test, describe, before } from 'node:test';
|
|
13
|
+
import assert from 'node:assert/strict';
|
|
14
|
+
|
|
15
|
+
describe('Router.onRouteChange (§3 emit-after-activeRoute)', () => {
|
|
16
|
+
let Router;
|
|
17
|
+
|
|
18
|
+
before(async () => {
|
|
19
|
+
globalThis.window = globalThis.window || { location: { pathname: '/' } };
|
|
20
|
+
globalThis.slice = globalThis.slice || { logger: { error() {} }, router: {} };
|
|
21
|
+
({ default: Router } = await import('../Slice/Components/Structural/Router/Router.js'));
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const makeRouter = (handleRoute) => {
|
|
25
|
+
const r = Object.create(Router.prototype);
|
|
26
|
+
r.renderRoutesComponentsInPage = async () => false; // not the MultiRoute container path
|
|
27
|
+
r.matchRoute = () => ({ route: { component: 'X' }, params: {} });
|
|
28
|
+
r.handleRoute = handleRoute;
|
|
29
|
+
return r;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
test('awaiting onRouteChange() waits until handleRoute() has finished', async () => {
|
|
33
|
+
const order = [];
|
|
34
|
+
const r = makeRouter(async () => {
|
|
35
|
+
await new Promise((res) => setTimeout(res, 5)); // simulate async render
|
|
36
|
+
order.push('handleRoute-done'); // activeRoute is assigned here in the real code
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
await r.onRouteChange();
|
|
40
|
+
order.push('after-await');
|
|
41
|
+
|
|
42
|
+
// With the fix, handleRoute completes BEFORE the await returns. (Before the
|
|
43
|
+
// fix the awaited promise resolved the instant the timer was scheduled, so
|
|
44
|
+
// 'after-await' would land first — i.e. emit-before-activeRoute.)
|
|
45
|
+
assert.deepEqual(order, ['handleRoute-done', 'after-await']);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('a superseded (debounced) call still resolves — no hang', async () => {
|
|
49
|
+
let handled = 0;
|
|
50
|
+
const r = makeRouter(async () => { handled++; });
|
|
51
|
+
|
|
52
|
+
const p1 = r.onRouteChange(); // schedules timer T1
|
|
53
|
+
const p2 = r.onRouteChange(); // clears T1, resolves p1, schedules T2
|
|
54
|
+
|
|
55
|
+
await Promise.race([
|
|
56
|
+
Promise.all([p1, p2]),
|
|
57
|
+
new Promise((_, rej) => setTimeout(() => rej(new Error('onRouteChange hung')), 1000)),
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
// Only the surviving timer runs its work.
|
|
61
|
+
assert.equal(handled, 1);
|
|
62
|
+
});
|
|
63
|
+
});
|