Skip to content

Commit

Permalink
feat(ferry): add evict operation to ferry isolate client
Browse files Browse the repository at this point in the history
  • Loading branch information
knaeckeKami committed Jan 6, 2025
1 parent 153e648 commit f1dbad8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/ferry/lib/ferry_isolate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,29 @@ class IsolateClient extends TypedLink {
return super.dispose();
}

/// returns all top-level keys in the cache
Future<Iterable<String>> getCacheKeys() {
return _handleSingleResponseCommand(
(sendPort) => CacheKeysCommand(sendPort));
}

/// evicts to top level selections from the cache
/// e.g. a query like
/// ```graphql
/// query GetPerson {
/// person(id: "1") {
/// id
/// name
/// }
/// }
/// ```
/// would evict the field `Query`->`person({id:"1"})`
/// Consider calling gcCache() after this to remove orphaned data
Future<void> evictOperation(OperationRequest request) {
return _handleSingleResponseCommand(
(sendPort) => EvictOperationCommand(sendPort, request));
}

/// adds a request to the requestController of the client on the isolate
/// this is useful for re-fetch and pagination
/// see https://ferry.gql-dart.dev/docs/pagination
Expand Down
40 changes: 40 additions & 0 deletions packages/ferry/lib/src/isolate/isolate_commands.dart
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,43 @@ class ClearOptimisticPatchesCommand extends IsolateCommand {
sendPort.send(null);
}
}

@internal
class CacheKeysCommand extends IsolateCommand {
CacheKeysCommand(SendPort sendPort) : super(sendPort);

@override
void handle(
TypedLinkWithCacheAndRequestController link, ReceivePort receivePort) {
final keys = link.cache.store.keys;
sendPort.send(keys);
}
}

@internal
class IdentifyCommand<T> extends IsolateCommand {
final T object;

IdentifyCommand(SendPort sendPort, this.object) : super(sendPort);

@override
void handle(
TypedLinkWithCacheAndRequestController link, ReceivePort receivePort) {
final id = link.cache.identify(object);
sendPort.send(id);
}
}

@internal
class EvictOperationCommand<TData, TVars> extends IsolateCommand {
final OperationRequest<TData, TVars> request;

EvictOperationCommand(SendPort sendPort, this.request) : super(sendPort);

@override
void handle(
TypedLinkWithCacheAndRequestController link, ReceivePort receivePort) {
link.cache.evictOperation(request);
sendPort.send(null);
}
}

0 comments on commit f1dbad8

Please sign in to comment.