Skip to content

Commit

Permalink
Merge pull request #49 from ainize-team/fix/woojae/ainInit
Browse files Browse the repository at this point in the history
fix: handler init
  • Loading branch information
akastercomcom authored Sep 20, 2023
2 parents 93d4296 + 2b39280 commit ee5a97f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/ain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export default class AinModule {
createAccount() {
this.checkAinInitiated();
const newAccount = this.ain!.wallet.create(1)[0];
return newAccount;
const wallet = this.ain!.wallet.accounts[newAccount];
this.ain!.wallet.remove(newAccount);
return wallet;
}

setDefaultAccount(privateKey: string) {
Expand Down
15 changes: 11 additions & 4 deletions src/ainize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export default class Ainize {

constructor(chainId: 1 | 0) {
this.ain.initAin(chainId);
this.handler.connect();
this.cache = new NodeCache();
this.middleware = new Middleware(this.cache);
this.internal = new Internal();
Expand All @@ -28,12 +27,20 @@ export default class Ainize {
return AinModule.getInstance().createAccount();
}

login(privateKey: string) {
async login(privateKey: string) {
this.ain.setDefaultAccount(privateKey);
await this.handler.connect();
console.log('login success! address:', this.ain.getAddress());
}

logout() {
async logout() {
await this.handler.disconnect();
this.ain.removeDefaultAccount();
console.log('logout success!');
}

async getAddress(): Promise<string> {
return await this.ain.getAddress();
}

async getAinBalance(): Promise<number> {
Expand All @@ -56,7 +63,7 @@ export default class Ainize {
}

await this.appController.createApp({ appName: modelName, serviceUrl, billingConfig });
return new Model(modelName);
return this.model(modelName);
}

model(modelName: string): Model {
Expand Down
23 changes: 13 additions & 10 deletions src/handlers/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import Ain from "@ainblockchain/ain-js";
import Ainize from "../ainize";
import { Path } from "../constants";
import AinModule from "../ain";
import EventManager from "@ainblockchain/ain-js/lib/event-manager";

export default class Handler {
private static instance: Handler | undefined;
em = AinModule.getInstance().getEventManager();
ain = AinModule.getInstance();

static getInstance() {
if(!Handler.instance){
Handler.instance = new Handler();
Expand All @@ -16,20 +16,23 @@ export default class Handler {
}

checkEventManager() {
if (!this.em) {
if(!AinModule.getInstance().getEventManager()){
throw new Error('you should init ain first');
}
this.em = AinModule.getInstance().getEventManager();
if(!AinModule.getInstance().getEventManager()){
throw new Error('you should init ain first');
}
return true;
}

async connect() {
this.checkEventManager();
await this.em!.connect({},this.disconnectedCb);
await this.ain.getEventManager().connect({},this.disconnectedCb);
console.log('connected');
};

async disconnect() {
this.checkEventManager();
await this.ain.getEventManager().disconnect();
console.log('disconnected');
}

private async disconnectedCb() {
console.log('disconnected. reconnecting...');
Expand All @@ -39,7 +42,7 @@ export default class Handler {
async subscribe(requester:string, recordId:string, appName: string, resolve: any) {
this.checkEventManager();
const responsePath = Path.app(appName).response(requester, recordId);
const subscribeId = await this.em!.subscribe(
const subscribeId = await this.ain.getEventManager().subscribe(
"VALUE_CHANGED",
{
path: responsePath,
Expand All @@ -57,7 +60,7 @@ export default class Handler {

async unsubscribe(filterId: string) {
this.checkEventManager();
await this.em!.unsubscribe(
await this.ain.getEventManager().unsubscribe(
filterId,
(err)=>{
if (err) {
Expand Down
2 changes: 1 addition & 1 deletion src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ModelController from "./controllers/modelController";

export default class Model {
modelName: string;
modelController: ModelController;
private modelController: ModelController;

constructor(modelName: string) {
this.modelName = modelName;
Expand Down

0 comments on commit ee5a97f

Please sign in to comment.