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

chore: removes redundant clientId() call during subscriber init #5623

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 16 additions & 6 deletions packages/core/src/controllers/subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,13 @@
super(relayer, logger);
this.relayer = relayer;
this.logger = generateChildLogger(logger, this.name);
this.clientId = ""; // assigned in init
this.clientId = ""; // assigned when calling this.getClientId()
}

public init: ISubscriber["init"] = async () => {
if (!this.initialized) {
this.logger.trace(`Initialized`);
this.registerEventListeners();
this.clientId = await this.relayer.core.crypto.getClientId();
await this.restore();
}
this.initialized = true;
Expand Down Expand Up @@ -240,7 +239,7 @@
this.logger.trace({ type: "payload", direction: "outgoing", request });
const shouldThrow = opts?.internal?.throwOnFailedPublish;
try {
const subId = this.getSubscriptionId(topic);
const subId = await this.getSubscriptionId(topic);
// in link mode, allow the app to update its network state (i.e. active airplane mode) with small delay before attempting to subscribe
if (opts?.transportType === TRANSPORT_TYPES.link_mode) {
setTimeout(() => {
Expand Down Expand Up @@ -302,7 +301,7 @@
private async rpcBatchSubscribe(subscriptions: SubscriberTypes.Params[]) {
if (!subscriptions.length) return;
const relay = subscriptions[0].relay;
const api = getRelayProtocolApi(relay!.protocol);

Check warning on line 304 in packages/core/src/controllers/subscriber.ts

View workflow job for this annotation

GitHub Actions / code_style (lint)

Forbidden non-null assertion
const request: RequestArguments<RelayJsonRpc.BatchSubscribeParams> = {
method: api.batchSubscribe,
params: {
Expand Down Expand Up @@ -331,7 +330,7 @@
private async rpcBatchFetchMessages(subscriptions: SubscriberTypes.Params[]) {
if (!subscriptions.length) return;
const relay = subscriptions[0].relay;
const api = getRelayProtocolApi(relay!.protocol);

Check warning on line 333 in packages/core/src/controllers/subscriber.ts

View workflow job for this annotation

GitHub Actions / code_style (lint)

Forbidden non-null assertion
const request: RequestArguments<RelayJsonRpc.BatchFetchMessagesParams> = {
method: api.batchFetchMessages,
params: {
Expand Down Expand Up @@ -495,7 +494,11 @@

await this.rpcBatchSubscribe(subscriptions);
this.onBatchSubscribe(
subscriptions.map((s) => ({ ...s, id: this.getSubscriptionId(s.topic) })),
await Promise.all(
subscriptions.map(async (s) => {
return { ...s, id: await this.getSubscriptionId(s.topic) };
}),
),
);
}

Expand Down Expand Up @@ -562,7 +565,14 @@
}
}

private getSubscriptionId(topic: string) {
return hashMessage(topic + this.clientId);
private async getClientId() {
if (!this.clientId) {
this.clientId = await this.relayer.core.crypto.getClientId();
}
return this.clientId;
}

private async getSubscriptionId(topic: string) {
return hashMessage(topic + (await this.getClientId()));
}
}
4 changes: 4 additions & 0 deletions packages/core/test/subscriber.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ describe("Subscriber", () => {

describe("init", () => {
it("registers event listeners", async () => {
expect(subscriber.clientId).to.equal("");

const topic = generateRandomBytes32();
const emitSpy = Sinon.spy();
subscriber.events.emit = emitSpy;
Expand All @@ -114,6 +116,8 @@ describe("Subscriber", () => {
relayer.provider.events.emit(RELAYER_PROVIDER_EVENTS.disconnect);
expect(subscriber.subscriptions.size).to.equal(0);
expect(subscriber.topics.length).to.equal(0);

expect(subscriber.clientId).to.not.equal("");
});
});

Expand Down
Loading