feat: allow filter callback to process any type of message

This commit is contained in:
fryorcraken.eth 2022-09-21 15:48:37 +10:00
parent cb43229b3a
commit dbe832c1f8
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 6 additions and 4 deletions

View File

@ -49,7 +49,9 @@ export type FilterSubscriptionOpts = {
peerId?: PeerId;
};
export type FilterCallback = (msg: Message) => void | Promise<void>;
export type FilterCallback<T extends Message> = (
msg: T
) => void | Promise<void>;
export type UnsubscribeFunction = () => Promise<void>;
@ -62,7 +64,7 @@ export type UnsubscribeFunction = () => Promise<void>;
*/
export class WakuFilter {
pubSubTopic: string;
private subscriptions: Map<string, FilterCallback>;
private subscriptions: Map<string, FilterCallback<any>>;
private decoders: Map<
string, // content topic
Set<Decoder<any>>
@ -85,7 +87,7 @@ export class WakuFilter {
*/
async subscribe<T extends Message>(
decoders: Decoder<T>[],
callback: FilterCallback,
callback: FilterCallback<T>,
opts?: FilterSubscriptionOpts
): Promise<UnsubscribeFunction> {
const topic = opts?.pubsubTopic ?? this.pubSubTopic;
@ -209,7 +211,7 @@ export class WakuFilter {
}
}
private addCallback(requestId: string, callback: FilterCallback): void {
private addCallback(requestId: string, callback: FilterCallback<any>): void {
this.subscriptions.set(requestId, callback);
}