Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ handle resend errors #62

Merged
merged 5 commits into from
Aug 6, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/sender.ts
lukas-runge marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Socket, createSocket } from 'dgram';
import { EventEmitter } from 'events';
import { multicastGroup } from './util';
import { Packet, Options } from './packet';

Expand Down Expand Up @@ -49,7 +50,7 @@ export namespace Sender {
}
}

export class Sender {
export class Sender extends EventEmitter {
private socket: Socket;

private readonly port: Sender.Props['port'];
Expand All @@ -66,6 +67,8 @@ export class Sender {

private sequence = 0;

private resendStatus = false;
lukas-runge marked this conversation as resolved.
Show resolved Hide resolved

#loopId: NodeJS.Timeout | undefined;

/**
Expand All @@ -84,6 +87,7 @@ export class Sender {
iface,
useUnicastDestination,
}: Sender.Props) {
super();
this.port = port;
this.universe = universe;
this.#destinationIp = useUnicastDestination || multicastGroup(universe);
Expand Down Expand Up @@ -123,7 +127,23 @@ export class Sender {
}

private reSend() {
if (this.#latestPacketOptions) this.send(this.#latestPacketOptions);
if (this.#latestPacketOptions) {
this.send(this.#latestPacketOptions)
.then(() => {
this.updateResendStatus(true);
})
.catch((err) => {
this.updateResendStatus(false);
this.emit('error', err);
});
}
}

private updateResendStatus(success: boolean) {
if (success !== this.resendStatus) {
this.resendStatus = success;
this.emit('changedResendStatus', success);
lukas-runge marked this conversation as resolved.
Show resolved Hide resolved
}
}

public close(): this {
Expand Down
Loading