Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/npm_and_yarn/typescript-eslint/…
Browse files Browse the repository at this point in the history
…parser-6.14.0
  • Loading branch information
bratanon authored Dec 16, 2023
2 parents 17b6690 + ffb3d46 commit ee690c0
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 18 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed
- More clear log messages for the base unit.
- Events should be assigned before calling the handler.
- Client `close()` now resolves immediately if the socket already is closed.
6 changes: 3 additions & 3 deletions src/BaseUnit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ export class BaseUnit {
}

private async handleConnectionMade() {
logger.debug('Connected successfully');
logger.debug('Connected to LifeSOS alarm');
this.isConnected = true;

// Get initial state info and find devices
Expand All @@ -378,9 +378,9 @@ export class BaseUnit {

private handleConnectionClose(hadError: boolean) {
if (hadError) {
logger.error('Connection closed with errors.');
logger.error('Connection closed to LifeSOS alarm with errors.');
} else {
logger.info('Connection closed.');
logger.info('Connection closed to LifeSOS alarm');
}

this.isConnected = false;
Expand Down
16 changes: 12 additions & 4 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,36 @@ class Client extends Protocol {
*/
async open(): Promise<void> {
return new Promise((resolve, reject) => {
this.socket.connect({ port: this.port, host: this.host });

this.socket.once('connect', () => {
resolve();
});

this.socket.once('error', (err) => {
reject(err);
});

this.socket.connect({ port: this.port, host: this.host });
});
}

/**
* Close connection to the LifeSOS ethernet interface.
*/
async close(): Promise<void> {
return new Promise((resolve) => {
this.socket.end();
if (this.socket.closed) {
return Promise.resolve();
}

return new Promise((resolve, reject) => {
this.socket.once('close', () => {
resolve();
});

this.socket.once('error', (error: Error) => {
reject(error);
});

this.socket.end();
});
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/Protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export type State = {
* Base class containing common functionality for the Client implementation.
*/
export abstract class Protocol {
// static ENSURE_ALIVE_SECS = 30;
static EXECUTE_TIMEOUT_SECS = 8;

/**
Expand Down Expand Up @@ -111,8 +110,6 @@ export abstract class Protocol {
onResponse: ((response: Response) => void) | undefined;

protected constructor() {
// @TODO: Fix keep alive?
// this._socket = new Socket().setKeepAlive(true, Protocol.ENSURE_ALIVE_SECS * 1000);
this.socket = new Socket();

// Handle data received
Expand Down Expand Up @@ -386,9 +383,7 @@ export abstract class Protocol {

this.executing[command.name] = state;

this._send(command, password);

return new Promise<R>((resolve, reject) => {
const response = new Promise<R>((resolve, reject) => {
const timer = setTimeout(() => {
reject(new CommandTimeoutError(command));
}, timeout * 1000);
Expand All @@ -401,6 +396,10 @@ export abstract class Protocol {
logger.debug(`Deleting command: ${command.name}`);
delete this.executing[command.name];
});

this._send(command, password);

return response;
}

private _send(command: Command, password: string) {
Expand Down
29 changes: 24 additions & 5 deletions tests/Client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,30 @@ describe('Client', () => {
});
});

test('close', async () => {
client.socket.end = jest.fn();
const closePromise = client.close();
client.socket.emit('close');
describe('close', () => {
test('success', async () => {
client.socket.end = jest.fn();
const closePromise = client.close();
client.socket.emit('close');

await expect(closePromise).resolves.toBeUndefined();
});

test('failure', async () => {
const closePromise = client.close();
client.socket.emit('error');

await expect(closePromise).resolves.toBeUndefined();
await expect(closePromise).rejects.toBeUndefined();
});

test('close when socket already is closed', async () => {
client.socket.end = jest.fn();
client.socket.destroy();

const closePromise = client.close();
await expect(closePromise).resolves.toBeUndefined();
expect(client.socket.end).not.toHaveBeenCalled();
});
});

});

0 comments on commit ee690c0

Please sign in to comment.