Skip to content

Commit

Permalink
fix call preEventCallbacks without listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
kele-leanes committed Dec 13, 2023
1 parent a52a9ac commit b4e7130
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 34 deletions.
61 changes: 33 additions & 28 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ public class XMTPModule: Module {
//
// Auth functions
//
AsyncFunction("auth") { (address: String, environment: String, appVersion: String?) in
AsyncFunction("auth") { (address: String, environment: String, appVersion: String?, hasCreateIdentityCallback: Bool?, hasEnableIdentityCallback: Bool?) in
let signer = ReactNativeSigner(module: self, address: address)
self.signer = signer
let preCreateIdentityCallback: PreEventCallback? = hasCreateIdentityCallback ?? false ? self.preCreateIdentityCallback : nil
let preEnableIdentityCallback: PreEventCallback? = hasEnableIdentityCallback ?? false ? self.preEnableIdentityCallback : nil
let options = createClientConfig(env: environment, appVersion: appVersion, preEnableIdentityCallback: preEnableIdentityCallback, preCreateIdentityCallback: preCreateIdentityCallback)
try await clientsManager.updateClient(key: address, client: await XMTP.Client.create(account: signer, options: options))
self.signer = nil
Expand All @@ -78,8 +80,10 @@ public class XMTPModule: Module {
}

// Generate a random wallet and set the client to that
AsyncFunction("createRandom") { (environment: String, appVersion: String?) -> String in
AsyncFunction("createRandom") { (environment: String, appVersion: String?, hasCreateIdentityCallback: Bool?, hasEnableIdentityCallback: Bool?) -> String in
let privateKey = try PrivateKey.generate()
let preCreateIdentityCallback: PreEventCallback? = hasCreateIdentityCallback ?? false ? self.preCreateIdentityCallback : nil
let preEnableIdentityCallback: PreEventCallback? = hasEnableIdentityCallback ?? false ? self.preEnableIdentityCallback : nil

let options = createClientConfig(env: environment, appVersion: appVersion, preEnableIdentityCallback: preEnableIdentityCallback, preCreateIdentityCallback: preCreateIdentityCallback)
let client = try await Client.create(account: privateKey, options: options)
Expand Down Expand Up @@ -147,14 +151,14 @@ public class XMTPModule: Module {
return try await client.canMessage(peerAddress)
}

AsyncFunction("staticCanMessage") { (peerAddress: String, environment: String, appVersion: String?) -> Bool in
do {
let options = createClientConfig(env: environment, appVersion: appVersion)
return try await XMTP.Client.canMessage(peerAddress, options: options)
} catch {
throw Error.noClient
}
}
AsyncFunction("staticCanMessage") { (peerAddress: String, environment: String, appVersion: String?) -> Bool in
do {
let options = createClientConfig(env: environment, appVersion: appVersion)
return try await XMTP.Client.canMessage(peerAddress, options: options)
} catch {
throw Error.noClient
}
}

AsyncFunction("encryptAttachment") { (clientAddress: String, fileJson: String) -> String in
guard let client = await clientsManager.getClient(key: clientAddress) else {
Expand Down Expand Up @@ -507,29 +511,29 @@ public class XMTPModule: Module {
throw Error.noClient
}
let consentList = try await client.contacts.refreshConsentList()
return try consentList.entries.compactMap { entry in
try ConsentWrapper.encode(entry.value)
}

return try consentList.entries.compactMap { entry in
try ConsentWrapper.encode(entry.value)
}
}

AsyncFunction("conversationConsentState") { (clientAddress: String, conversationTopic: String) -> String in
guard let conversation = try await findConversation(clientAddress: clientAddress, topic: conversationTopic) else {
throw Error.conversationNotFound(conversationTopic)
}
return ConsentWrapper.consentStateToString(state: await conversation.consentState())
return ConsentWrapper.consentStateToString(state: await conversation.consentState())
}

AsyncFunction("consentList") { (clientAddress: String) -> [String] in
guard let client = await clientsManager.getClient(key: clientAddress) else {
throw Error.noClient
}
let entries = await client.contacts.consentList.entries
return try entries.compactMap { entry in
try ConsentWrapper.encode(entry.value)
}
}
AsyncFunction("consentList") { (clientAddress: String) -> [String] in
guard let client = await clientsManager.getClient(key: clientAddress) else {
throw Error.noClient
}
let entries = await client.contacts.consentList.entries

return try entries.compactMap { entry in
try ConsentWrapper.encode(entry.value)
}
}
}

//
Expand Down Expand Up @@ -666,11 +670,12 @@ public class XMTPModule: Module {
func getConversationsKey(clientAddress: String) -> String {
return "conversations:\(clientAddress)"
}
func preEnableIdentityCallback () -> Void {

func preEnableIdentityCallback() {
sendEvent("preEnableIdentityCallback")
}
func preCreateIdentityCallback () -> Void {

func preCreateIdentityCallback() {
sendEvent("preCreateIdentityCallback")
}
}
23 changes: 19 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@ export function address(): string {
export async function auth(
address: string,
environment: 'local' | 'dev' | 'production',
appVersion?: string | undefined
appVersion?: string | undefined,
hasCreateIdentityCallback?: boolean | undefined,
hasEnableIdentityCallback?: boolean | undefined
) {
return await XMTPModule.auth(address, environment, appVersion)
return await XMTPModule.auth(
address,
environment,
appVersion,
hasCreateIdentityCallback,
hasEnableIdentityCallback
)
}

export async function receiveSignature(requestID: string, signature: string) {
Expand All @@ -42,9 +50,16 @@ export async function receiveSignature(requestID: string, signature: string) {

export async function createRandom(
environment: 'local' | 'dev' | 'production',
appVersion?: string | undefined
appVersion?: string | undefined,
hasCreateIdentityCallback?: boolean | undefined,
hasEnableIdentityCallback?: boolean | undefined
): Promise<string> {
return await XMTPModule.createRandom(environment, appVersion)
return await XMTPModule.createRandom(
environment,
appVersion,
hasCreateIdentityCallback,
hasEnableIdentityCallback
)
}

export async function createFromKeyBundle(
Expand Down
8 changes: 6 additions & 2 deletions src/lib/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ export class Client<ContentTypes> {
XMTPModule.auth(
await signer.getAddress(),
options.env,
options.appVersion
options.appVersion,
Boolean(createSubscription),
Boolean(enableSubscription)
)
})()
this.removeSubscription(enableSubscription)
Expand Down Expand Up @@ -112,7 +114,9 @@ export class Client<ContentTypes> {
this.setupSubscriptions(options)
const address = await XMTPModule.createRandom(
options.env,
options.appVersion
options.appVersion,
Boolean(createSubscription),
Boolean(enableSubscription)
)
this.removeSubscription(enableSubscription)
this.removeSubscription(createSubscription)
Expand Down

0 comments on commit b4e7130

Please sign in to comment.