From 795f14e44046bb022ff49a27a3cc91fa4fa6ce2a Mon Sep 17 00:00:00 2001 From: cameronvoell Date: Wed, 28 Aug 2024 12:26:20 -0700 Subject: [PATCH] feat: added static function for clearing a client for a given inboxId from memory --- .../expo/modules/xmtpreactnativesdk/XMTPModule.kt | 7 +++++++ ios/XMTPModule.swift | 12 +++++++++++- src/index.ts | 4 ++++ src/lib/Client.ts | 7 +++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt b/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt index 47bd717c8..61a6df984 100644 --- a/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt +++ b/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt @@ -371,6 +371,13 @@ class XMTPModule : Module() { } } + AsyncFunction("dropClient") Coroutine { inboxId: String -> + withContext(Dispatchers.IO) { + logV("dropClient") + clients.remove(inboxId) + } + } + AsyncFunction("sign") Coroutine { inboxId: String, digest: List, keyType: String, preKeyIndex: Int -> withContext(Dispatchers.IO) { logV("sign") diff --git a/ios/XMTPModule.swift b/ios/XMTPModule.swift index e3fba916b..b655a0c95 100644 --- a/ios/XMTPModule.swift +++ b/ios/XMTPModule.swift @@ -53,6 +53,11 @@ public class XMTPModule: Module { ContentJson.initCodecs(client: client) clients[key] = client } + + // A method to drop client for a given key from memory + func dropClient(key: String) { + clients[key] = nil + } // A method to retrieve a client func getClient(key: String) -> XMTP.Client? { @@ -271,10 +276,15 @@ public class XMTPModule: Module { await clientsManager.updateClient(key: client.inboxID, client: client) return try ClientWrapper.encodeToObj(client) } catch { - print("ERRO! Failed to create client: \(error)") + print("ERROR! Failed to create client: \(error)") throw error } } + + // Remove a client from memory for a given inboxId + AsyncFunction("dropClient") { (inboxId: String) in + await clientsManager.dropClient(key: inboxId) + } AsyncFunction("sign") { (inboxId: String, digest: [UInt8], keyType: String, preKeyIndex: Int) -> [UInt8] in guard let client = await clientsManager.getClient(key: inboxId) else { diff --git a/src/index.ts b/src/index.ts index f44db2916..14bd5030c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -178,6 +178,10 @@ export async function createFromKeyBundle( ) } +export async function dropClient(inboxId: string) { + return await XMTPModule.dropClient(inboxId) +} + export async function createGroup< ContentTypes extends DefaultContentTypes = DefaultContentTypes, >( diff --git a/src/lib/Client.ts b/src/lib/Client.ts index 7e8d11116..620e42d29 100644 --- a/src/lib/Client.ts +++ b/src/lib/Client.ts @@ -253,6 +253,13 @@ export class Client< ) } + /** + * Drop the client from memory. Use when you want to remove the client from memory and are done with it. + */ + static async dropClient(inboxId: string) { + return await XMTPModule.dropClient(inboxId) + } + /** * Static method to determine if the address is currently in our network. *