Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: refactor query promises #802

Merged
merged 11 commits into from
Nov 17, 2023
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
Loading