Skip to content

Commit

Permalink
fix: addEventListener & move for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
danisharora099 committed Nov 3, 2023
1 parent e0852f1 commit 9cd3c57
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions packages/core/src/lib/filter/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,6 @@ export class Subscription extends EventEmitter<SubscriptionEventMap> {
this.processSubscribeResponse(response, decoders);
}

addEventListener<K extends string | number>(
type: K,
listener: EventHandler<SubscriptionEventMap[K]> | null,
options?: boolean | AddEventListenerOptions | undefined
): void {
if (type !== "string")
throw new Error(
"Invalid event type. Expected content topic which should be a string."
);

const decoder = this.decoders.get(type);
if (!decoder) throw new Error("No decoder found for content topic " + type);
if (decoder.pubsubTopic !== this.pubsubTopic)
throw new Error(
`Invalid decoder received. Expected decoder for pubsub topic ${this.pubsubTopic} but received decoder for pubsub topic ${decoder.pubsubTopic}`
);

super.addEventListener(type, listener, options);
}

async unsubscribe(contentTopics: ContentTopic[]): Promise<void> {
const stream = await this.newStream(this.peer);
const unsubscribeRequest = FilterSubscribeRpc.createUnsubscribeRequest(
Expand Down Expand Up @@ -193,6 +173,27 @@ export class Subscription extends EventEmitter<SubscriptionEventMap> {
}
}

addEventListener<K extends string | number>(
contentTopic: K,
listener: EventHandler<SubscriptionEventMap[K]> | null,
options?: boolean | AddEventListenerOptions | undefined
): void {
if (typeof contentTopic !== "string")
throw new Error(
`Invalid event type. Expected content topic which should be a string but received ${contentTopic} of the type ${typeof contentTopic}`
);

const decoder = this.decoders.get(contentTopic);
if (!decoder)
throw new Error("No decoder found for content topic " + contentTopic);
if (decoder.pubsubTopic !== this.pubsubTopic)
throw new Error(
`Invalid decoder received. Expected decoder for pubsub topic ${this.pubsubTopic} but received decoder for pubsub topic ${decoder.pubsubTopic}`
);

super.addEventListener(contentTopic, listener, options);
}

async processMessage(message: WakuMessage): Promise<void> {
const { contentTopic } = message;
if (!contentTopic) {
Expand Down

0 comments on commit 9cd3c57

Please sign in to comment.