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

v2: Drop all the helpers #16

Merged
merged 2 commits into from
May 18, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"no-param-reassign": 0,
"no-underscore-dangle": 0,
"func-names": 0,
"no-multi-assign": 0
"no-multi-assign": 0,
"prefer-destructuring": 0
}
}
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
language: node_js
script:
- npm run lint
- npm run test
2 changes: 1 addition & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default function subscribeConfig(conn, configChanged) {
initPromise.then(
([unsubComp, unsubServReg, unsubServRem]) => {
configUnsub = () => {
removeEventListener('ready', fetchAll);
conn.removeEventListener('ready', fetchAll);
unsubComp();
unsubServReg();
unsubServRem();
Expand Down
80 changes: 39 additions & 41 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,34 @@ import {
ERR_CANNOT_CONNECT,
ERR_INVALID_AUTH,
ERR_CONNECTION_LOST,

MSG_TYPE_AUTH_REQUIRED,
MSG_TYPE_AUTH_INVALID,
MSG_TYPE_AUTH_OK,

MSG_TYPE_EVENT,
MSG_TYPE_RESULT,
MSG_TYPE_PONG,
} from './const';

function getSocket(url, options) {
if (__DEV__) {
/* eslint-disable no-console */
// eslint-disable-next-line
console.log('[Auth phase] Initializing', url);
/* eslint-enable no-console */
}

function connect(triesLeft, promResolve, promReject) {
if (__DEV__) {
/* eslint-disable no-console */
// eslint-disable-next-line
console.log('[Auth Phase] New connection', url);
/* eslint-enable no-console */
}

const socket = new WebSocket(url);

// If invalid auth, we will not try to reconnect.
let invalidAuth = false;

const closeMessage = (evt) => {
const closeMessage = () => {
// If we are in error handler make sure close handler doesn't also fire.
socket.removeEventListener('close', closeMessage);

Expand All @@ -49,12 +55,11 @@ function getSocket(url, options) {
const message = JSON.parse(event.data);

if (__DEV__) {
/* eslint-disable no-console */
// eslint-disable-next-line
console.log('[Auth phase] Received', message);
/* eslint-enable no-console */
}
switch (message.type) {
case 'auth_required':
case MSG_TYPE_AUTH_REQUIRED:
if (options.authToken) {
socket.send(JSON.stringify(messages.auth(options.authToken)));
} else if (options.accessToken) {
Expand All @@ -65,12 +70,12 @@ function getSocket(url, options) {
}
break;

case 'auth_invalid':
case MSG_TYPE_AUTH_INVALID:
invalidAuth = true;
socket.close();
break;

case 'auth_ok':
case MSG_TYPE_AUTH_OK:
socket.removeEventListener('message', handleMessage);
socket.removeEventListener('close', closeMessage);
socket.removeEventListener('error', closeMessage);
Expand All @@ -79,9 +84,8 @@ function getSocket(url, options) {

default:
if (__DEV__) {
/* eslint-disable no-console */
// eslint-disable-next-line
console.warn('[Auth phase] Unhandled message', message);
/* eslint-enable no-console */
}
}
};
Expand Down Expand Up @@ -201,22 +205,20 @@ class Connection {
// eventCallback will be called when a new event fires
// Returned promise resolves to an unsubscribe function.
subscribeEvents(eventCallback, eventType) {
return this.sendMessagePromise(messages.subscribeEvents(eventType)).then(
(resultMessage) => {
// We store unsubscribe on info object. That way we can overwrite it in case
// we get disconnected and we have to subscribe again.
const info = {
eventCallback,
eventType,
unsubscribe: () => this.sendMessagePromise(messages.unsubscribeEvents(resultMessage.id))
.then(() => { delete this.commands[resultMessage.id]; }),
};

this.commands[resultMessage.id] = info;

return () => info.unsubscribe();
}
);
return this.sendMessagePromise(messages.subscribeEvents(eventType)).then((resultMessage) => {
// We store unsubscribe on info object. That way we can overwrite it in case
// we get disconnected and we have to subscribe again.
const info = {
eventCallback,
eventType,
unsubscribe: () => this.sendMessagePromise(messages.unsubscribeEvents(resultMessage.id))
.then(() => { delete this.commands[resultMessage.id]; }),
};

this.commands[resultMessage.id] = info;

return () => info.unsubscribe();
});
}

ping() {
Expand All @@ -225,9 +227,8 @@ class Connection {

sendMessage(message) {
if (__DEV__) {
/* eslint-disable no-console */
// eslint-disable-next-line
console.log('Sending', message);
/* eslint-enable no-console */
}

this.socket.send(JSON.stringify(message));
Expand All @@ -247,17 +248,16 @@ class Connection {
const message = JSON.parse(event.data);

if (__DEV__) {
/* eslint-disable no-console */
// eslint-disable-next-line
console.log('Received', message);
/* eslint-enable no-console */
}

switch (message.type) {
case 'event':
case MSG_TYPE_EVENT:
this.commands[message.id].eventCallback(message.event);
break;

case 'result':
case MSG_TYPE_RESULT:
if (message.success) {
this.commands[message.id].resolve(message);
} else {
Expand All @@ -266,14 +266,13 @@ class Connection {
delete this.commands[message.id];
break;

case 'pong':
case MSG_TYPE_PONG:
break;

default:
if (__DEV__) {
/* eslint-disable no-console */
// eslint-disable-next-line
console.warn('Unhandled message', message);
/* eslint-enable no-console */
}
}
}
Expand All @@ -299,14 +298,13 @@ class Connection {
const reconnect = (tries) => {
setTimeout(() => {
if (__DEV__) {
/* eslint-disable no-console */
// eslint-disable-next-line
console.log('Trying to reconnect');
/* eslint-enable no-console */
}
getSocket(this.url, options).then(
socket => this.setSocket(socket),
(err) => err === ERR_INVALID_AUTH ?
this.fireEvent('reconnect-error', err) : reconnect(tries + 1)
err => (err === ERR_INVALID_AUTH ?
this.fireEvent('reconnect-error', err) : reconnect(tries + 1))
);
}, Math.min(tries, 5) * 1000);
};
Expand Down
8 changes: 8 additions & 0 deletions lib/const.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
export const ERR_CANNOT_CONNECT = 1;
export const ERR_INVALID_AUTH = 2;
export const ERR_CONNECTION_LOST = 3;

export const MSG_TYPE_AUTH_REQUIRED = 'auth_required';
export const MSG_TYPE_AUTH_INVALID = 'auth_invalid';
export const MSG_TYPE_AUTH_OK = 'auth_ok';

export const MSG_TYPE_EVENT = 'event';
export const MSG_TYPE_RESULT = 'result';
export const MSG_TYPE_PONG = 'pong';
39 changes: 0 additions & 39 deletions lib/group.js

This file was deleted.

23 changes: 2 additions & 21 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,15 @@ import subscribeEntities from './entities';
import {
ERR_CANNOT_CONNECT,
ERR_INVALID_AUTH,
ERR_CONNECTION_LOST
} from './const';
import {
getGroupEntities,
splitByGroups,
} from './group';
import {
getViewEntities,
extractViews,
} from './view';
import {
extractDomain,
extractObjectId,
} from './util';

export {
ERR_CANNOT_CONNECT,
ERR_INVALID_AUTH,
ERR_CONNECTION_LOST,

createConnection,
subscribeConfig,
subscribeEntities,

getGroupEntities,
splitByGroups,

getViewEntities,
extractViews,

extractDomain,
extractObjectId,
};
8 changes: 0 additions & 8 deletions lib/util.js

This file was deleted.

55 changes: 0 additions & 55 deletions lib/view.js

This file was deleted.

22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"name": "home-assistant-js-websocket",
"version": "1.2.1",
"version": "2.0.0",
"description": "Home Assistant websocket client",
"main": "dist/haws.umd.js",
"jsnext:main": "dist/haws.es.js",
"module": "dist/haws.es.js",
"repository": {
"url": "https://github.com/home-assistant/home-assistant-js-websocket.git",
Expand All @@ -19,16 +18,17 @@
"author": "Paulus Schoutsen <[email protected]>",
"license": "Apache-2.0",
"devDependencies": {
"eslint": "^3.17.1",
"eslint-config-airbnb-base": "^11.1.1",
"eslint": "^4.19.1",
"eslint-config-airbnb-base": "^12.1.0",
"eslint-plugin-import": "^2.2.0",
"mocha": "^3.5.0",
"reify": "^0.12.0",
"rollup": "^0.41.5",
"rollup-plugin-babili": "^2.0.0",
"rollup-plugin-buble": "^0.15.0",
"rollup-plugin-replace": "^1.1.1",
"rollup-watch": "^3.2.2"
"mocha": "^5.1.1",
"reify": "^0.15.1",
"rollup": "^0.59.1",
"rollup-plugin-babel-minify": "^4.0.0",
"rollup-plugin-babili": "^3.1.1",
"rollup-plugin-buble": "^0.19.2",
"rollup-plugin-replace": "^2.0.0",
"rollup-watch": "^4.3.1"
},
"files": [
"dist"
Expand Down
Loading