Skip to content

Commit e4f2d9d

Browse files
fix: prevent navigating to already active route
Co-authored-by: MasterMarcoHD <MasterMarcoHD@users.noreply.github.com>
1 parent ba3e055 commit e4f2d9d

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

lib/src/domain/services/routing_service.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ abstract class RoutingService<T, Config extends Object> extends Service {
2828
/// Default is false.
2929
bool isActive(String path, {bool exact = true, bool ignoreParams = false});
3030

31+
/// Returns the current routing context, or null if no route is active.
32+
RouteContext? get currentContext;
33+
3134
/// Returns the current routing context.
32-
RouteContext get currentContext;
35+
/// Throws an error if no route is active.
36+
RouteContext get requiredCurrentContext => currentContext!;
3337

3438
/// Adds a listener that is called on routing changes.
3539
///

lib/src/infra/services/routingkit_routing_service.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class RoutingKitRoutingService<T, Config extends Object>
2727
}
2828

2929
@override
30-
RouteContext get currentContext => _context!;
30+
RouteContext? get currentContext => _context;
3131

3232
@override
3333
FutureOr<void> dispose() {
@@ -119,6 +119,12 @@ class RoutingKitRoutingService<T, Config extends Object>
119119
required void Function(T) callback,
120120
}) async {
121121
final uri = Uri.parse(path);
122+
123+
if (uri == currentContext?.uri) {
124+
log('Already at path: $path, skipping navigation.');
125+
return;
126+
}
127+
122128
final cleanPath = uri.path;
123129

124130
// find the route

lib/src/module.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ abstract class Module<RouteType, Config extends Object>
1616
with LifecycleMixin, LogMixin, Disposable {
1717
GetIt get _di => GetIt.instance;
1818

19+
bool _isActive = false;
20+
1921
@override
2022
String? get group => 'Module';
2123

@@ -64,10 +66,34 @@ abstract class Module<RouteType, Config extends Object>
6466
_firstImportScope ??= module.runtimeType.toString();
6567

6668
await module.initialize();
69+
await module.activate();
6770

6871
log('${module.runtimeType} mounted successfully.');
6972
}
7073

74+
@mustCallSuper
75+
@override
76+
FutureOr<void> activate() async {
77+
if (_isActive) return;
78+
79+
for (final module in imports) {
80+
await module.activate();
81+
}
82+
83+
_isActive = true;
84+
}
85+
86+
@mustCallSuper
87+
@override
88+
FutureOr<void> deactivate() async {
89+
if (!_isActive) return;
90+
91+
for (final module in imports.reversed) {
92+
await module.deactivate();
93+
}
94+
_isActive = false;
95+
}
96+
7197
@mustCallSuper
7298
@override
7399
FutureOr<void> initialize() async {

0 commit comments

Comments
 (0)