Parcourir la source

🚧 change user assets loading strategy

jmacura il y a 3 ans
Parent
commit
8776274823

+ 1 - 1
src/app/bc-info/bc-info.component.html

@@ -1,3 +1,3 @@
-My account balance: {{getUserBalance()}}$$
+My account balance: {{bcInfoService.userBalance}} $$ <button (click)="updateUserBalance()">UPDATE</button>
 <br>
 Price: $$

+ 2 - 2
src/app/bc-info/bc-info.component.ts

@@ -11,7 +11,7 @@ export class BcInfoComponent implements OnInit {
 
   ngOnInit() {}
 
-  getUserBalance() {
-    return this.bcInfoService.getUserBalance();
+  updateUserBalance() {
+    this.bcInfoService.getUserBalance();
   }
 }

+ 34 - 15
src/app/bc-info/bc-info.service.ts

@@ -13,35 +13,54 @@ export class BcInfoService {
   IROHA_ADDRESS = "localhost:50051" as const;
   ASSET = "testcoin#test" as const;
 
-  user = 'jmacura';
-  userPrivateKey = 'test';
-  user_balance = -1;
-
   commandService: CommandService;
   queryService: QueryService;
+  user = 'jmacura';
+  userPrivateKey = 'test';
+  userBalance = -1;
+  private _requestInProcess = false;
 
   constructor(public httpClient: HttpClient) { 
     this.commandService = new CommandService(this.IROHA_ADDRESS);
     this.queryService = new QueryService(this.IROHA_ADDRESS);
+    this.getUserBalance();
+  }
+
+  async getUserBalance(): Promise<any> {
+    if (this._requestInProcess) {
+      return;
+    }
+    this.userBalance = await this.getUserBalanceFromIroha()
   }
 
-  getUserBalance() : any{
+  async getUserBalanceFromIroha(): Promise<any> {
+    this._requestInProcess = true;
     let queryOptions = {
       privateKey: this.userPrivateKey,
       creatorAccountId: this.user,
       queryService: this.queryService,
       timeoutLimit: 5000
     };
-    
-    queries.getAccountAssets(queryOptions, {
-      accountId: this.user,
-      firstAssetId: undefined,
-      pageSize: 10
-    })
-    .then((data) => console.log(data))
-    .catch((err) => console.error(err));
-
-    return "test";
 
+    let data;
+    try {
+      // For testing
+      /*data = await new Promise((resolve, reject) => {
+        setTimeout(() => {
+          resolve(Math.random()*100);
+        }, 1500);
+      });*/
+      data = await queries.getAccountAssets(queryOptions, {
+        accountId: this.user,
+        firstAssetId: undefined,
+        pageSize: 10
+      })
+    } catch(err) {
+      this._requestInProcess = false
+      console.error(err)
+    }
+    this._requestInProcess = false
+    console.log(data)
+    return data
   }
 }