Skip to content

Commit

Permalink
Add 'this.protocol' attribute to factory classes
Browse files Browse the repository at this point in the history
- allows user to customize protocol class used for factory
  • Loading branch information
isaacgr committed Sep 20, 2021
1 parent 7378b40 commit 62e6781
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/client/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ class HttpClientFactory extends JsonRpcClientFactory {
...this.options.headers
}
};
this.protocol = HttpClientProtocol;
this.headers = this.options.headers;
this.encoding = this.options.encoding;
this.scheme = this.options.scheme;

this.pcolInstance = new HttpClientProtocol(
this.pcolInstance = new this.protocol(
this,
this.options.version,
this.options.delimiter,
Expand Down
3 changes: 3 additions & 0 deletions src/client/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const EventEmitter = require("events");
const JsonRpcClientProtocol = require("./protocol/base");

/**
* Creates an instance of JsonRpcClientFactory. This is the base factory which
Expand All @@ -18,6 +19,7 @@ class JsonRpcClientFactory extends EventEmitter {
* @param {number} [options.timeout=30] Timeout for request response
* @param {number} [options.connectionTimeout=5000] Timeout for connection to server
* @param {number} [options.retries=2] Number of connection retry attempts
* @property {Object} protocol Instance of [JsonRpcClientProtocol]{@link JsonRpcClientProtocol} to use for managing client connections
* @property {class} pcolInstance The [JsonRpcClientProtocol]{@link JsonRpcClientProtocol} instance
* @property {object} timeouts Key value pairs of request IDs to `setTimeout` instance
* @property {number} requestTimeout Same as `options.timeout`
Expand Down Expand Up @@ -45,6 +47,7 @@ class JsonRpcClientFactory extends EventEmitter {
...defaults,
...(options || {})
};
this.protocol = JsonRpcClientProtocol;
this.pcolInstance = undefined;
this.timeouts = {};
this.requestTimeout = this.options.timeout * 1000;
Expand Down
7 changes: 6 additions & 1 deletion src/client/tcp.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ const TcpClientProtocol = require("./protocol/tcp");
* @extends JsonRpcClientFactory
*/
class TcpClientFactory extends JsonRpcClientFactory {
constructor(options) {
super(options);
this.protocol = TcpClientProtocol;
}

/** @inheritdoc */
buildProtocol() {
this.pcolInstance = new TcpClientProtocol(
this.pcolInstance = new this.protocol(
this,
this.options.version,
this.options.delimiter
Expand Down
4 changes: 2 additions & 2 deletions src/client/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ class WsClientFactory extends JsonRpcClientFactory {
...defaults,
...(this.options || {})
};

this.protocol = WsClientProtocol;
this.url = this.options.url;
}

/** @inheritdoc */
buildProtocol() {
this.pcolInstance = new WsClientProtocol(
this.pcolInstance = new this.protocol(
this,
this.options.version,
this.options.delimiter
Expand Down

0 comments on commit 62e6781

Please sign in to comment.