Skip to content

Commit

Permalink
chore: removes clientId set on subscriber init and assigns it only wh…
Browse files Browse the repository at this point in the history
…en used
  • Loading branch information
ganchoradkov committed Jan 29, 2025
1 parent 2817190 commit 92a6a5e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
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 @@ export class Subscriber extends ISubscriber {
super(relayer, logger);
this.relayer = relayer;
this.logger = generateChildLogger(logger, this.name);
this.clientId = ""; // assigned in init
this.clientId = ""; // assigned when used via 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 @@ export class Subscriber extends ISubscriber {
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 @@ -495,7 +494,11 @@ export class Subscriber extends ISubscriber {

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 @@ export class Subscriber extends ISubscriber {
}
}

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

0 comments on commit 92a6a5e

Please sign in to comment.