Skip to content

Commit

Permalink
Cache immutable uri
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed Nov 27, 2023
1 parent a402130 commit 7824906
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/src/state/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ abstract class OctopusState extends _OctopusTree {

/// Current state representation as a [Uri]
/// e.g. /shop/category?id=1/category?id=12/product?id=123
Uri get uri => StateUtil.encodeLocation(this);
Uri get uri;

/// Current state representation as a location string.
String get location;
Expand Down Expand Up @@ -127,6 +127,9 @@ class OctopusState$Mutable extends OctopusState
@override
bool get isMutable => true;

@override
Uri get uri => StateUtil.encodeLocation(this);

@override
String get location => uri.toString();

Expand Down Expand Up @@ -179,6 +182,9 @@ class OctopusState$Immutable extends OctopusState
@override
bool get isMutable => false;

@override
late final Uri uri = StateUtil.encodeLocation(this);

@override
late final String location = uri.toString();

Expand Down
43 changes: 43 additions & 0 deletions lib/src/state/state_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,49 @@ abstract final class StateUtil {
);
}

/* @internal
static Future<Uri> encodeLocationAsync(OctopusState state) async {
final segments = <String>[];
final stopwatch = Stopwatch()..start();
try {
Future<void> encodeNode(OctopusNode node, int depth) async {
if (stopwatch.elapsedMilliseconds > 8) {
await Future<void>.delayed(Duration.zero);
stopwatch.reset();
}
final prefix = '.' * depth;
final String name;
if (node.arguments.isEmpty) {
name = node.name;
} else {
final args = (node.arguments.entries.toList(growable: false)
..sort((a, b) => a.key.compareTo(b.key)))
.map<String>((e) => '${Uri.encodeComponent(e.key)}'
'='
'${Uri.encodeComponent(e.value)}')
.join('&');
name = args.isEmpty ? node.name : '${node.name}~$args';
}
segments.add('$prefix$name');
for (final child in node.children) {
await encodeNode(child, depth + 1);
}
}
for (final node in state.children) {
await encodeNode(node, 0);
}
} finally {
stopwatch.stop();
}
return Uri(
pathSegments: segments,
queryParameters: state.arguments.isEmpty ? null : state.arguments,
);
} */

/// Convert location string to tree components.
/// {@nodoc}
@internal
Expand Down

0 comments on commit 7824906

Please sign in to comment.