@mandujs/core 0.9.5 → 0.9.7

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.7",
4
4
  "description": "Mandu Framework Core - Spec, Generator, Guard, Runtime",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -423,16 +423,39 @@ 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 initGlobalState() {
431
+ if (window.__MANDU_ROUTER_STATE__) return;
432
+ var 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__ && window.__MANDU_DATA__[route && route.id] ? window.__MANDU_DATA__[route.id].serverData : undefined,
440
+ navigation: { state: 'idle' }
441
+ };
442
+ window.__MANDU_ROUTER_LISTENERS__ = window.__MANDU_ROUTER_LISTENERS__ || new Set();
443
+ })();
444
+
445
+ function getGlobalState() {
446
+ return window.__MANDU_ROUTER_STATE__;
447
+ }
448
+
449
+ function setGlobalState(state) {
450
+ window.__MANDU_ROUTER_STATE__ = state;
451
+ }
452
+
453
+ function getListeners() {
454
+ return window.__MANDU_ROUTER_LISTENERS__;
455
+ }
433
456
 
434
457
  // 패턴 매칭 캐시
435
- const patternCache = new Map();
458
+ var patternCache = new Map();
436
459
 
437
460
  function compilePattern(pattern) {
438
461
  if (patternCache.has(pattern)) return patternCache.get(pattern);
@@ -468,25 +491,17 @@ function extractParams(pattern, pathname) {
468
491
  }
469
492
 
470
493
  function notifyListeners() {
471
- const state = {
472
- currentRoute,
473
- loaderData: currentLoaderData,
474
- navigation: navigationState
475
- };
476
- listeners.forEach(fn => { try { fn(state); } catch(e) {} });
494
+ const state = getGlobalState();
495
+ getListeners().forEach(fn => { try { fn(state); } catch(e) {} });
477
496
  }
478
497
 
479
498
  export function subscribe(listener) {
480
- listeners.add(listener);
481
- return () => listeners.delete(listener);
499
+ getListeners().add(listener);
500
+ return () => getListeners().delete(listener);
482
501
  }
483
502
 
484
503
  export function getRouterState() {
485
- return {
486
- currentRoute,
487
- loaderData: currentLoaderData,
488
- navigation: navigationState
489
- };
504
+ return getGlobalState();
490
505
  }
491
506
 
492
507
  export async function navigate(to, options = {}) {
@@ -499,7 +514,9 @@ export async function navigate(to, options = {}) {
499
514
  return;
500
515
  }
501
516
 
502
- navigationState = { state: 'loading', location: to };
517
+ // 로딩 상태로 전환
518
+ const state = getGlobalState();
519
+ setGlobalState({ ...state, navigation: { state: 'loading', location: to } });
503
520
  notifyListeners();
504
521
 
505
522
  const dataUrl = url.pathname + (url.search ? url.search + '&' : '?') + '_data=1';
@@ -518,9 +535,12 @@ export async function navigate(to, options = {}) {
518
535
  history.pushState({ routeId: data.routeId }, '', to);
519
536
  }
520
537
 
521
- currentRoute = { id: data.routeId, pattern: data.pattern, params: data.params };
522
- currentLoaderData = data.loaderData;
523
- navigationState = { state: 'idle' };
538
+ // 전역 상태 업데이트
539
+ setGlobalState({
540
+ currentRoute: { id: data.routeId, pattern: data.pattern, params: data.params },
541
+ loaderData: data.loaderData,
542
+ navigation: { state: 'idle' }
543
+ });
524
544
 
525
545
  window.__MANDU_DATA__ = window.__MANDU_DATA__ || {};
526
546
  window.__MANDU_DATA__[data.routeId] = { serverData: data.loaderData };
@@ -562,8 +582,10 @@ function handlePopState(e) {
562
582
 
563
583
  // 초기화
564
584
  function init() {
565
- if (currentRoute) {
566
- currentRoute.params = extractParams(currentRoute.pattern, location.pathname);
585
+ var state = getGlobalState();
586
+ if (state.currentRoute) {
587
+ state.currentRoute.params = extractParams(state.currentRoute.pattern, location.pathname);
588
+ setGlobalState(state);
567
589
  }
568
590
 
569
591
  window.addEventListener('popstate', handlePopState);
@@ -576,8 +598,6 @@ if (document.readyState === 'loading') {
576
598
  } else {
577
599
  init();
578
600
  }
579
-
580
- export { currentRoute, currentLoaderData, navigationState };
581
601
  `;
582
602
  }
583
603