Use Set to facilitate removal of observers

This commit is contained in:
Franck Royer 2021-06-16 14:26:05 +10:00
parent 9244674cf5
commit af1e97fafe
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 5 additions and 5 deletions

View File

@ -65,7 +65,7 @@ export class WakuRelay extends Gossipsub implements Pubsub {
* Observers under key "" are always called.
*/
public observers: {
[contentTopic: string]: Array<(message: WakuMessage) => void>;
[contentTopic: string]: Set<(message: WakuMessage) => void>;
};
constructor(
@ -131,15 +131,15 @@ export class WakuRelay extends Gossipsub implements Pubsub {
): void {
if (contentTopics.length === 0) {
if (!this.observers['']) {
this.observers[''] = [];
this.observers[''] = new Set();
}
this.observers[''].push(callback);
this.observers[''].add(callback);
} else {
contentTopics.forEach((contentTopic) => {
if (!this.observers[contentTopic]) {
this.observers[contentTopic] = [];
this.observers[contentTopic] = new Set();
}
this.observers[contentTopic].push(callback);
this.observers[contentTopic].add(callback);
});
}
}