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

Support connection / disconnection callback functions #126

Merged
merged 4 commits into from
Jul 18, 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.10.2",
"@ainblockchain/ain-js": "^1.12.0",
"axios": "^0.26.1",
"express": "^4.18.2",
"fast-json-stable-stringify": "^2.1.0",
Expand Down
9 changes: 5 additions & 4 deletions src/ainize.ts
Copy link
Contributor

@jiyoung-an jiyoung-an Jul 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@jiyoung-an jiyoung-an Jul 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've checked that the disconnection function in ain-js does not have a callback option.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(discussed offline) A disconnection callback can be added with login functions.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import AinModule from "./ain";
import Internal from "./internal";
import { Account } from "@ainblockchain/ain-util";
import { AinWalletSigner } from "@ainblockchain/ain-js/lib/signer/ain-wallet-signer";
import { ConnectionCallback, DisconnectionCallback } from "@ainblockchain/ain-js/lib/types";

export default class Ainize {
private cache: NodeCache;
Expand Down Expand Up @@ -37,19 +38,19 @@ export default class Ainize {
* Login to ainize using AI Network account private key.
* @param {string} privateKey
*/
async login(privateKey: string) {
async login(privateKey: string, connectionCb?: ConnectionCallback, disconnectionCb?: DisconnectionCallback) {
this.ain.setDefaultAccount(privateKey);
await this.handler.connect();
await this.handler.connect(connectionCb, disconnectionCb);
console.log('login success! address:', await this.ain.getAddress());
}

/**
* Login to ainize using AIN Wallet Signer.
*/
async loginWithSigner() {
async loginWithSigner(connectionCb?: ConnectionCallback, disconnectionCb?: DisconnectionCallback) {
const signer = new AinWalletSigner;
this.ain.setSigner(signer);
await this.handler.connect();
await this.handler.connect(connectionCb, disconnectionCb);
console.log('login success! address: ', await this.ain.getAddress());
}

Expand Down
12 changes: 8 additions & 4 deletions src/handlers/handler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ConnectionCallback, DisconnectionCallback } from "@ainblockchain/ain-js/lib/types";
import AinModule from "../ain";
import _ from "lodash";

Expand All @@ -23,9 +24,9 @@ export default class Handler {
return this.ain.getEventManager().isConnected();
}

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

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

private async disconnectedCb() {
private async connectionRetryCb(connectionCb?: ConnectionCallback, disconnectionCb?: DisconnectionCallback, webSocket?: any) {
try {
if (disconnectionCb) {
disconnectionCb(webSocket);
Copy link
Contributor

@jiyoung-an jiyoung-an Jul 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is the webSocket passed from?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's the last param of connectionRetryCb().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
const address = await AinModule.getInstance().getAddress();
if (address) {
console.log('Disconnected. Reconnecting...');
await this.connect();
await this.connect(connectionCb, disconnectionCb);
}
} catch (_) {
return;
Expand Down
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# yarn lockfile v1


"@ainblockchain/ain-js@^1.10.2":
version "1.10.2"
resolved "https://registry.yarnpkg.com/@ainblockchain/ain-js/-/ain-js-1.10.2.tgz#c0ed1e230a6c2b3703169a5c243c173944004bdd"
integrity sha512-2lHYnvbjEHm3/K5Lw5wCNVBtj61Z1p1u+FbveXMg4vDCwUnuYwvCrtZjTxxv13MXHoJ6izdkkA3X93GWtQhpow==
"@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==
dependencies:
"@ainblockchain/ain-util" "^1.1.9"
"@ainblockchain/ain-util" "^1.2.1"
"@types/node" "^12.7.3"
"@types/randombytes" "^2.0.0"
"@types/semver" "^7.3.4"
Expand All @@ -31,10 +31,10 @@
uuid "^3.3.3"
ws "^8.16.0"

"@ainblockchain/ain-util@^1.1.9":
version "1.1.9"
resolved "https://registry.npmjs.org/@ainblockchain/ain-util/-/ain-util-1.1.9.tgz"
integrity sha512-u3q0h0zwWk+vzZ6VpBZiagVKJbNw/Dw4LVjBAhOvgPCx/E3jHHQCufIMDGqD4wjeBuHVtTAQyMTv7LRPSZFBGg==
"@ainblockchain/ain-util@^1.2.1":
version "1.2.1"
resolved "https://registry.yarnpkg.com/@ainblockchain/ain-util/-/ain-util-1.2.1.tgz#c5445f4718da0820c4955be44b07400172a864ef"
integrity sha512-++Sjv4PBT2/sdHeQNCDNb67ZIObxQnRQzILh+E72BUNnaEcS9cf9+4RC9I8JRqWkBiPvsD7oJPpMYYn0xwfECg==
dependencies:
bip39 "^3.0.4"
bn.js "^4.11.8"
Expand Down
Loading