Skip to content

Commit

Permalink
style: refactor query promises (#802)
Browse files Browse the repository at this point in the history
  • Loading branch information
krpeacock authored Nov 17, 2023
1 parent 3495986 commit da0e483
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/agent/src/agent/http/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ test('queries with the same content should have the same signature', async () =>
const response4 = await httpAgent.query(canisterIdent, { methodName, arg });

const { calls } = mockFetch.mock;
expect(calls.length).toBe(6);
expect(calls.length).toBe(4);

expect(calls[0]).toEqual(calls[1]);
expect(response1).toEqual(response2);
Expand Down
36 changes: 12 additions & 24 deletions packages/agent/src/agent/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,34 +500,20 @@ export class HttpAgent implements Agent {
requestId,
};
};
const queryPromise = new Promise<ApiQueryResponse>((resolve, reject) => {
makeQuery()
.then(response => {
resolve(response);
})
.catch(error => {
reject(error);
});
});

const subnetStatusPromise = new Promise<SubnetStatus | void>((resolve, reject) => {
const getSubnetStatus = async (): Promise<SubnetStatus | void> => {
if (!this.#verifyQuerySignatures) {
resolve(undefined);
return undefined;
}
const subnetStatus = this.#subnetKeys.get(canisterId.toString());
if (subnetStatus) {
resolve(subnetStatus);
} else {
this.fetchSubnetKeys(canisterId)
.then(response => {
resolve(response);
})
.catch(error => {
reject(error);
});
return subnetStatus;
}
});
const [query, subnetStatus] = await Promise.all([queryPromise, subnetStatusPromise]);
await this.fetchSubnetKeys(canisterId.toString());
return this.#subnetKeys.get(canisterId.toString());
};
// Make query and fetch subnet keys in parallel
const [query, subnetStatus] = await Promise.all([makeQuery(), getSubnetStatus()]);
// Skip verification if the user has disabled it
if (!this.#verifyQuerySignatures) {
return query;
Expand Down Expand Up @@ -751,7 +737,7 @@ export class HttpAgent implements Agent {
this._identity = Promise.resolve(identity);
}

public async fetchSubnetKeys(canisterId: Principal | string): Promise<any> {
public async fetchSubnetKeys(canisterId: Principal | string) {
const effectiveCanisterId: Principal = Principal.from(canisterId);
const response = await request({
canisterId: effectiveCanisterId,
Expand All @@ -762,8 +748,10 @@ export class HttpAgent implements Agent {
const subnetResponse = response.get('subnet');
if (subnetResponse && typeof subnetResponse === 'object' && 'nodeKeys' in subnetResponse) {
this.#subnetKeys.set(effectiveCanisterId.toText(), subnetResponse as SubnetStatus);
return subnetResponse as SubnetStatus;
}
return subnetResponse;
// If the subnet status is not returned, return undefined
return undefined;
}

protected _transform(request: HttpAgentRequest): Promise<HttpAgentRequest> {
Expand Down

0 comments on commit da0e483

Please sign in to comment.