@mandujs/core 0.9.5 → 0.9.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mandujs/core",
3
- "version": "0.9.5",
3
+ "version": "0.9.6",
4
4
  "description": "Mandu Framework Core - Spec, Generator, Guard, Runtime",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -423,13 +423,37 @@ function generateRouterRuntimeSource(): string {
423
423
  /**
424
424
  * Mandu Client Router Runtime (Generated)
425
425
  * Client-side Routing을 위한 런타임
426
+ * 전역 상태를 사용하여 모든 모듈에서 동일 인스턴스 공유
426
427
  */
427
428
 
428
- // 라우트 정보
429
- let currentRoute = window.__MANDU_ROUTE__ || null;
430
- let currentLoaderData = window.__MANDU_DATA__?.[currentRoute?.id]?.serverData;
431
- let navigationState = { state: 'idle' };
432
- const listeners = new Set();
429
+ // 전역 상태 초기화 (Island와 공유)
430
+ function getGlobalState() {
431
+ if (!window.__MANDU_ROUTER_STATE__) {
432
+ const route = window.__MANDU_ROUTE__;
433
+ window.__MANDU_ROUTER_STATE__ = {
434
+ currentRoute: route ? {
435
+ id: route.id,
436
+ pattern: route.pattern,
437
+ params: route.params || {}
438
+ } : null,
439
+ loaderData: window.__MANDU_DATA__?.[route?.id]?.serverData,
440
+ navigation: { state: 'idle' }
441
+ };
442
+ }
443
+ return window.__MANDU_ROUTER_STATE__;
444
+ }
445
+
446
+ function setGlobalState(state) {
447
+ window.__MANDU_ROUTER_STATE__ = state;
448
+ }
449
+
450
+ // 전역 listeners
451
+ function getListeners() {
452
+ if (!window.__MANDU_ROUTER_LISTENERS__) {
453
+ window.__MANDU_ROUTER_LISTENERS__ = new Set();
454
+ }
455
+ return window.__MANDU_ROUTER_LISTENERS__;
456
+ }
433
457
 
434
458
  // 패턴 매칭 캐시
435
459
  const patternCache = new Map();
@@ -468,25 +492,17 @@ function extractParams(pattern, pathname) {
468
492
  }
469
493
 
470
494
  function notifyListeners() {
471
- const state = {
472
- currentRoute,
473
- loaderData: currentLoaderData,
474
- navigation: navigationState
475
- };
476
- listeners.forEach(fn => { try { fn(state); } catch(e) {} });
495
+ const state = getGlobalState();
496
+ getListeners().forEach(fn => { try { fn(state); } catch(e) {} });
477
497
  }
478
498
 
479
499
  export function subscribe(listener) {
480
- listeners.add(listener);
481
- return () => listeners.delete(listener);
500
+ getListeners().add(listener);
501
+ return () => getListeners().delete(listener);
482
502
  }
483
503
 
484
504
  export function getRouterState() {
485
- return {
486
- currentRoute,
487
- loaderData: currentLoaderData,
488
- navigation: navigationState
489
- };
505
+ return getGlobalState();
490
506
  }
491
507
 
492
508
  export async function navigate(to, options = {}) {
@@ -499,7 +515,9 @@ export async function navigate(to, options = {}) {
499
515
  return;
500
516
  }
501
517
 
502
- navigationState = { state: 'loading', location: to };
518
+ // 로딩 상태로 전환
519
+ const state = getGlobalState();
520
+ setGlobalState({ ...state, navigation: { state: 'loading', location: to } });
503
521
  notifyListeners();
504
522
 
505
523
  const dataUrl = url.pathname + (url.search ? url.search + '&' : '?') + '_data=1';
@@ -518,9 +536,12 @@ export async function navigate(to, options = {}) {
518
536
  history.pushState({ routeId: data.routeId }, '', to);
519
537
  }
520
538
 
521
- currentRoute = { id: data.routeId, pattern: data.pattern, params: data.params };
522
- currentLoaderData = data.loaderData;
523
- navigationState = { state: 'idle' };
539
+ // 전역 상태 업데이트
540
+ setGlobalState({
541
+ currentRoute: { id: data.routeId, pattern: data.pattern, params: data.params },
542
+ loaderData: data.loaderData,
543
+ navigation: { state: 'idle' }
544
+ });
524
545
 
525
546
  window.__MANDU_DATA__ = window.__MANDU_DATA__ || {};
526
547
  window.__MANDU_DATA__[data.routeId] = { serverData: data.loaderData };