Skip to content

Commit

Permalink
refactor: The on method of ModuleContext can now return null, and rem…
Browse files Browse the repository at this point in the history
…ove will sink null
  • Loading branch information
foxsofter committed Sep 1, 2023
1 parent 4a66e0d commit 2bd17a2
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 4.5.0
- refactor: The on method of ModuleContext can now return null, and remove will sink null

## 4.4.7
fix: remove transitionPage pop transition

Expand Down
2 changes: 1 addition & 1 deletion example/lib/src/biz/biz1/biz1.context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension Biz1Context on ModuleContext {
///
int? removeIntValue() => remove<int>('intValue');

Stream<int> get onIntValue =>
Stream<int?> get onIntValue =>
on<int>('intValue') ??
(throw ArgumentError('intValue stream cannot be null'));
}
2 changes: 1 addition & 1 deletion example/lib/src/biz/biz1/flutter1/home/home.context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extension HomeContext on ModuleContext {
bool setStringKeyBiz1(final String value) =>
set<String>('stringKeyBiz1', value);

Stream<String> get onStringKeyBiz1 =>
Stream<String?> get onStringKeyBiz1 =>
on<String>('stringKeyBiz1') ??
(throw ArgumentError('stringKeyBiz1 stream cannot be null'));

Expand Down
2 changes: 1 addition & 1 deletion example/lib/src/biz/biz2/flutter4/flutter4.context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension Flutter4Context on ModuleContext {
///
People? removePeople() => remove<People>('people');

Stream<People> get onPeople =>
Stream<People?> get onPeople =>
on<People>('people') ??
(throw ArgumentError('people stream cannot be null'));
}
2 changes: 1 addition & 1 deletion lib/src/module/module_anchor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class ModuleAnchor
void set<T>(final Comparable<dynamic> key, final T value) =>
setParam(key, value);

T remove<T>(final Comparable<dynamic> key) => removeParam(key);
T? remove<T>(final Comparable<dynamic> key) => removeParam(key);

List<ThrioModule> _getModules({final String? url}) {
if (modules.isEmpty) {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/module/module_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ class ModuleContext {
}
}
// Anchor module caches the data of the framework
return module.parent == anchor || module.parent == null
return module.parent == anchor
? null
: module.parent?._moduleContext.remove<T>(key);
}

/// Subscribe to a series of param by `key`.
///
Stream<T>? on<T>(final String key, {final T? initialValue}) {
Stream<T?>? on<T>(final String key, {final T? initialValue}) {
if (module == anchor) {
return anchor.onParam(key, initialValue: initialValue);
}
Expand Down
19 changes: 13 additions & 6 deletions lib/src/module/module_param_scheme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ mixin ModuleParamScheme on ThrioModule {
/// Subscribe to a series of param by `key`.
///
@protected
Stream<T> onParam<T>(final Comparable<dynamic> key, {final T? initialValue}) {
Stream<T?>? onParam<T>(final Comparable<dynamic> key, {final T? initialValue}) {
if (!hasParamScheme<T>(key)) {
return null;
}
paramStreamCtrls[key] ??= <StreamController<dynamic>>{};
final sc = StreamController<T>();
sc
Expand Down Expand Up @@ -81,7 +84,7 @@ mixin ModuleParamScheme on ThrioModule {
T? getParam<T>(final Comparable<dynamic> key) {
// Anchor module does not need to get param scheme.
if (this == anchor) {
return _params[key] as T?; // ignore: avoid_as
return _params[key] as T?;
}
if (!_paramSchemes.keys.contains(key)) {
return null;
Expand All @@ -94,7 +97,7 @@ mixin ModuleParamScheme on ThrioModule {
throw ThrioException(
'$T does not match the param scheme type: ${value.runtimeType}');
}
return value as T; // ignore: avoid_as
return value as T?;
}

/// Sets param with `key` & `value`.
Expand Down Expand Up @@ -149,10 +152,10 @@ mixin ModuleParamScheme on ThrioModule {
///
/// Throw `ThrioException` if `T` is not matched param scheme.
///
T removeParam<T>(final Comparable<dynamic> key) {
T? removeParam<T>(final Comparable<dynamic> key) {
// Anchor module does not need to get param scheme.
if (this == anchor) {
return _params.remove(key) as T; // ignore: avoid_as
return _params.remove(key) as T?;
}
if (T != dynamic &&
T != Object &&
Expand All @@ -161,7 +164,11 @@ mixin ModuleParamScheme on ThrioModule {
throw ThrioException(
'$T does not match the param scheme type: ${_paramSchemes[key]}');
}
return _params.remove(key) as T; // ignore: avoid_as
final param = _params.remove(key) as T?;
if (param != null) {
_setParam(key, param);
}
return param;
}

/// A function for register a param scheme.
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_thrio
description: Thrio makes it easy and fast to add flutter to existing mobile applications, and provide a simple and consistent navigator APIs.
version: 4.4.7
version: 4.5.0
homepage: https://github.com/flutter-thrio/flutter_thrio

environment:
Expand Down

0 comments on commit 2bd17a2

Please sign in to comment.