diff --git a/src/stores/useClientStore.js b/src/stores/useClientStore.js index aabe342..5d49818 100644 --- a/src/stores/useClientStore.js +++ b/src/stores/useClientStore.js @@ -19,7 +19,7 @@ const makeUseClientStore = ({ useInterfaceStore, onInit }) => defineStore('clien useInterfaceStore().$subscribe((_, state) => { // This could be more efficient by inspecting the contents of mutation - this.isReady = state.isReady + this.isReady = state if (state.appInfo?.agent_pub_key) { this.agentKey = state.appInfo.agent_pub_key diff --git a/src/stores/useHoloportAPIStore.js b/src/stores/useHoloportAPIStore.js index 3251ea9..182fb0d 100644 --- a/src/stores/useHoloportAPIStore.js +++ b/src/stores/useHoloportAPIStore.js @@ -1,28 +1,69 @@ import axios from 'axios' -import { generateSigningKeyPair, setSigningCredentials } from '@holochain/client' +import { AppWebsocket, generateSigningKeyPair, setSigningCredentials } from '@holochain/client' import { defineStore } from 'pinia' import { listify } from '../utils' import { kycLevel2 } from '../services/hbs' -const makeUseHoloportAPIStore = ({ useHolochainStore }) => defineStore('holoportAPI', { +const makeUseHoloportAPIStore = () => defineStore('holoportAPI', { state: () => ({ - client: useHolochainStore().client, + client: null, // These two values are subscribed to by clientStore - appInfo: useHolochainStore().appInfo, - isReady: useHolochainStore().isReady, - signingCredentials: useHolochainStore().signingCredentials + appInfo: null, + isReady: false, + signingCredentials: null }), actions: { async initialize() { - useHolochainStore().initialize() + try { + const holochainClient = await AppWebsocket.connect( + app_ws_url, + HC_APP_TIMEOUT, + signal => useSignalStore().handleSignal(presentHcSignal(signal)) + ) + + this.client = holochainClient + + holochainClient.client.socket.onclose = function(e) { + console.log( + 'Socket to Holochain App Interface has closed.', + inspect(e) + ) + this.client = null + this.isReady = false + } + + this.loadAppInfo() + } catch (e) { + console.error('Holochain connection error ', e) + this.isReady = false + } }, async loadAppInfo() { - useHolochainStore().loadAppInfo() + try { + const appInfo = await this.client.appInfo({ + installed_app_id + }) + this.appInfo = appInfo + this.isReady = true + + return appInfo + } catch (error) { + console.error('appInfo() returned error.', inspect(error)) + } }, async callZome(args) { - useHolochainStore().callZome(args) + const zomeCallArgs = { + appId: installed_app_id, + roleId: args.role_name, + zomeName: args.zome_name, + fnName: args.fn_name, + payload: args.payload + } + + const response = await this.hposHolochainCall({path: 'zome_call', headers: {}, params: zomeCallArgs}) + return response }, setCredentials(cellId) { this.signingCredentials = new Promise(async (resolve, reject) => {