Skip to content

Commit

Permalink
requestManager handling and add of 'data' listener improved
Browse files Browse the repository at this point in the history
  • Loading branch information
nivida committed Oct 24, 2019
1 parent 9dd09db commit 1a05194
Show file tree
Hide file tree
Showing 7 changed files with 863 additions and 858 deletions.
11 changes: 8 additions & 3 deletions packages/web3-core-requestmanager/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var RequestManager = function RequestManager(provider) {
this.provider = null;
this.providers = RequestManager.providers;

this.setProvider(provider);
this.setProvider(provider, null, false);
this.subscriptions = {};
};

Expand All @@ -61,9 +61,14 @@ RequestManager.providers = {
* Should be used to set provider of request manager
*
* @method setProvider
*
* @param {Object} p
* @param {net.Socket} net
* @param {boolean} registerDataListener - default true
*
* @returns void
*/
RequestManager.prototype.setProvider = function (p, net) {
RequestManager.prototype.setProvider = function (p, net, registerDataListener) {
var _this = this;

// autodetect provider
Expand Down Expand Up @@ -94,7 +99,7 @@ RequestManager.prototype.setProvider = function (p, net) {
this.provider = p || null;

// listen to incoming notifications
if(this.provider && this.provider.on) {
if(this.provider && this.provider.on && registerDataListener !== false) {
this.provider.on('data', function requestManagerNotification(result, deprecatedResult){
result = result || deprecatedResult; // this is for possible old providers, which may had the error first handler

Expand Down
2 changes: 0 additions & 2 deletions packages/web3-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ module.exports = {
throw new Error('You need to instantiate using the "new" keyword.');
}


// make property of pkg._provider, which can properly set providers
Object.defineProperty(pkg, 'currentProvider', {
get: function () {
Expand All @@ -50,7 +49,6 @@ module.exports = {
// inherit from web3 umbrella package
if (args[0] && args[0]._requestManager) {
pkg._requestManager = new requestManager.Manager(args[0].currentProvider);

// set requestmanager on package
} else {
pkg._requestManager = new requestManager.Manager();
Expand Down
13 changes: 12 additions & 1 deletion packages/web3-eth-contract/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var Contract = function Contract(jsonInterface, address, options) {
}

// sets _requestmanager
core.packageInit(this, [this.constructor.currentProvider]);
core.packageInit(this, [this.constructor]);

this.clearSubscriptions = this._requestManager.clearSubscriptions;

Expand Down Expand Up @@ -225,6 +225,17 @@ var Contract = function Contract(jsonInterface, address, options) {

};

/**
* Sets the new provider, creates a new requestManager, registers the "data" listener on the provider and sets the
* accounts module for the Contract class.
*
* @method setProvider
*
* @param {string|provider} provider
* @param {Accounts} accounts
*
* @returns void
*/
Contract.setProvider = function(provider, accounts) {
// Contract.currentProvider = provider;
core.packageInit(this, [provider]);
Expand Down
13 changes: 3 additions & 10 deletions packages/web3-eth-ens/src/contracts/Registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,10 @@ var RESOLVER_ABI = require('../ressources/ABI/Resolver');
* @constructor
*/
function Registry(ens) {
var self = this;
this.ens = ens;
this.contract = ens.checkNetwork().then(function (address) {
var contract = new Contract(REGISTRY_ABI, address);
contract.setProvider(self.ens.eth.currentProvider);

return contract;
return new Contract(REGISTRY_ABI, address);
// contract.setProvider(self.ens.eth.currentProvider);
});
}

Expand Down Expand Up @@ -86,14 +83,10 @@ Registry.prototype.owner = function (name, callback) {
* @return {Promise<Contract>}
*/
Registry.prototype.resolver = function (name) {
var self = this;

return this.contract.then(function (contract) {
return contract.methods.resolver(namehash.hash(name)).call();
}).then(function (address) {
var contract = new Contract(RESOLVER_ABI, address);
contract.setProvider(self.ens.eth.currentProvider);
return contract;
return new Contract(RESOLVER_ABI, address);
});
};

Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth-personal/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var Personal = function Personal() {
// sets _requestmanager
core.packageInit(this, arguments);

this.net = new Net(this.currentProvider);
this.net = new Net(this);

var defaultAccount = null;
var defaultBlock = 'latest';
Expand Down
18 changes: 11 additions & 7 deletions packages/web3-eth/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ var Eth = function Eth() {
_this.net.setProvider.apply(_this, arguments);
_this.personal.setProvider.apply(_this, arguments);
_this.accounts.setProvider.apply(_this, arguments);
_this.Contract.setProvider(_this.currentProvider, _this.accounts);
_this.Contract._requestManager = _this._requestManager;
_this.Contract.accounts = _this.accounts;
};


Expand Down Expand Up @@ -234,15 +235,15 @@ var Eth = function Eth() {
this.clearSubscriptions = _this._requestManager.clearSubscriptions;

// add net
this.net = new Net(this.currentProvider);
this.net = new Net(this);
// add chain detection
this.net.getNetworkType = getNetworkType.bind(this);

// add accounts
this.accounts = new Accounts(this.currentProvider);
this.accounts = new Accounts(this);

// add personal
this.personal = new Personal(this.currentProvider);
this.personal = new Personal(this);
this.personal.defaultAccount = this.defaultAccount;

// create a proxy Contract type for this instance, as a Contract's provider
Expand All @@ -262,7 +263,7 @@ var Eth = function Eth() {
var setProvider = self.setProvider;
self.setProvider = function() {
setProvider.apply(self, arguments);
core.packageInit(_this, [self.currentProvider]);
core.packageInit(_this, [self]);
};
};

Expand All @@ -282,7 +283,9 @@ var Eth = function Eth() {
this.Contract.transactionBlockTimeout = this.transactionBlockTimeout;
this.Contract.transactionConfirmationBlocks = this.transactionConfirmationBlocks;
this.Contract.transactionPollingTimeout = this.transactionPollingTimeout;
this.Contract.setProvider(this.currentProvider, this.accounts);
this.Contract._requestManager = this._requestManager;
this.Contract._ethAccounts = this.accounts;
this.Contract.currentProvider = this._requestManager.provider;

// add IBAN
this.Iban = Iban;
Expand Down Expand Up @@ -567,7 +570,7 @@ var Eth = function Eth() {

methods.forEach(function(method) {
method.attachToObject(_this);
method.setRequestManager(_this._requestManager, _this.accounts); // second param means is eth.accounts (necessary for wallet signing)
method.setRequestManager(_this._requestManager, _this.accounts); // second param is the eth.accounts module (necessary for signing transactions locally)
method.defaultBlock = _this.defaultBlock;
method.defaultAccount = _this.defaultAccount;
method.transactionBlockTimeout = _this.transactionBlockTimeout;
Expand All @@ -577,6 +580,7 @@ var Eth = function Eth() {

};

// Adds the static givenProvider and providers property to the Eth module
core.addProviders(Eth);


Expand Down
Loading

0 comments on commit 1a05194

Please sign in to comment.