@@ -8,34 +8,42 @@ import {_SERVICE as _STORAGE_SERVICE} from '../declarations/storage/storage.did.
8
8
import { getEndpoint } from './network.js' ;
9
9
import { getNetwork } from './network.js' ;
10
10
11
- export let mainActor = async ( identity ?: Identity ) : Promise < _SERVICE > => {
12
- let network = getNetwork ( ) ;
13
- let host = getEndpoint ( network ) . host ;
14
- let canisterId = getEndpoint ( network ) . canisterId ;
11
+ let agentPromiseByPrincipal = new Map < string , Promise < HttpAgent > > ( ) ;
12
+
13
+ let getAgent = async ( identity ?: Identity ) : Promise < HttpAgent > => {
14
+ let principal = identity ? identity ?. getPrincipal ( ) . toText ( ) : '' ;
15
+ let agentPromise = agentPromiseByPrincipal . get ( principal ) ;
15
16
16
- // @ts -ignore exactOptionalPropertyTypes
17
- let agent = new HttpAgent ( { host, identity} ) ;
17
+ if ( ! agentPromise ) {
18
+ let network = getNetwork ( ) ;
19
+ let host = getEndpoint ( network ) . host ;
18
20
19
- if ( network === 'local' ) {
20
- await agent . fetchRootKey ( ) ;
21
+ agentPromise = HttpAgent . create ( {
22
+ host,
23
+ identity,
24
+ shouldFetchRootKey : network === 'local' ,
25
+ verifyQuerySignatures : process . env . MOPS_VERIFY_QUERY_SIGNATURES !== 'false' ,
26
+ } ) ;
27
+
28
+ agentPromiseByPrincipal . set ( principal , agentPromise ) ;
21
29
}
22
30
31
+ return agentPromise ;
32
+ } ;
33
+
34
+ export let mainActor = async ( identity ?: Identity ) : Promise < _SERVICE > => {
35
+ let agent = await getAgent ( identity ) ;
36
+ let network = getNetwork ( ) ;
37
+ let canisterId = getEndpoint ( network ) . canisterId ;
38
+
23
39
return Actor . createActor ( idlFactory , {
24
40
agent,
25
41
canisterId,
26
42
} ) ;
27
43
} ;
28
44
29
45
export let storageActor = async ( storageId : Principal , identity ?: Identity ) : Promise < _STORAGE_SERVICE > => {
30
- let network = getNetwork ( ) ;
31
- let host = getEndpoint ( network ) . host ;
32
-
33
- // @ts -ignore exactOptionalPropertyTypes
34
- let agent = new HttpAgent ( { host, identity} ) ;
35
-
36
- if ( network === 'local' ) {
37
- await agent . fetchRootKey ( ) ;
38
- }
46
+ let agent = await getAgent ( identity ) ;
39
47
40
48
return Actor . createActor ( storageIdlFactory , {
41
49
agent,
0 commit comments