@real-router/core 0.34.1 → 0.35.0

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.
@@ -6,13 +6,9 @@ import { RouterError } from "../../RouterError";
6
6
  import type { RouterLifecycleDependencies } from "./types";
7
7
  import type { NavigationOptions, State } from "@real-router/types";
8
8
 
9
- // ═══════════════════════════════════════════════════════════════════════════════
10
- // CYCLIC DEPENDENCIES
11
- // ═══════════════════════════════════════════════════════════════════════════════
12
- // RouterLifecycle → Navigation.navigateToState() (for start transitions)
13
- //
14
- // Solution: functional references configured in Router.#setupDependencies()
15
- // ═══════════════════════════════════════════════════════════════════════════════
9
+ const REPLACE_OPTS: NavigationOptions = { replace: true };
10
+
11
+ Object.freeze(REPLACE_OPTS);
16
12
 
17
13
  /**
18
14
  * Independent namespace for managing router lifecycle.
@@ -21,17 +17,6 @@ import type { NavigationOptions, State } from "@real-router/types";
21
17
  * by RouterFSM in the facade (Router.ts).
22
18
  */
23
19
  export class RouterLifecycleNamespace {
24
- // ═══════════════════════════════════════════════════════════════════════════
25
- // Functional references for cyclic dependencies
26
- // ═══════════════════════════════════════════════════════════════════════════
27
-
28
- // Dependencies injected via setDependencies (replaces full router reference)
29
- #navigateToState!: (
30
- toState: State,
31
- fromState: State | undefined,
32
- opts: NavigationOptions,
33
- ) => Promise<State>;
34
-
35
20
  #deps!: RouterLifecycleDependencies;
36
21
 
37
22
  // =========================================================================
