mirror of https://github.com/waku-org/js-waku.git
parent
4cf3d3ffd8
commit
5687908ca0
|
@ -42,10 +42,7 @@ describe("Waku Filter", () => {
|
||||||
expect(msg.contentTopic).to.eq(TestContentTopic);
|
expect(msg.contentTopic).to.eq(TestContentTopic);
|
||||||
expect(msg.payloadAsUtf8).to.eq(messageText);
|
expect(msg.payloadAsUtf8).to.eq(messageText);
|
||||||
};
|
};
|
||||||
await waku.filter.subscribe(
|
await waku.filter.subscribe(callback, [TestContentTopic]);
|
||||||
{ contentTopics: [TestContentTopic] },
|
|
||||||
callback
|
|
||||||
);
|
|
||||||
const message = await WakuMessage.fromUtf8String(
|
const message = await WakuMessage.fromUtf8String(
|
||||||
messageText,
|
messageText,
|
||||||
TestContentTopic
|
TestContentTopic
|
||||||
|
@ -65,10 +62,7 @@ describe("Waku Filter", () => {
|
||||||
messageCount++;
|
messageCount++;
|
||||||
expect(msg.contentTopic).to.eq(TestContentTopic);
|
expect(msg.contentTopic).to.eq(TestContentTopic);
|
||||||
};
|
};
|
||||||
await waku.filter.subscribe(
|
await waku.filter.subscribe(callback, [TestContentTopic]);
|
||||||
{ contentTopics: [TestContentTopic] },
|
|
||||||
callback
|
|
||||||
);
|
|
||||||
await waku.relay.send(
|
await waku.relay.send(
|
||||||
await WakuMessage.fromUtf8String("Filtering works!", TestContentTopic)
|
await WakuMessage.fromUtf8String("Filtering works!", TestContentTopic)
|
||||||
);
|
);
|
||||||
|
@ -89,10 +83,9 @@ describe("Waku Filter", () => {
|
||||||
const callback = (): void => {
|
const callback = (): void => {
|
||||||
messageCount++;
|
messageCount++;
|
||||||
};
|
};
|
||||||
const unsubscribe = await waku.filter.subscribe(
|
const unsubscribe = await waku.filter.subscribe(callback, [
|
||||||
{ contentTopics: [TestContentTopic] },
|
TestContentTopic,
|
||||||
callback
|
]);
|
||||||
);
|
|
||||||
await waku.relay.send(
|
await waku.relay.send(
|
||||||
await WakuMessage.fromUtf8String(
|
await WakuMessage.fromUtf8String(
|
||||||
"This should be received",
|
"This should be received",
|
||||||
|
|
|
@ -25,10 +25,6 @@ type FilterSubscriptionOpts = {
|
||||||
* Optionally specify a PeerId for the subscription. If not included, will use a random peer.
|
* Optionally specify a PeerId for the subscription. If not included, will use a random peer.
|
||||||
*/
|
*/
|
||||||
peerId?: PeerId;
|
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<void>;
|
type FilterCallback = (msg: WakuMessage) => void | Promise<void>;
|
||||||
|
@ -56,17 +52,18 @@ export class WakuFilter {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* @param contentTopics Array of ContentTopics to subscribe to. If empty, no messages will be returned from the filter.
|
||||||
* @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.
|
||||||
* @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
|
* @returns Unsubscribe function that can be used to end the subscription.
|
||||||
*/
|
*/
|
||||||
async subscribe(
|
async subscribe(
|
||||||
opts: FilterSubscriptionOpts,
|
callback: FilterCallback,
|
||||||
callback: FilterCallback
|
contentTopics: string[],
|
||||||
|
opts?: FilterSubscriptionOpts
|
||||||
): Promise<UnsubscribeFunction> {
|
): Promise<UnsubscribeFunction> {
|
||||||
const topic = opts.pubsubTopic || DefaultPubSubTopic;
|
const topic = opts?.pubsubTopic || DefaultPubSubTopic;
|
||||||
const contentFilters = opts.contentTopics.map((contentTopic) => ({
|
const contentFilters = contentTopics.map((contentTopic) => ({
|
||||||
contentTopic,
|
contentTopic,
|
||||||
}));
|
}));
|
||||||
const request = FilterRPC.createRequest(
|
const request = FilterRPC.createRequest(
|
||||||
|
@ -76,7 +73,7 @@ export class WakuFilter {
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
const peer = await this.getPeer(opts.peerId);
|
const peer = await this.getPeer(opts?.peerId);
|
||||||
const stream = await this.newStream(peer);
|
const stream = await this.newStream(peer);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -86,7 +83,7 @@ export class WakuFilter {
|
||||||
"Error subscribing to peer ",
|
"Error subscribing to peer ",
|
||||||
peer.id.toB58String(),
|
peer.id.toB58String(),
|
||||||
"for content topics",
|
"for content topics",
|
||||||
opts.contentTopics,
|
contentTopics,
|
||||||
": ",
|
": ",
|
||||||
e
|
e
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue