Skip to content

Commit

Permalink
* make emitter.notify boolean return just like emitter.emit
Browse files Browse the repository at this point in the history
  • Loading branch information
balazskreith committed Aug 16, 2024
1 parent b432124 commit 560dc3e
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/collections/HamokEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,12 @@ export class HamokEmitter<T extends HamokEmitterEventMap> {
return result;
}

public notify<K extends keyof T>(event: K, ...args: T[K]): void {
public notify<K extends keyof T>(event: K, ...args: T[K]): boolean {
if (this._closed) throw new Error('Cannot publish on a closed emitter');

const remotePeerIds = this._subscriptions.get(event);
const entry = [ event as string, this.payloadsCodec?.get(event)?.encode(...args) ?? JSON.stringify(args) ] as [string, string];
let delivered = false;

for (const remotePeerId of remotePeerIds ?? []) {
if (remotePeerId === this.connection.grid.localPeerId) continue;
Expand All @@ -231,11 +232,14 @@ export class HamokEmitter<T extends HamokEmitterEventMap> {
new Map([ entry ]),
remotePeerId
);
delivered = true;
}

if (remotePeerIds?.has(this.connection.grid.localPeerId)) {
this._emitter.emit(event as string, ...args);
delivered ||= this._emitter.emit(event as string, ...args);
}

return delivered;
}

public export(): HamokEmitterSnapshot {
Expand Down

0 comments on commit 560dc3e

Please sign in to comment.