Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate connection retry callback and support custom client id parameter #129

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"typescript": "^4.6.3"
},
"dependencies": {
"@ainblockchain/ain-js": "^1.12.0",
"@ainblockchain/ain-js": "^1.13.0",
"axios": "^0.26.1",
"express": "^4.18.2",
"fast-json-stable-stringify": "^2.1.0",
Expand Down
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
19 changes: 2 additions & 17 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, disconnectionCb, customClientId);
console.log('connected');
};

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

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

async subscribe(subscribePath: string, resolve: any) {
this.checkEventManager();

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# yarn lockfile v1


"@ainblockchain/ain-js@^1.12.0":
version "1.12.0"
resolved "https://registry.yarnpkg.com/@ainblockchain/ain-js/-/ain-js-1.12.0.tgz#6bd2c051ae200b6a8eee28d175f14fa677f56e04"
integrity sha512-wsipsiVzgtAkNhBR04t/UlQ7nXnIt1MRU11kctYzCbFsz8YrVhrX6WM2kXxWEsm37KnmtsZkt7f6WgfO/JdM4Q==
"@ainblockchain/ain-js@^1.13.0":
version "1.13.0"
resolved "https://registry.yarnpkg.com/@ainblockchain/ain-js/-/ain-js-1.13.0.tgz#547a036385ac8ea2fb9a643cbf8151bf4c4b65ed"
integrity sha512-XFxbxamfoUbgh6iOUjIQZPduotEI32lsRZxYCjD5SGlQ/kXzdrZ9rz0EJCFoE0O/Nu6bawC7h96Jvc8M/xon0w==
dependencies:
"@ainblockchain/ain-util" "^1.2.1"
"@types/node" "^12.7.3"
Expand Down
Loading