From 1718fc02be45a391d39adf6a2390272efc1f42f0 Mon Sep 17 00:00:00 2001 From: akaster99 Date: Thu, 21 Sep 2023 14:30:48 +0900 Subject: [PATCH] refactor: integrate subscribe --- src/ainize.ts | 3 ++- src/controllers/modelController.ts | 3 ++- src/handlers/handler.ts | 23 ++--------------------- 3 files changed, 6 insertions(+), 23 deletions(-) diff --git a/src/ainize.ts b/src/ainize.ts index b939b01..8389bb5 100644 --- a/src/ainize.ts +++ b/src/ainize.ts @@ -65,7 +65,8 @@ export default class Ainize { if (!serviceUrl) { serviceUrl = `https://${modelName}.ainetwork.xyz`; } - await this.handler.subscribeDeploy(modelName, resolve); + const modelPath = Path.app(modelName).root(); + await this.handler.subscribe(modelPath, resolve); await this.appController.createApp({ appName: modelName, serviceUrl, billingConfig }); }); console.log(`${modelName} deploy success!`); diff --git a/src/controllers/modelController.ts b/src/controllers/modelController.ts index 0d3350d..51982a9 100644 --- a/src/controllers/modelController.ts +++ b/src/controllers/modelController.ts @@ -82,7 +82,8 @@ export default class ModelController { const result = await new Promise(async (resolve, reject) => { const requestKey = Date.now(); const requesterAddress = this.ain.getAddress(); - await this.handler.subscribeResponse(requesterAddress, requestKey.toString(), modelName, resolve); + const responsePath = Path.app(modelName).response(requesterAddress, requestKey.toString()); + await this.handler.subscribe(responsePath, resolve); const requestPath = Path.app(modelName).request(requesterAddress, requestKey); const requestOp = buildSetOperation("SET_VALUE", requestPath, {prompt: requestData}); const txBody = buildTxBody(requestOp); diff --git a/src/handlers/handler.ts b/src/handlers/handler.ts index b996e5a..758e3c2 100644 --- a/src/handlers/handler.ts +++ b/src/handlers/handler.ts @@ -41,32 +41,13 @@ export default class Handler { } } - async subscribeResponse(requester:string, recordId:string, appName: string, resolve: any) { + async subscribe(subscribePath: string, resolve: any) { this.checkEventManager(); - const responsePath = Path.app(appName).response(requester, recordId); - const subscribeId = await this.ain.getEventManager().subscribe( - "VALUE_CHANGED", - { - path: responsePath, - event_source: "USER", - }, - (valueChangedEvent: any) => { - this.unsubscribe(subscribeId); - resolve(valueChangedEvent.values.after.data); - }, - (err) => { - throw new Error(err.message); - }, - ); - } - async subscribeDeploy(appName: string, resolve: any) { - this.checkEventManager(); - const appPath = Path.app(appName).root(); const subscribeId = await this.ain.getEventManager().subscribe( "VALUE_CHANGED", { - path: appPath, + path: subscribePath, event_source: "USER", }, (valueChangedEvent: any) => {