From 33ec1f528cc6daac61d53a5e34aea9011e0eecd9 Mon Sep 17 00:00:00 2001 From: AlejandroHerr Date: Sat, 15 Dec 2018 01:28:20 +0100 Subject: [PATCH] perf(typescript): Improve type definitions --- src/Bus/Bus.ts | 31 +++---------------------------- src/Bus/BusError.ts | 6 ++++-- src/Bus/BusInterface.ts | 29 +++++++++++++++++++++++++++++ src/Bus/index.ts | 3 ++- src/Device/Device.ts | 23 +++-------------------- src/Device/DeviceError.ts | 6 ++++-- src/Device/DeviceInterface.ts | 20 ++++++++++++++++++++ src/Device/index.ts | 3 ++- src/createI2cBusMock/index.ts | 9 ++++----- src/index.ts | 6 +++--- src/types.ts | 26 +++++++++++++------------- 11 files changed, 87 insertions(+), 75 deletions(-) create mode 100644 src/Bus/BusInterface.ts create mode 100644 src/Device/DeviceInterface.ts diff --git a/src/Bus/Bus.ts b/src/Bus/Bus.ts index ab6096d..59258f7 100644 --- a/src/Bus/Bus.ts +++ b/src/Bus/Bus.ts @@ -1,37 +1,12 @@ import { promisifyAll } from 'bluebird'; -import { I2cBusFuncs, open as openI2cBus } from 'i2c-bus'; +import { open as openI2cBus } from 'i2c-bus'; import { I2cBusPromised } from '../types'; import BusError from './BusError'; +import BusInterface from './BusInterface'; -interface Bus { - readonly busNumber: number; - readonly i2cBus: I2cBusPromised | null; - readonly isOpen: boolean; - - open: () => Promise; - close: () => Promise; - - i2cFuncs: () => Promise; - scan: () => Promise; - - i2cRead: (address: number, length: number, buffer: Buffer) => Promise; - i2cWrite: (address: number, length: number, buffer: Buffer) => Promise; - - receiveByte: (address: number) => Promise; - sendByte: (address: number, byte: number) => Promise; - - readByte: (address: number, command: number) => Promise; - readI2cBlock: (address: number, command: number, length: number, buffer: Buffer) => Promise; - readWord: (address: number, command: number) => Promise; - - writeByte: (address: number, command: number, byte: number) => Promise; - writeWord: (address: number, command: number, word: number) => Promise; - writeI2cBlock: (address: number, command: number, length: number, buffer: Buffer) => Promise; -} - -const Bus = ({ busNumber = 1, openBus = openI2cBus } = {}): Bus => { +const Bus = ({ busNumber = 1, openBus = openI2cBus } = {}): BusInterface => { let i2cBus: I2cBusPromised | null = null; let isOpen: boolean = false; diff --git a/src/Bus/BusError.ts b/src/Bus/BusError.ts index 652d10d..3093817 100644 --- a/src/Bus/BusError.ts +++ b/src/Bus/BusError.ts @@ -1,8 +1,10 @@ export default class BusError extends Error { public busNumber: number; - constructor(msg: string, busNumber: number) { - super(msg); + constructor(message: string, busNumber: number) { + super(message); + + this.name = this.constructor.name; Error.captureStackTrace(this, BusError); diff --git a/src/Bus/BusInterface.ts b/src/Bus/BusInterface.ts new file mode 100644 index 0000000..cdee7b3 --- /dev/null +++ b/src/Bus/BusInterface.ts @@ -0,0 +1,29 @@ +import { I2cBusFuncs } from 'i2c-bus'; + +import { I2cBusPromised } from '../types'; + +export default interface BusInterface { + readonly busNumber: number; + readonly i2cBus: I2cBusPromised | null; + readonly isOpen: boolean; + + open(): Promise; + close(): Promise; + + i2cFuncs(): Promise; + scan(): Promise; + + i2cRead(address: number, length: number, buffer: Buffer): Promise; + i2cWrite(address: number, length: number, buffer: Buffer): Promise; + + receiveByte(address: number): Promise; + sendByte(address: number, byte: number): Promise; + + readByte(address: number, command: number): Promise; + readI2cBlock(address: number, command: number, length: number, buffer: Buffer): Promise; + readWord(address: number, command: number): Promise; + + writeByte(address: number, command: number, byte: number): Promise; + writeWord(address: number, command: number, word: number): Promise; + writeI2cBlock(address: number, command: number, length: number, buffer: Buffer): Promise; +} diff --git a/src/Bus/index.ts b/src/Bus/index.ts index 61f5bb2..b8e7289 100644 --- a/src/Bus/index.ts +++ b/src/Bus/index.ts @@ -1,4 +1,5 @@ import Bus from './Bus'; import BusError from './BusError'; +import BusInterface from './BusInterface'; -export { Bus, BusError }; +export { Bus, BusError, BusInterface }; diff --git a/src/Device/Device.ts b/src/Device/Device.ts index 8f0a6f5..6542076 100644 --- a/src/Device/Device.ts +++ b/src/Device/Device.ts @@ -1,25 +1,8 @@ -import { Bus } from '../Bus'; +import BusInterface from '../Bus/BusInterface'; -export interface Device { - readonly address: number; - readonly bus: Bus; +import DeviceInterface from './DeviceInterface'; - i2cRead: (length: number, buffer: Buffer) => Promise; - i2cWrite: (length: number, buffer: Buffer) => Promise; - - receiveByte: () => Promise; - sendByte: (byte: number) => Promise; - - readByte: (command: number) => Promise; - readI2cBlock: (command: number, length: number, buffer: Buffer) => Promise; - readWord: (command: number) => Promise; - - writeByte: (command: number, byte: number) => Promise; - writeWord: (command: number, word: number) => Promise; - writeI2cBlock: (command: number, length: number, buffer: Buffer) => Promise; -} - -const Device = ({ address, bus }: { address: number; bus: Bus }): Device => ({ +const Device = ({ address, bus }: { address: number; bus: BusInterface }): DeviceInterface => ({ get address() { return address; }, diff --git a/src/Device/DeviceError.ts b/src/Device/DeviceError.ts index 1cee935..dda09a7 100644 --- a/src/Device/DeviceError.ts +++ b/src/Device/DeviceError.ts @@ -2,8 +2,10 @@ export default class DeviceError extends Error { public busNumber: number; public deviceAddress: number; - constructor(msg: string, busNumber: number, deviceAddress: number) { - super(msg); + constructor(message: string, busNumber: number, deviceAddress: number) { + super(message); + + this.name = this.constructor.name; Error.captureStackTrace(this, DeviceError); diff --git a/src/Device/DeviceInterface.ts b/src/Device/DeviceInterface.ts new file mode 100644 index 0000000..fceb82f --- /dev/null +++ b/src/Device/DeviceInterface.ts @@ -0,0 +1,20 @@ +import BusInterface from '../Bus/BusInterface'; + +export default interface DeviceInterface { + readonly address: number; + readonly bus: BusInterface; + + i2cRead(length: number, buffer: Buffer): Promise; + i2cWrite(length: number, buffer: Buffer): Promise; + + receiveByte(): Promise; + sendByte(byte: number): Promise; + + readByte(command: number): Promise; + readI2cBlock(command: number, length: number, buffer: Buffer): Promise; + readWord(command: number): Promise; + + writeByte(command: number, byte: number): Promise; + writeWord(command: number, word: number): Promise; + writeI2cBlock(command: number, length: number, buffer: Buffer): Promise; +} diff --git a/src/Device/index.ts b/src/Device/index.ts index a3c2fdc..04f4875 100644 --- a/src/Device/index.ts +++ b/src/Device/index.ts @@ -1,4 +1,5 @@ import Device from './Device'; import DeviceError from './DeviceError'; +import DeviceInterface from './DeviceInterface'; -export { Device, DeviceError }; +export { Device, DeviceError, DeviceInterface }; diff --git a/src/createI2cBusMock/index.ts b/src/createI2cBusMock/index.ts index fa8aa74..65c3a28 100644 --- a/src/createI2cBusMock/index.ts +++ b/src/createI2cBusMock/index.ts @@ -1,9 +1,5 @@ import { BufferCallback, CompletionCallback, I2cBus, I2cBusFuncs, ResultCallback } from 'i2c-bus'; -interface Devices { - [address: number]: Buffer; -} - const defaultI2cBusFuncs = { i2c: false, tenBitAddr: false, @@ -28,7 +24,10 @@ const defaultI2cBusFuncs = { export default ({ devices = {}, i2cBusFuncs = defaultI2cBusFuncs, -}: { devices?: Devices; i2cBusFuncs?: I2cBusFuncs } = {}) => (_: number, openCallback: CompletionCallback): I2cBus => { +}: { + devices?: { [address: number]: Buffer }; + i2cBusFuncs?: I2cBusFuncs; +} = {}) => (_: number, openCallback: CompletionCallback): I2cBus => { openCallback(null); return { diff --git a/src/index.ts b/src/index.ts index 5a778aa..74b24a4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ -import { Bus, BusError } from './Bus'; -import { Device, DeviceError } from './Device'; +import { Bus, BusError, BusInterface } from './Bus'; +import { Device, DeviceError, DeviceInterface } from './Device'; import createI2cBusMock from './createI2cBusMock'; -export { Bus, BusError, Device, DeviceError, createI2cBusMock }; +export { Bus, BusError, BusInterface, Device, DeviceError, DeviceInterface, createI2cBusMock }; diff --git a/src/types.ts b/src/types.ts index 7ccfe16..cbf7317 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2,22 +2,22 @@ import { I2cBus, I2cBusFuncs } from 'i2c-bus'; /* tslint:disable-next-line:interface-name */ export interface I2cBusPromised extends I2cBus { - closeAsync: () => Promise; + closeAsync(): Promise; - i2cFuncsAsync: () => Promise; - scanAsync: () => Promise; + i2cFuncsAsync(): Promise; + scanAsync(): Promise; - i2cReadAsync: (address: number, length: number, buffer: Buffer) => Promise; - i2cWriteAsync: (address: number, length: number, buffer: Buffer) => Promise; + i2cReadAsync(address: number, length: number, buffer: Buffer): Promise; + i2cWriteAsync(address: number, length: number, buffer: Buffer): Promise; - receiveByteAsync: (address: number) => Promise; - sendByteAsync: (address: number, byte: number) => Promise; + receiveByteAsync(address: number): Promise; + sendByteAsync(address: number, byte: number): Promise; - readByteAsync: (address: number, command: number) => Promise; - readWordAsync: (address: number, command: number) => Promise; - readI2cBlockAsync: (address: number, command: number, length: number, buffer: Buffer) => Promise; + readByteAsync(address: number, command: number): Promise; + readWordAsync(address: number, command: number): Promise; + readI2cBlockAsync(address: number, command: number, length: number, buffer: Buffer): Promise; - writeByteAsync: (address: number, command: number, byte: number) => Promise; - writeWordAsync: (address: number, command: number, word: number) => Promise; - writeI2cBlockAsync: (address: number, command: number, length: number, buffer: Buffer) => Promise; + writeByteAsync(address: number, command: number, byte: number): Promise; + writeWordAsync(address: number, command: number, word: number): Promise; + writeI2cBlockAsync(address: number, command: number, length: number, buffer: Buffer): Promise; }