Skip to content

Commit

Permalink
refactor(node-pool)!: inline helpers, export by default
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Jun 16, 2021
1 parent 3e84139 commit ed1cfb5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 32 deletions.
17 changes: 0 additions & 17 deletions src/node-pool/helpers.js

This file was deleted.

34 changes: 21 additions & 13 deletions src/node-pool/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
import stampit from '@stamp/it'
import Joi from 'joi-browser'
import { getterForCurrentNode, prepareNodeObject } from './helpers'
import { getNetworkId } from '../node'

/**
Expand All @@ -19,7 +18,7 @@ import { getNetworkId } from '../node'
* @param {Array} [options.nodes] - Array with Node instances
* @return {Object} NodePool instance
*/
export const NodePool = stampit({
export default stampit({
init ({ nodes = [] } = {}) {
this.pool = new Map()
this.validateNodes(nodes)
Expand All @@ -35,7 +34,10 @@ export const NodePool = stampit({
enumerable: true,
configurable: false,
get () {
return getterForCurrentNode(this.selectedNode)
if (!this.selectedNode || !this.selectedNode.instance) {
throw new Error('You can\'t use Node API. Node is not connected or not defined!')
}
return this.selectedNode.instance.api
}
}
},
Expand All @@ -44,20 +46,28 @@ export const NodePool = stampit({
* Add Node
* @function
* @alias module:@aeternity/aepp-sdk/es/node-pool
* @rtype (name: String, nodeInstance: Object, select: Boolean) => Void
* @rtype (name: String, nodeInstance: Object, select: Boolean) => void
* @param {String} name - Node name
* @param {Object} nodeInstance - Node instance
* @param {Object} node - Node instance
* @param {Boolean} select - Select this node as current
* @return {Void}
* @return {void}
* @example
* nodePool.addNode('testNode', awaitNode({ url, internalUrl }), true) // add and select new node with name 'testNode'
*/
addNode (name, nodeInstance, select = false) {
addNode (name, node, select = false) {
if (this.pool.has(name)) throw new Error(`Node with name ${name} already exist`)

this.validateNodes([{ name, instance: nodeInstance }])
this.validateNodes([{ name, instance: node }])

this.pool.set(name, prepareNodeObject(name, nodeInstance))
this.pool.set(name, {
name,
instance: node,
url: node.url,
internalUrl: node.internalUrl,
networkId: node.nodeNetworkId,
version: node.version,
consensusProtocolVersion: node.consensusProtocolVersion
})
if (select || !this.selectedNode) {
this.selectNode(name)
}
Expand All @@ -66,9 +76,9 @@ export const NodePool = stampit({
* Select Node
* @function
* @alias module:@aeternity/aepp-sdk/es/node-pool
* @rtype (name: String) => Void
* @rtype (name: String) => void
* @param {String} name - Node name
* @return {Void}
* @return {void}
* @example
* nodePool.selectNode('testNode')
*/
Expand Down Expand Up @@ -148,5 +158,3 @@ export const NodePool = stampit({
selectedNode: {}
}
})

export default NodePool
2 changes: 1 addition & 1 deletion src/tx/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
TX_TYPE
} from './builder/schema'
import { buildTxHash, calculateFee, unpackTx } from './builder'
import { NodePool } from '../node-pool'
import NodePool from '../node-pool'

/**
* Transaction validator
Expand Down
2 changes: 1 addition & 1 deletion test/integration/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { url, internalUrl, ignoreVersion } from './'
import { describe, it, before } from 'mocha'
import { expect } from 'chai'
import * as R from 'ramda'
import { NodePool } from '../../src/node-pool'
import NodePool from '../../src/node-pool'

describe('Node client', function () {
let client
Expand Down

0 comments on commit ed1cfb5

Please sign in to comment.