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

View File

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