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

Add DeviceData and eventHandlers.js #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ryan-rowland
Copy link

@mar-v-in Based on our conversation around nimiq-network/developer-reference#24

This PR does a few things:

  1. Adds knowledge of deviceData to PoolAgent
  2. Adds public read-only properties to PoolAgent, since we will now be exposing it to pool owners. Properties are: deviceId, deviceLabel, mode, isRegistered
  3. Added eventHandlers.sample.js and support for eventHandlers.js which allows pool owners to easily add handlers for events. I started with onRegister and beforeRegister but this can be expanded on.

@ryan-rowland ryan-rowland mentioned this pull request May 16, 2018
*/

/**
* Fired when a REGISTER message is received, before creating a corresponding
Copy link
Contributor

Choose a reason for hiding this comment

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

The description is misleading. The PoolAgent was already created at this time (because it is created before the first register arrives). There is also no reason why the PoolAgent instance is not a param on this function.

My suggestions would be
a) add PoolAgent param to beforeRegister
b) rename beforeRegister to onRegisterMessage
c) fix the description to state that it is fired before the message is processed by the PoolAgent
d) also rename onRegister to onRegistrationCompleted to make clear that it is fired after the registration.

src/PoolAgent.js Outdated
@@ -37,6 +37,30 @@ class PoolAgent extends Nimiq.Observable {
/** @type {Nimiq.Timers} */
this._timers = new Nimiq.Timers();
this._timers.resetTimeout('connection-timeout', () => this._onError(), this._pool.config.connectionTimeout);

// Public interface
Object.defineProperties(this, {
Copy link
Contributor

Choose a reason for hiding this comment

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

We use ES6 getters in our codebase instead.

@ryan-rowland
Copy link
Author

@mar-v-in Addressed your concerns.

* if registration should not continue.
* @returns {void}
*/
module.exports.onRegisterMessage = function onRegisterMessage(agent, msg, connectionPool) { }
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this one be async as well?

Copy link
Author

Choose a reason for hiding this comment

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

I thought I'd leave it synchronous since I was catching with try/catch but I suppose this will still work as async.

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, exceptions do work across async/await

src/PoolAgent.js Outdated
this._pool.requestCurrentHead(this);
}
await this.sendBalance();
this._timers.resetInterval('send-balance', () => this.sendBalance(), 1000 * 60 * 5);
this._timers.resetInterval('send-keep-alive-ping', () => this._ws.ping(), 1000 * 10);

this._pool.eventHandlers.onRegistrationCompleted(this, this._pool.connectionPool);
Copy link
Contributor

Choose a reason for hiding this comment

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

missing await?

Copy link
Author

Choose a reason for hiding this comment

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

Good catch

@ryan-rowland
Copy link
Author

Thanks @mar-v-in, addressed second round of comments.

* Fired when a REGISTER message is received, before being processed by the
* PoolAgent. Good time to perform validation or mutation of message data.
* @param {PoolAgent} agent - The Agent for the newly registered device.
* @param {Object} msg - The full register message. This is not a copy; any

Choose a reason for hiding this comment

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

msg should definitely be a copy

@Zolmeister
Copy link

This feels like it should be middleware.js (instead of having events trigger side-effects)

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.

3 participants