Skip to content

Commit

Permalink
work on client connexion with host
Browse files Browse the repository at this point in the history
  • Loading branch information
Soushi888 committed Feb 16, 2024
1 parent daba648 commit 36cf9d5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 31 deletions.
58 changes: 28 additions & 30 deletions ui/src/lib/utils/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CellType, type AppInfo, AdminWebsocket, AppAgentWebsocket } from '@holochain/client';
import { log } from 'console';

export const HOLOCHAIN_APP_ID = 'mewsfeed';
export const HOLOCHAIN_APP_ID = 'requests-and-offers';
export const IS_LAUNCHER = import.meta.env.VITE_IS_LAUNCHER;
export const IS_HOLO_HOSTED = import.meta.env.VITE_IS_HOLO_HOSTED;

Expand All @@ -10,22 +11,19 @@ export const IS_HOLO_HOSTED = import.meta.env.VITE_IS_HOLO_HOSTED;
* @return {Promise<AppAgentWebsocket>} The connected Holochain client.
*/
export const setupHolochain = async () => {
try {
console.log(import.meta.env);
const client = await AppAgentWebsocket.connect(
IS_LAUNCHER ? `ws://UNUSED` : `ws://localhost:${import.meta.env.VITE_HC_PORT}`,
HOLOCHAIN_APP_ID,
60000
);
if (typeof window === 'object' && !('__HC_LAUNCHER_ENV__' in window)) {
const appInfo = await client.appInfo();
await authorizeClient(appInfo);
}
return client;
} catch (e) {
console.log('Holochain client setup error', e);
throw e;
}
log('Setting up holochain');
log(import.meta.env);
// try {
// const client = await AppAgentWebsocket.connect(
// IS_LAUNCHER ? `ws://UNUSED` : `ws://localhost:${import.meta.env.VITE_HC_PORT}`,
// HOLOCHAIN_APP_ID,
// 60000
// );
// return client;
// } catch (e) {
// console.log('Holochain client setup error', e);
// throw e;
// }
};

/**
Expand All @@ -34,16 +32,16 @@ export const setupHolochain = async () => {
* @param {AppInfo} appInfo - information about the Holochain app
* @return {Promise<void>} a Promise that resolves when the client is authorized
*/
export const authorizeClient = async (appInfo: AppInfo) => {
if (typeof window === 'object' && !('__HC_LAUNCHER_ENV__' in window)) {
if (!(CellType.Provisioned in appInfo.cell_info.mewsfeed[0])) {
throw new Error('mewsfeed cell not provisioned');
}
const { cell_id } = appInfo.cell_info.mewsfeed[0][CellType.Provisioned];
const adminWs = await AdminWebsocket.connect(
`ws://localhost:${import.meta.env.VITE_HC_ADMIN_PORT}`
);
await adminWs.authorizeSigningCredentials(cell_id);
console.log('Holochain app client authorized for zome calls');
}
};
// export const authorizeClient = async (appInfo: AppInfo) => {
// if (typeof window === 'object' && !('__HC_LAUNCHER_ENV__' in window)) {
// if (!(CellType.Provisioned in appInfo.cell_info.mewsfeed[0])) {
// throw new Error('mewsfeed cell not provisioned');
// }
// const { cell_id } = appInfo.cell_info.mewsfeed[0][CellType.Provisioned];
// const adminWs = await AdminWebsocket.connect(
// `ws://localhost:${import.meta.env.VITE_HC_ADMIN_PORT}`
// );
// await adminWs.authorizeSigningCredentials(cell_id);
// console.log('Holochain app client authorized for zome calls');
// }
// };
6 changes: 6 additions & 0 deletions ui/src/lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* Check if the given object is empty.
*
* @param {object} obj - The object to be checked for emptiness
* @return {boolean} True if the object is empty, otherwise false
*/
export function isEmptyObj(obj: object) {
return Object.keys(obj).length === 0;
}
4 changes: 3 additions & 1 deletion ui/src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { setupHolochain } from "$lib/utils/client";

export const load: PageLoad = async () => {
// setupHolochain();
setupHolochain();
};

0 comments on commit 36cf9d5

Please sign in to comment.