From f1dbad8ad6cb21538781cea1ee8ab8d721f6ed6a Mon Sep 17 00:00:00 2001 From: Martin Kamleithner Date: Mon, 6 Jan 2025 23:14:49 +0000 Subject: [PATCH] feat(ferry): add evict operation to ferry isolate client --- packages/ferry/lib/ferry_isolate.dart | 23 +++++++++++ .../lib/src/isolate/isolate_commands.dart | 40 +++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/packages/ferry/lib/ferry_isolate.dart b/packages/ferry/lib/ferry_isolate.dart index db6df24e..b102a219 100644 --- a/packages/ferry/lib/ferry_isolate.dart +++ b/packages/ferry/lib/ferry_isolate.dart @@ -320,6 +320,29 @@ class IsolateClient extends TypedLink { return super.dispose(); } + /// returns all top-level keys in the cache + Future> 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 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 diff --git a/packages/ferry/lib/src/isolate/isolate_commands.dart b/packages/ferry/lib/src/isolate/isolate_commands.dart index eb638489..0f966594 100644 --- a/packages/ferry/lib/src/isolate/isolate_commands.dart +++ b/packages/ferry/lib/src/isolate/isolate_commands.dart @@ -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 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 extends IsolateCommand { + final OperationRequest request; + + EvictOperationCommand(SendPort sendPort, this.request) : super(sendPort); + + @override + void handle( + TypedLinkWithCacheAndRequestController link, ReceivePort receivePort) { + link.cache.evictOperation(request); + sendPort.send(null); + } +}