From 4592037b0916d77d30558ef987503145df54d893 Mon Sep 17 00:00:00 2001 From: b3hr4d Date: Sat, 5 Oct 2024 19:17:10 +0300 Subject: [PATCH] Refactor HttpAgent instantiation to use synchronous creation method --- examples/custom-provider/src/App.tsx | 10 ++++++---- examples/vite-react-motoko/vite.config.ts | 2 +- lerna.json | 2 +- packages/core/src/classes/actor/index.ts | 7 ++----- packages/core/src/classes/agent/index.ts | 15 ++++++++++++--- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/examples/custom-provider/src/App.tsx b/examples/custom-provider/src/App.tsx index 141c98c8dc..86ac80a383 100644 --- a/examples/custom-provider/src/App.tsx +++ b/examples/custom-provider/src/App.tsx @@ -79,10 +79,12 @@ const App: React.FC = () => { {functionNames.map((functionName) => ( ))} - + {principal && ( + + )} {principal && } diff --git a/examples/vite-react-motoko/vite.config.ts b/examples/vite-react-motoko/vite.config.ts index 2df73cf8c4..8086eede8f 100644 --- a/examples/vite-react-motoko/vite.config.ts +++ b/examples/vite-react-motoko/vite.config.ts @@ -38,6 +38,6 @@ export default defineConfig({ cache: { dir: '../node_modules/.vitest' }, }, resolve: { - preserveSymlinks: true, // this is the fix! + preserveSymlinks: true, }, }); diff --git a/lerna.json b/lerna.json index 4e267ba7b5..efb28e58be 100644 --- a/lerna.json +++ b/lerna.json @@ -2,6 +2,6 @@ "$schema": "node_modules/lerna/schemas/lerna-schema.json", "version": "independent", "npmClient": "yarn", - "packages": ["packages/*"], + "packages": ["packages/core", "packages/react"], "ignoreChanges": ["examples/**/*", "**/*.md", "**/*.spec.ts", "**/test/**/*"] } diff --git a/packages/core/src/classes/actor/index.ts b/packages/core/src/classes/actor/index.ts index c9586c5fca..708c93822f 100644 --- a/packages/core/src/classes/actor/index.ts +++ b/packages/core/src/classes/actor/index.ts @@ -173,11 +173,8 @@ export class ActorManager { } private initializeActor = (agent: HttpAgent) => { - console.info( - `Initializing actor ${ - this.canisterId - } on ${this._agentManager.getNetwork()} network` - ) + const network = this._agentManager.getNetwork() + console.info(`Initializing actor ${this.canisterId} on ${network} network`) const { _idlFactory, canisterId } = this diff --git a/packages/core/src/classes/agent/index.ts b/packages/core/src/classes/agent/index.ts index be5b8ed384..9f89190ec9 100644 --- a/packages/core/src/classes/agent/index.ts +++ b/packages/core/src/classes/agent/index.ts @@ -23,7 +23,9 @@ import { } from "../../utils/constants" export class AgentManager { + private _anonymousAgent: HttpAgent private _agent: HttpAgent + private _auth: AuthClient | null = null private _subscribers: Array<(agent: HttpAgent) => void> = [] @@ -55,7 +57,8 @@ export class AgentManager { ) } - private updateAuthState = (newState: Partial, action?: string) => { + //TODO: make it private + public updateAuthState = (newState: Partial, action?: string) => { this.authStore.setState( (state) => ({ ...state, ...newState }), false, @@ -93,6 +96,7 @@ export class AgentManager { store: "auth", }) + this._anonymousAgent = HttpAgent.createSync(agentOptions) this._agent = HttpAgent.createSync(agentOptions) this.initializeAgent() } @@ -110,6 +114,7 @@ export class AgentManager { if (network !== "ic") { try { await this._agent.fetchRootKey() + await this._anonymousAgent.fetchRootKey() } catch (error) { this.updateAgentState( { error: error as Error, initializing: false }, @@ -222,16 +227,20 @@ export class AgentManager { return this._agent } - public getAgentHost = () => { + public getAgentHost = (): URL | undefined => { return this._agent.host } + public getAgentHostName = () => { + return this.getAgentHost()?.hostname || "" + } + public getIsLocal = () => { return this.getNetwork() !== "ic" } public getNetwork = () => { - const hostname = this.getAgentHost().hostname + const hostname = this.getAgentHostName() if (LOCAL_HOSTS.some((host) => hostname.endsWith(host))) { return "local"