Skip to content

Commit

Permalink
feat: added static function for clearing a client for a given inboxId…
Browse files Browse the repository at this point in the history
… from memory
  • Loading branch information
cameronvoell committed Aug 28, 2024
1 parent 89cf5d4 commit 795f14e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int>, keyType: String, preKeyIndex: Int ->
withContext(Dispatchers.IO) {
logV("sign")
Expand Down
12 changes: 11 additions & 1 deletion ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
>(
Expand Down
7 changes: 7 additions & 0 deletions src/lib/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 795f14e

Please sign in to comment.