Skip to content

Commit

Permalink
don't report connection still alive after close without Client.discon…
Browse files Browse the repository at this point in the history
…nect

previously, due to an oversight, closing the connection through a means
other than calling the function Client.disconnect (i.e. computer
suspending, server dying) would cause AP.js's state machine to still report
that it was connected to the server. This fixes that by moving much of
the deinitialization logic in Client.disconnect to a new disconnection
listener which is established in Client.#finalizeConnection (so that it
only gets called when the stuff we want to deinitialize is definitely
present.
  • Loading branch information
CodeTriangle committed Aug 10, 2024
1 parent f2a4b2e commit 0057f63
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ export class Client<TSlotData = SlotData> {
}

#finalizeConnection(info: ConnectionInformation): Promise<ConnectedPacket> {
if (this.#socket != undefined) {
this.#socket.onclose = this.#closeSocket.bind(this);
}

const version = info.version ?? MINIMUM_SUPPORTED_AP_VERSION;

return new Promise<ConnectedPacket>((resolve, reject) => {
Expand Down Expand Up @@ -192,6 +196,19 @@ export class Client<TSlotData = SlotData> {
});
}

#closeSocket(): void {
this.#socket = undefined;
this.#status = CONNECTION_STATUS.DISCONNECTED;
this.#emitter.removeAllListeners();

// Reinitialize our Managers.
this.#dataManager = new DataManager(this);
this.#hintManager = new HintsManager(this);
this.#itemsManager = new ItemsManager(this);
this.#locationsManager = new LocationsManager(this);
this.#playersManager = new PlayersManager(this);
}

/**
* Send a list of raw packets to the Archipelago server in the order they are listed as arguments.
*
Expand Down Expand Up @@ -223,16 +240,6 @@ export class Client<TSlotData = SlotData> {
*/
public disconnect(): void {
this.#socket?.close();
this.#socket = undefined;
this.#status = CONNECTION_STATUS.DISCONNECTED;
this.#emitter.removeAllListeners();

// Reinitialize our Managers.
this.#dataManager = new DataManager(this);
this.#hintManager = new HintsManager(this);
this.#itemsManager = new ItemsManager(this);
this.#locationsManager = new LocationsManager(this);
this.#playersManager = new PlayersManager(this);
}

public addListener(event: "Bounced", listener: (packet: BouncedPacket) => void): void;
Expand Down

0 comments on commit 0057f63

Please sign in to comment.