Skip to content

Commit

Permalink
Add custom client id parameter to connect()
Browse files Browse the repository at this point in the history
  • Loading branch information
platfowner committed Aug 13, 2024
1 parent 9ae5a6b commit 9dc961c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
16 changes: 11 additions & 5 deletions src/ainize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,27 @@ export default class Ainize {

/**
* Login to ainize using AI Network account private key.
* @param {string} privateKey
* @param {string} privateKey The private key to initialize the AinModule with.
* @param {ConnectionCallback} ConnectionCallback The connection callback function.
* @param {DisconnectionCallback} disconnectionCallback The disconnection callback function.
* @param {string} customClientId The custom client id to set.
*/
async login(privateKey: string, connectionCb?: ConnectionCallback, disconnectionCb?: DisconnectionCallback) {
async login(privateKey: string, connectionCb?: ConnectionCallback, disconnectionCb?: DisconnectionCallback, customClientId?: string) {
this.ain.setDefaultAccount(privateKey);
await this.handler.connect(connectionCb, disconnectionCb);
await this.handler.connect(connectionCb, disconnectionCb, customClientId);
console.log('login success! address:', await this.ain.getAddress());
}

/**
* Login to ainize using AIN Wallet Signer.
* @param {ConnectionCallback} ConnectionCallback The connection callback function.
* @param {DisconnectionCallback} disconnectionCallback The disconnection callback function.
* @param {string} customClientId The custom client id to set.
*/
async loginWithSigner(connectionCb?: ConnectionCallback, disconnectionCb?: DisconnectionCallback) {
async loginWithSigner(connectionCb?: ConnectionCallback, disconnectionCb?: DisconnectionCallback, customClientId?: string) {
const signer = new AinWalletSigner;
this.ain.setSigner(signer);
await this.handler.connect(connectionCb, disconnectionCb);
await this.handler.connect(connectionCb, disconnectionCb, customClientId);
console.log('login success! address: ', await this.ain.getAddress());
}

Expand Down
8 changes: 4 additions & 4 deletions src/handlers/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export default class Handler {
return this.ain.getEventManager().isConnected();
}

async connect(connectionCb?: ConnectionCallback, disconnectionCb?: DisconnectionCallback) {
async connect(connectionCb?: ConnectionCallback, disconnectionCb?: DisconnectionCallback, customClientId?: string) {
this.checkEventManager();
await this.ain.getEventManager().connect(connectionCb, this.connectionRetryCb.bind(this, connectionCb, disconnectionCb));
await this.ain.getEventManager().connect(connectionCb, this.connectionRetryCb.bind(this, connectionCb, disconnectionCb, customClientId));
console.log('connected');
};

Expand All @@ -36,15 +36,15 @@ export default class Handler {
console.log('Disconnected');
}

private async connectionRetryCb(connectionCb?: ConnectionCallback, disconnectionCb?: DisconnectionCallback, webSocket?: any) {
private async connectionRetryCb(connectionCb?: ConnectionCallback, disconnectionCb?: DisconnectionCallback, customClientId?: string, webSocket?: any) {
try {
if (disconnectionCb) {
disconnectionCb(webSocket);
}
const address = await AinModule.getInstance().getAddress();
if (address) {
console.log('Disconnected. Reconnecting...');
await this.connect(connectionCb, disconnectionCb);
await this.connect(connectionCb, disconnectionCb, customClientId);
}
} catch (_) {
return;
Expand Down

0 comments on commit 9dc961c

Please sign in to comment.