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

fix: ObservableLog no longer extends Function #887

Merged
merged 4 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Changed

- fix: ObservableLog no longer extends Function and class instance can no longer be called. Fixes an issue when running in a browser extension context.
- chore: updates dfinity/conventional-pr-title-action to v3.2.0

## [1.3.0] - 2024-05-01
Expand Down
31 changes: 17 additions & 14 deletions packages/agent/src/agent/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,10 @@ export class HttpAgent implements Agent {

const body = cbor.encode(transformedRequest.body);

this.log(`fetching "/api/v2/canister/${ecid.toText()}/call" with request:`, transformedRequest);
this.log.log(
`fetching "/api/v2/canister/${ecid.toText()}/call" with request:`,
transformedRequest,
);

// Run both in parallel. The fetch is quite expensive, so we have plenty of time to
// calculate the requestId locally.
Expand Down Expand Up @@ -455,7 +458,7 @@ export class HttpAgent implements Agent {
const { ecid, transformedRequest, body, requestId, backoff, tries } = args;

const delay = tries === 0 ? 0 : backoff.next();
this.log(`fetching "/api/v2/canister/${ecid.toString()}/query" with tries:`, {
this.log.log(`fetching "/api/v2/canister/${ecid.toString()}/query" with tries:`, {
tries,
backoff,
delay,
Expand All @@ -476,7 +479,7 @@ export class HttpAgent implements Agent {
let response: ApiQueryResponse;
// Make the request and retry if it throws an error
try {
this.log(
this.log.log(
`fetching "/api/v2/canister/${ecid.toString()}/query" with request:`,
transformedRequest,
);
Expand Down Expand Up @@ -541,7 +544,7 @@ export class HttpAgent implements Agent {
// Convert the timestamp to milliseconds
const timeStampInMs = Number(BigInt(timestamp) / BigInt(1_000_000));

this.log('watermark and timestamp', {
this.log.log('watermark and timestamp', {
waterMark: this.waterMark,
timestamp: timeStampInMs,
});
Expand Down Expand Up @@ -635,8 +638,8 @@ export class HttpAgent implements Agent {
? Principal.from(fields.effectiveCanisterId)
: Principal.from(canisterId);

this.log(`ecid ${ecid.toString()}`);
this.log(`canisterId ${canisterId.toString()}`);
this.log.log(`ecid ${ecid.toString()}`);
this.log.log(`canisterId ${canisterId.toString()}`);
const makeQuery = async () => {
const id = await (identity !== undefined ? await identity : await this._identity);
if (!id) {
Expand Down Expand Up @@ -706,7 +709,7 @@ export class HttpAgent implements Agent {
// Make query and fetch subnet keys in parallel
const [query, subnetStatus] = await Promise.all([makeQuery(), getSubnetStatus()]);

this.log('Query response:', query);
this.log.log('Query response:', query);
// Skip verification if the user has disabled it
if (!this.#verifyQuerySignatures) {
return query;
Expand Down Expand Up @@ -852,7 +855,7 @@ export class HttpAgent implements Agent {
const transformedRequest = request ?? (await this.createReadStateRequest(fields, identity));
const body = cbor.encode(transformedRequest.body);

this.log(
this.log.log(
`fetching "/api/v2/canister/${canister}/read_state" with request:`,
transformedRequest,
);
Expand Down Expand Up @@ -882,10 +885,10 @@ export class HttpAgent implements Agent {
}
const decodedResponse: ReadStateResponse = cbor.decode(await response.arrayBuffer());

this.log('Read state response:', decodedResponse);
this.log.log('Read state response:', decodedResponse);
const parsedTime = await this.parseTimeFromResponse(decodedResponse);
if (parsedTime > 0) {
this.log('Read state response time:', parsedTime);
this.log.log('Read state response time:', parsedTime);
this.#waterMark = parsedTime;
}

Expand All @@ -910,8 +913,8 @@ export class HttpAgent implements Agent {
throw new Error('Time was not found in the response or was not in its expected format.');
}
const date = decodeTime(bufFromBufLike(timeLookup));
this.log('Time from response:', date);
this.log('Time from response in milliseconds:', Number(date));
this.log.log('Time from response:', date);
this.log.log('Time from response in milliseconds:', Number(date));
return Number(date);
} else {
this.log.warn('No certificate found in response');
Expand All @@ -928,7 +931,7 @@ export class HttpAgent implements Agent {
const callTime = Date.now();
try {
if (!canisterId) {
this.log(
this.log.log(
'Syncing time with the IC. No canisterId provided, so falling back to ryjl3-tyaaa-aaaaa-aaaba-cai',
);
}
Expand All @@ -955,7 +958,7 @@ export class HttpAgent implements Agent {
}
: {};

this.log(`fetching "/api/v2/status"`);
this.log.log(`fetching "/api/v2/status"`);
const backoff = this.#backoffStrategy();
const response = await this.#requestAndRetry({
backoff,
Expand Down
3 changes: 0 additions & 3 deletions packages/agent/src/observable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ describe('Observable', () => {
observable.notify(42);
expect(observer1).toHaveBeenCalledWith(42);
expect(observer2).toHaveBeenCalledWith(42);
observable(24);
expect(observer1).toHaveBeenCalledWith(24);
expect(observer2).toHaveBeenCalledWith(24);
});

it('should notify only subscribed observers', () => {
Expand Down
9 changes: 1 addition & 8 deletions packages/agent/src/observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ import { AgentError } from './errors';

export type ObserveFunction<T> = (data: T, ...rest: unknown[]) => void;

export class Observable<T> extends Function {
export class Observable<T> {
observers: ObserveFunction<T>[];

constructor() {
super();
this.observers = [];
return new Proxy(this, {
apply: (target, _, args) => target.#call(args[0], ...args.slice(1)),
});
}

#call(message: T, ...rest: unknown[]) {
Expand Down Expand Up @@ -44,9 +40,6 @@ export type AgentLog =
export class ObservableLog extends Observable<AgentLog> {
constructor() {
super();
return new Proxy(this, {
apply: (target, _, args) => target.#call(args[0], ...args.slice(1)),
});
}
log(message: string, ...rest: unknown[]) {
this.notify({ message, level: 'info' }, ...rest);
Expand Down
Loading