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

fix: handler init #49

Merged
merged 3 commits into from
Sep 20, 2023
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
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 @@ -15,7 +15,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);
}
Expand All @@ -24,12 +23,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 @@ -52,7 +59,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