Skip to content

Commit

Permalink
Chore: Resolve async Promise executor function
Browse files Browse the repository at this point in the history
  • Loading branch information
vieira committed Nov 10, 2023
1 parent f08ccfa commit f6896ec
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
41 changes: 22 additions & 19 deletions bulbs/bulb.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class YeeBulb {
callback(err);
}
})
.on('get', async callback => {
.on('get', async (callback) => {
try {
const [value] = await this.getProperty(['power', 'active_mode']);
this.power = value;
Expand Down Expand Up @@ -118,12 +118,12 @@ class YeeBulb {
handle([this.responseHandler.bind(this), this.stateHandler.bind(this)])
);

this.sock.on('error', error => {
this.sock.on('error', (error) => {
this.log.error(`${this.host}: ${error.message}.`);
reject(error.code);
});

this.sock.on('close', hadError => {
this.sock.on('close', (hadError) => {
this.log.warn(`${this.host} closed. error? ${hadError}.`);
this.cmds = {};
reject(new Error(`close: error? ${hadError}`));
Expand All @@ -147,7 +147,7 @@ class YeeBulb {
}

async sendCmd(cmd) {
this.log.info(`Sending command: ${JSON.stringify(cmd)}`)
this.log.info(`Sending command: ${JSON.stringify(cmd)}`);
const { retries, timeout } = this;
cmd.id = id.next().value;
for (let i = 0; i <= retries; i += 1) {
Expand All @@ -171,22 +171,25 @@ class YeeBulb {
}

_sendCmd(cmd, duration) {
return new Promise(async (resolve, reject) => {
const _connect = async () => {
if (!this.sock || this.sock.destroyed) {
try {
await this.connect();
} catch (err) {
reject(err);
}
await this.connect();
}
const msg = JSON.stringify(cmd);
const timeout = setTimeout(() => {
reject(new Error(`${cmd.id}`));
delete this.cmds[cmd.id];
}, duration);
this.sock.write(msg + global.EOL);
this.cmds[cmd.id] = { resolve, reject, timeout };
this.log.debug(msg);
};

return new Promise((resolve, reject) => {
_connect()
.catch(reject)
.then(() => {
const msg = JSON.stringify(cmd);
const timeout = setTimeout(() => {
reject(new Error(`${cmd.id}`));
delete this.cmds[cmd.id];
}, duration);
this.sock.write(msg + global.EOL);
this.cmds[cmd.id] = { resolve, reject, timeout };
this.log.debug(msg);
});
});
}

Expand Down Expand Up @@ -215,7 +218,7 @@ class YeeBulb {
if (!('method' in message && message.method === 'props')) {
return false;
}
Object.keys(message.params).forEach(param => {
Object.keys(message.params).forEach((param) => {
this.updateStateFromProp(param, message.params[param]);
});
return true;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
},
"eslintConfig": {
"parserOptions": {
"ecmaVersion": 2019
"ecmaVersion": 2020
},
"env": {
"node": true,
Expand Down
5 changes: 2 additions & 3 deletions platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class YeePlatform {
this.sock.setBroadcast(true);
this.sock.setMulticastTTL(128);
this.sock.addMembership(this.addr);
const multicastInterface =
config && config.multicast && config.multicast.interface;
const multicastInterface = config?.multicast?.interface;
if (multicastInterface) {
this.sock.setMulticastInterface(multicastInterface);
}
Expand Down Expand Up @@ -121,7 +120,7 @@ class YeePlatform {
]);
}

if (accessory && accessory.initialized) return;
if (accessory?.initialized) return;

const mixins = [];
const limits = devices[model] || devices['default'];
Expand Down

0 comments on commit f6896ec

Please sign in to comment.