Skip to content

Commit

Permalink
fix(cache): add value == and hashCode to lazy cache map, fix traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
micimize committed Aug 30, 2019
1 parent ffb16c9 commit 617dde7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
9 changes: 9 additions & 0 deletions packages/graphql/lib/src/cache/lazy_cache_map.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:core';
import 'package:quiver/core.dart' show hash3;

import 'package:meta/meta.dart';

Expand Down Expand Up @@ -33,6 +34,14 @@ class LazyCacheMap extends LazyDereferencingMap {
}
return result;
}

int get hashCode => hash3(_data, _dereference, cacheState);

bool operator ==(Object other) =>
other is LazyCacheMap &&
other._data == _data &&
other._dereference == _dereference &&
other.cacheState == cacheState;
}

/// Unwrap a given Object that could possibly be a lazy map
Expand Down
16 changes: 8 additions & 8 deletions packages/graphql/lib/src/utilities/traverse.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,35 @@ class Traversal {
this.transformSideEffect,
this.seenObjects,
}) {
seenObjects ??= HashSet<int>();
seenObjects ??= HashSet<Object>();
}

Transform transform;

/// An optional side effect to call when a node is transformed.
SideEffect transformSideEffect;
HashSet<int> seenObjects;
HashSet<Object> seenObjects;

bool hasAlreadySeen(Object node) {
final bool wasAdded = seenObjects.add(node.hashCode);
bool alreadySeen(Object node) {
final bool wasAdded = seenObjects.add(node);
return !wasAdded;
}

/// Traverse only the values of the given map
Map<String, Object> traverseValues(Map<String, Object> node) {
return node.map<String, Object>(
(String key, Object value) => MapEntry<String, Object>(
key,
traverse(value),
),
key,
traverse(value),
),
);
}

// Attempts to apply the transform to every leaf of the data structure recursively.
// Stops recursing when a node is transformed (returns non-null)
Object traverse(Object node) {
final Object transformed = transform(node);
if (hasAlreadySeen(node)) {
if (alreadySeen(node)) {
return transformed ?? node;
}
if (transformed != null) {
Expand Down
1 change: 1 addition & 0 deletions packages/graphql/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies:
graphql_parser: ^1.1.3
rxdart: ^0.22.0
websocket: ^0.0.5
quiver: '>=2.0.0 <3.0.0'
dev_dependencies:
pedantic: <=1.7.99
mockito: ^4.0.0
Expand Down

0 comments on commit 617dde7

Please sign in to comment.