Skip to content

Commit

Permalink
add ability to pass inbox id to build to speed up building a client
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Dec 4, 2024
1 parent 48e197e commit 759793d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 12 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ repositories {
dependencies {
implementation project(':expo-modules-core')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${getKotlinVersion()}"
implementation "org.xmtp:android:3.0.11"
implementation "org.xmtp:android:3.0.12"
implementation 'com.google.code.gson:gson:2.10.1'
implementation 'com.facebook.react:react-native:0.71.3'
implementation "com.daveanthonythomas.moshipack:moshipack:1.0.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,14 @@ class XMTPModule : Module() {
}
}

AsyncFunction("build") Coroutine { address: String, dbEncryptionKey: List<Int>, authParams: String ->
AsyncFunction("build") Coroutine { address: String, inboxId: String?, dbEncryptionKey: List<Int>, authParams: String ->
withContext(Dispatchers.IO) {
logV("build")
val options = clientOptions(
dbEncryptionKey,
authParams,
)
val client = Client().build(address = address, options = options)
val client = Client().build(address = address, options = options, inboxId = inboxId)
ContentJson.Companion
clients[client.installationId] = client
ClientWrapper.encodeToObj(client)
Expand Down
41 changes: 36 additions & 5 deletions example/src/tests/groupPerformanceTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@
import Config from 'react-native-config'

Check warning on line 2 in example/src/tests/groupPerformanceTests.ts

View workflow job for this annotation

GitHub Actions / lint

'Config' is defined but never used
import RNFS from 'react-native-fs'
import { PrivateKeyAccount, privateKeyToAccount } from 'viem/accounts'

Check warning on line 4 in example/src/tests/groupPerformanceTests.ts

View workflow job for this annotation

GitHub Actions / lint

'privateKeyToAccount' is defined but never used
import { Client, Dm, Group, GroupUpdatedCodec, ReactionCodec, RemoteAttachmentCodec, ReplyCodec, Signer, StaticAttachmentCodec } from 'xmtp-react-native-sdk'
import {
Client,
Dm,
Group,
GroupUpdatedCodec,
ReactionCodec,
RemoteAttachmentCodec,
ReplyCodec,
Signer,
StaticAttachmentCodec,
} from 'xmtp-react-native-sdk'

import { Test, assert, createClients } from './test-utils'

Check warning on line 17 in example/src/tests/groupPerformanceTests.ts

View workflow job for this annotation

GitHub Actions / lint

There should be at least one empty line between import groups

Check warning on line 17 in example/src/tests/groupPerformanceTests.ts

View workflow job for this annotation

GitHub Actions / lint

'assert' is defined but never used

Check warning on line 17 in example/src/tests/groupPerformanceTests.ts

View workflow job for this annotation

GitHub Actions / lint

'createClients' is defined but never used
import { Wallet } from 'ethers'

Check warning on line 18 in example/src/tests/groupPerformanceTests.ts

View workflow job for this annotation

GitHub Actions / lint

`ethers` import should occur before import of `react-native-config`
Expand Down Expand Up @@ -108,7 +118,7 @@ test('building and creating', async () => {

let start = performance.now()
const alix = await Client.create(alixWallet, {
env: 'local',
env: 'dev',
appVersion: 'Testing/0.0.0',
dbEncryptionKey: keyBytes,
dbDirectory: dbDirPath,
Expand All @@ -131,7 +141,7 @@ test('building and creating', async () => {

start = performance.now()
const alixBuild = await Client.build(alixWallet.address, {
env: 'local',
env: 'dev',
appVersion: 'Testing/0.0.0',
dbEncryptionKey: keyBytes,
dbDirectory: dbDirPath,
Expand All @@ -152,6 +162,28 @@ test('building and creating', async () => {
end = performance.now()
console.log(`Alix build registered group updated codec in ${end - start}ms`)

start = performance.now()
const alixBuild2 = await Client.build(
alixWallet.address,
{
env: 'dev',
appVersion: 'Testing/0.0.0',
dbEncryptionKey: keyBytes,
dbDirectory: dbDirPath,
codecs: [
new ReactionCodec(),
new ReplyCodec(),
new GroupUpdatedCodec(),
new StaticAttachmentCodec(),
new RemoteAttachmentCodec(),
],
},
alix.inboxId
)
await alixBuild2.register(new GroupUpdatedCodec())
end = performance.now()
console.log(`Built a client with inboxId in ${end - start}ms`)

start = performance.now()
await alix.deleteLocalDatabase()
end = performance.now()
Expand All @@ -162,10 +194,9 @@ test('building and creating', async () => {
end = performance.now()
console.log(`Droped a client in ${end - start}ms`)


start = performance.now()
await Client.create(alixWallet, {
env: 'local',
env: 'dev',
appVersion: 'Testing/0.0.0',
dbEncryptionKey: keyBytes,
dbDirectory: dbDirPath,
Expand Down
2 changes: 1 addition & 1 deletion ios/XMTPReactNative.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Pod::Spec.new do |s|
s.source_files = "**/*.{h,m,swift}"

s.dependency "MessagePacker"
s.dependency "XMTP", "= 3.0.12"
s.dependency "XMTP", "= 3.0.13"
s.dependency 'CSecp256k1', '~> 0.2'
s.dependency "SQLCipher", "= 4.5.7"
end
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ export async function build(
dbEncryptionKey: Uint8Array,
appVersion?: string | undefined,
dbDirectory?: string | undefined,
historySyncUrl?: string | undefined
historySyncUrl?: string | undefined,
inboxId?: InboxId | undefined
): Promise<string> {
const authParams: AuthParams = {
environment,
Expand All @@ -183,6 +184,7 @@ export async function build(
}
return await XMTPModule.build(
address,
inboxId,
Array.from(dbEncryptionKey),
JSON.stringify(authParams)
)
Expand Down
6 changes: 4 additions & 2 deletions src/lib/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ export class Client<
ContentCodecs extends DefaultContentTypes = DefaultContentTypes,
>(
address: Address,
options: ClientOptions & { codecs?: ContentCodecs }
options: ClientOptions & { codecs?: ContentCodecs },
inboxId?: InboxId | undefined
): Promise<Client<ContentCodecs>> {
if (options.dbEncryptionKey.length !== 32) {
throw new Error('Must pass an encryption key that is exactly 32 bytes.')
Expand All @@ -215,7 +216,8 @@ export class Client<
options.dbEncryptionKey,
options.appVersion,
options.dbDirectory,
options.historySyncUrl
options.historySyncUrl,
inboxId
)

return new Client(
Expand Down

0 comments on commit 759793d

Please sign in to comment.