From 5687908ca065407aaedd2e62c5b58ef64a720456 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 26 May 2022 16:03:46 +1000 Subject: [PATCH] Reorder parameters To match `waku.relay.addObserver`. --- src/lib/waku_filter/index.node.spec.ts | 17 +++++------------ src/lib/waku_filter/index.ts | 25 +++++++++++-------------- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/src/lib/waku_filter/index.node.spec.ts b/src/lib/waku_filter/index.node.spec.ts index db14526a58..a9dc05e3dc 100644 --- a/src/lib/waku_filter/index.node.spec.ts +++ b/src/lib/waku_filter/index.node.spec.ts @@ -42,10 +42,7 @@ describe("Waku Filter", () => { expect(msg.contentTopic).to.eq(TestContentTopic); expect(msg.payloadAsUtf8).to.eq(messageText); }; - await waku.filter.subscribe( - { contentTopics: [TestContentTopic] }, - callback - ); + await waku.filter.subscribe(callback, [TestContentTopic]); const message = await WakuMessage.fromUtf8String( messageText, TestContentTopic @@ -65,10 +62,7 @@ describe("Waku Filter", () => { messageCount++; expect(msg.contentTopic).to.eq(TestContentTopic); }; - await waku.filter.subscribe( - { contentTopics: [TestContentTopic] }, - callback - ); + await waku.filter.subscribe(callback, [TestContentTopic]); await waku.relay.send( await WakuMessage.fromUtf8String("Filtering works!", TestContentTopic) ); @@ -89,10 +83,9 @@ describe("Waku Filter", () => { const callback = (): void => { messageCount++; }; - const unsubscribe = await waku.filter.subscribe( - { contentTopics: [TestContentTopic] }, - callback - ); + const unsubscribe = await waku.filter.subscribe(callback, [ + TestContentTopic, + ]); await waku.relay.send( await WakuMessage.fromUtf8String( "This should be received", diff --git a/src/lib/waku_filter/index.ts b/src/lib/waku_filter/index.ts index d0e501c210..883e5307cb 100644 --- a/src/lib/waku_filter/index.ts +++ b/src/lib/waku_filter/index.ts @@ -25,10 +25,6 @@ type FilterSubscriptionOpts = { * Optionally specify a PeerId for the subscription. If not included, will use a random peer. */ peerId?: PeerId; - /** - * Array of ContentTopics to subscribe to. If empty, no messages will be returned from the filter. - */ - contentTopics: string[]; }; type FilterCallback = (msg: WakuMessage) => void | Promise; @@ -56,17 +52,18 @@ export class WakuFilter { } /** - * - * @param opts The FilterSubscriptionOpts used to narrow which messages are returned, and which peer to connect to - * @param callback A function that will be called on each message returned by the filter - * @returns Unsubscribe function that can be used to end the subscription + * @param contentTopics Array of ContentTopics to subscribe to. If empty, no messages will be returned from the filter. + * @param callback A function that will be called on each message returned by the filter. + * @param opts The FilterSubscriptionOpts used to narrow which messages are returned, and which peer to connect to. + * @returns Unsubscribe function that can be used to end the subscription. */ async subscribe( - opts: FilterSubscriptionOpts, - callback: FilterCallback + callback: FilterCallback, + contentTopics: string[], + opts?: FilterSubscriptionOpts ): Promise { - const topic = opts.pubsubTopic || DefaultPubSubTopic; - const contentFilters = opts.contentTopics.map((contentTopic) => ({ + const topic = opts?.pubsubTopic || DefaultPubSubTopic; + const contentFilters = contentTopics.map((contentTopic) => ({ contentTopic, })); const request = FilterRPC.createRequest( @@ -76,7 +73,7 @@ export class WakuFilter { true ); - const peer = await this.getPeer(opts.peerId); + const peer = await this.getPeer(opts?.peerId); const stream = await this.newStream(peer); try { @@ -86,7 +83,7 @@ export class WakuFilter { "Error subscribing to peer ", peer.id.toB58String(), "for content topics", - opts.contentTopics, + contentTopics, ": ", e );