@@ -54,20 +39,6 @@ export class RouterLifecycleNamespace {
54
39
  // Dependency injection
55
40
  // =========================================================================
56
41
 
57
- /**
58
- * Sets the navigateToState reference (cyclic dependency on NavigationNamespace).
59
- * Must be called before using start().
60
- */
61
- setNavigateToState(
62
- fn: (
63
- toState: State,
64
- fromState: State | undefined,
65
- opts: NavigationOptions,
66
- ) => Promise<State>,
67
- ): void {
68
- this.#navigateToState = fn;
69
- }
70
-
71
42
  /**
72
43
  * Sets dependencies for lifecycle operations.
73
44
  * Must be called before using lifecycle methods.
@@ -90,10 +61,6 @@ export class RouterLifecycleNamespace {
90
61
  const deps = this.#deps;
91
62
  const options = deps.getOptions();
92
63
 
93
- const startOptions: NavigationOptions = {
94
- replace: true,
95
- };
96
-
97
64
  const matchedState = deps.matchPath(startPath);
98
65
 
99
66
  if (!matchedState && !options.allowNotFound) {
@@ -108,25 +75,15 @@ export class RouterLifecycleNamespace {
108
75
 
109
76
  deps.completeStart();
110
77
 
111
- let finalState: State;
112
-
113
78
  if (matchedState) {
114
- finalState = await this.#navigateToState(
115
- matchedState,
116
- undefined,
117
- startOptions,
118
- );
119
- } else {
120
- const notFoundState = deps.makeNotFoundState(startPath);
121
-
122
- finalState = await this.#navigateToState(
123
- notFoundState,
124
- undefined,
125
- startOptions,
79
+ return deps.navigate(
80
+ matchedState.name,
81
+ matchedState.params,
82
+ REPLACE_OPTS,
126
83
  );
127
84
  }
128
85
 
129
- return finalState;
86
+ return deps.navigateToNotFound(startPath);
130
87
  }
131
88
 
132
89
  /**
@@ -1,10 +1,20 @@
1
1
  // packages/core/src/namespaces/RouterLifecycleNamespace/types.ts
2
2
 
3
- import type { Options, Params, State } from "@real-router/types";
3
+ import type {
4
+ NavigationOptions,
5
+ Options,
6
+ Params,
7
+ State,
8
+ } from "@real-router/types";
4
9
 
5
10
  export interface RouterLifecycleDependencies {
6
11
  getOptions: () => Options;
7
- makeNotFoundState: (path: string) => State;
12
+ navigate: (
13
+ name: string,
14
+ params: Params,
15
+ opts: NavigationOptions,
16
+ ) => Promise<State>;
17
+ navigateToNotFound: (path: string) => State;
8
18
  clearState: () => void;
9
19
  matchPath: <P extends Params = Params, MP extends Params = Params>(
10
20
  path: string,
@@ -3,7 +3,6 @@
3
3
  import { getTypeDescription, validateState } from "type-guards";
4
4
 
5
5
  import { areParamValuesEqual, getUrlParamsFromMeta } from "./helpers";
6
- import { constants } from "../../constants";
7
6
  import { freezeStateInPlace } from "../../helpers";
8
7
 
9
8
  import type { StateNamespaceDependencies } from "./types";
@@ -181,20 +180,6 @@ export class StateNamespace {
181
180
  return freezeStateInPlace(state);
182
181
  }
183
182
 
184
- /**
185
- * Creates a frozen state object for the "not found" route.
186
- */
187
- makeNotFoundState(path: string): State {
188
- return this.makeState<{ path: string }>(
189
- constants.UNKNOWN_ROUTE,
190
- { path },
191
- path,
192
- {
193
- params: {},
194
- },
195
- );
196
- }
197
-
198
183
  // =========================================================================
199
184
  // State Comparison Methods
200
185
  // =========================================================================
@@ -181,7 +181,7 @@ function pointOfDifference(
181
181
  * Validation significantly slows down nameToIDs execution.
182
182
  * The input should be validated by the function/method that calls nameToIDs.
183
183
  */
184
- function nameToIDs(name: string): string[] {
184
+ export function nameToIDs(name: string): string[] {
185
185
  // ===== FAST PATH 1: Empty string (root route) =====
186
186
  // Most common in initial navigation
187
187
  if (!name) {
@@ -164,9 +164,13 @@ export class RouterWiringBuilder<
164
164
  emitTransitionError: (toState, fromState, error) => {
165
165
  this.eventBus.emitOrFailTransitionError(toState, fromState, error);
166
166
  },
167
+ emitTransitionSuccess: (toState, fromState, opts) => {
168
+ this.eventBus.emitTransitionSuccess(toState, fromState, opts);
169
+ },
167
170
  };
168
171
 
169
172
  this.navigation.setDependencies(navigationDeps);
173
+ this.navigation.setCanNavigate(() => this.eventBus.canBeginTransition());
170
174
 
171
175
  const transitionDeps: TransitionDependencies = {
172
176
  getLifecycleFunctions: () => this.routeLifecycle.getFunctions(),
@@ -183,7 +187,9 @@ export class RouterWiringBuilder<
183
187
  wireLifecycleDeps(): void {
184
188
  const lifecycleDeps: RouterLifecycleDependencies = {
185
189
  getOptions: () => this.options.get(),
186
- makeNotFoundState: (path) => this.state.makeNotFoundState(path),
190
+ navigate: (name, params, opts) =>
191
+ this.navigation.navigate(name, params, opts),
192
+ navigateToNotFound: (path) => this.navigation.navigateToNotFound(path),
187
193
  clearState: () => {
188
194
  this.state.set(undefined);
189
195
  },
@@ -210,12 +216,4 @@ export class RouterWiringBuilder<
210
216
  getUrlParams: (name) => this.routes.getUrlParams(name),
211
217
  });
212
218
  }
213
-
214
- wireCyclicDeps(): void {
215
- this.navigation.setCanNavigate(() => this.eventBus.canBeginTransition());
216
-
217
- this.lifecycle.setNavigateToState((toState, fromState, opts) =>
218
- this.navigation.navigateToState(toState, fromState, opts),
219
- );
220
- }
221
219
  }
@@ -10,8 +10,8 @@ import type { DefaultDependencies } from "@real-router/types";
10
10
  * - `wireLimits()` first: all namespaces must have limits before any other setup
11
11
  * - `wireRouteLifecycleDeps()` BEFORE `wireRoutesDeps()`: RoutesNamespace.setDependencies()
12
12
  * registers pending canActivate handlers which require RouteLifecycleNamespace to be ready
13
- * - `wireCyclicDeps()` LAST: resolves circular references between NavigationNamespace and
14
- * RouterLifecycleNamespace (they depend on each other via direct property assignment)
13
+ * - `wireNavigationDeps()` BEFORE `wireLifecycleDeps()`: lifecycle deps reference
14
+ * NavigationNamespace.navigate() which requires navigation deps to be set
15
15
  */
16
16
  export function wireRouter<Dependencies extends DefaultDependencies>(
17
17
  builder: RouterWiringBuilder<Dependencies>,
@@ -23,5 +23,4 @@ export function wireRouter<Dependencies extends DefaultDependencies>(
23
23
  builder.wireNavigationDeps();
24
24
  builder.wireLifecycleDeps();
25
25
  builder.wireStateDeps();
26
- builder.wireCyclicDeps();
27
26
  }