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

configurable ping interval #49

Merged
merged 4 commits into from
Jan 5, 2018
Merged

configurable ping interval #49

merged 4 commits into from
Jan 5, 2018

Conversation

shinsuny
Copy link

No description provided.

Copy link
Member

@tjenkinson tjenkinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like the ping interval option, but not sure about the other one

client.on('error', function(e) {

// https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback
client.setTimeout(cfg.timeout);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this makes sense? The documentation says this does nothing except trigger the 'timeout' event which we are not handling

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, will remove this.

**/
function HyperdeckCore(ip) {
function HyperdeckCore(config) {
if (config === null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be

typeof config !== 'string' && typeof config !== 'object'

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see below

function HyperdeckCore(ip) {
function HyperdeckCore(config) {
if (config === null) {
throw 'Invalid Configuration, please refer to documentations.';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to throw an Error object so there's a stack trace.

throw new Error('...');

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  if (config == null || ((typeof config) !== 'string' && config.hasOwnProperty('ip') === false)) {
    throw new Error('Invalid Configuration, please refer to documentations.');
  }

Thinking like this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

== null is OK because it will also catch 'undefined'

But I think it would be clearer though to be more explicit like

function isConfigValid(config) {
return (
typeof config === 'string' ||
(typeof config === 'object' && !!config.ip)
);
}

...

if (!isConfigValid(config))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On phone and it's removed all the indentation

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if config = null then typeof config === 'object' will be true and config.ip will throw exception

Copy link
Member

@tjenkinson tjenkinson Jan 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah l see in which case

(typeof config === 'object' && && config !== null && !!config.ip)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if use config != null then npm test fails so did like below:

  /**
   * validate configuration
   * 
   * @param {*} config 
   */
  function isConfigValid(config) {
    return (
      config !== undefined &&
      config !== null &&
      (typeof config === 'string' || !!config.ip)
    );
  }

  // check for valid configuration
  if (!isConfigValid(config)) {
    throw new Error('Invalid Configuration, please refer to documentations.');
  }

var self = this;
var cfg = {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be a bit neater using Object.assign

var cfg = Object.assign({ pingInterval: 10000 }, config)

Where you would check if the provided config is a string and set it to { ip: config } above if it is

This also means we never mutate the input :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var cfg = {};
if (typeof config === 'string') {
  cfg.ip = config;
}
cfg = Object.assign({ pingInterval: 10000, timeout: 3000 }, cfg);

Do you mean like this?

Copy link
Member

@tjenkinson tjenkinson Jan 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost

if (typeof config === 'string') {
config = { ip: config };
}
config = Object.assign({ pingInterval: 10000, timeout: 3000 }, config);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean:

if (typeof config === 'string') {
config = { ip: config };
}
config = Object.assign({ pingInterval: 10000, timeout: 3000 }, config);

yes its better

Sarveshwar Singh added 2 commits January 5, 2018 16:27
configurable ping interval and timeout
review changes
setTimeout no more needed
**/
function HyperdeckCore(ip) {
function HyperdeckCore(config) {
if (config === undefined ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See other comment :)

Copy link
Member

@tjenkinson tjenkinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great thanks! Will merge and publish a new version when I'm at a computer

@tjenkinson tjenkinson merged commit 87d7eb1 into LA1TV:master Jan 5, 2018
@tjenkinson tjenkinson changed the title configurable ping interval and timeout configurable ping interval Jan 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants