Skip to content

Commit

Permalink
Refactor HttpAgent instantiation to use synchronous creation method
Browse files Browse the repository at this point in the history
  • Loading branch information
b3hr4d committed Oct 5, 2024
1 parent 06725ac commit 4592037
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
10 changes: 6 additions & 4 deletions examples/custom-provider/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ const App: React.FC<AppProps> = () => {
{functionNames.map((functionName) => (
<ICRC1Call key={functionName} functionName={functionName} />
))}
<ICRC1Call
functionName="icrc1_balance_of"
args={[{ owner: Principal.anonymous(), subaccount: [] }]}
/>
{principal && (
<ICRC1Call
functionName="icrc1_balance_of"
args={[{ owner: principal, subaccount: [] }]}
/>
)}
{principal && <UserWallet principal={principal} />}
</ICRC1Provider>
</CandidAdapterProvider>
Expand Down
2 changes: 1 addition & 1 deletion examples/vite-react-motoko/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ export default defineConfig({
cache: { dir: '../node_modules/.vitest' },
},
resolve: {
preserveSymlinks: true, // this is the fix!
preserveSymlinks: true,
},
});
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/**/*"]
}
7 changes: 2 additions & 5 deletions packages/core/src/classes/actor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,8 @@ export class ActorManager<A = BaseActor> {
}

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

Expand Down
15 changes: 12 additions & 3 deletions packages/core/src/classes/agent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> = []

Expand Down Expand Up @@ -55,7 +57,8 @@ export class AgentManager {
)
}

private updateAuthState = (newState: Partial<AuthState>, action?: string) => {
//TODO: make it private
public updateAuthState = (newState: Partial<AuthState>, action?: string) => {
this.authStore.setState(
(state) => ({ ...state, ...newState }),
false,
Expand Down Expand Up @@ -93,6 +96,7 @@ export class AgentManager {
store: "auth",
})

this._anonymousAgent = HttpAgent.createSync(agentOptions)
this._agent = HttpAgent.createSync(agentOptions)
this.initializeAgent()
}
Expand All @@ -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 },
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 4592037

Please sign in to comment.