ethers.js/packages/experimental/lib/metamask-provider.js

93 lines
3.6 KiB
JavaScript
Raw Permalink Normal View History

2019-05-14 18:48:48 -04:00
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
2021-03-07 18:24:04 -05:00
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
2019-05-14 18:48:48 -04:00
return extendStatics(d, b);
};
return function (d, b) {
2021-03-07 18:24:04 -05:00
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
2019-05-14 18:48:48 -04:00
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
2021-03-07 18:24:04 -05:00
exports.MetamaskProvider = void 0;
2019-05-14 18:48:48 -04:00
var ethers_1 = require("ethers");
var _version_1 = require("./_version");
var logger = new ethers_1.ethers.utils.Logger(_version_1.version);
2019-05-14 18:48:48 -04:00
var MetamaskProvider = /** @class */ (function (_super) {
__extends(MetamaskProvider, _super);
function MetamaskProvider(ethereum) {
var _this = this;
if (!ethereum) {
ethereum = global.ethereum;
if (!ethereum) {
logger.throwError("could not auto-detect global.ethereum", ethers_1.ethers.errors.UNSUPPORTED_OPERATION, {
2019-05-14 18:48:48 -04:00
operation: "window.ethereum"
});
}
}
_this = _super.call(this, ethereum) || this;
var _account = null;
ethers_1.ethers.utils.defineReadOnly(_this, "_pollAccountFunc", function () {
var account = null;
if (account === _account) {
return;
}
console.log("poll");
_this.emit("account", account, _account);
_account = account;
});
_this = _super.call(this, ethereum) || this;
return _this;
}
MetamaskProvider.prototype.getSigner = function (addressOrIndex) {
if (!this.enabled) {
return null;
}
return _super.prototype.getSigner.call(this, addressOrIndex);
};
Object.defineProperty(MetamaskProvider.prototype, "enabled", {
get: function () {
return false;
},
2021-03-07 18:24:04 -05:00
enumerable: false,
2019-05-14 18:48:48 -04:00
configurable: true
});
MetamaskProvider.prototype._startPollingAccount = function () {
if (this._pollingAccount) {
return;
}
console.log("start polling for account changes including to/from null");
this._pollingAccount = setInterval(this._pollAccountFunc, 1000);
};
MetamaskProvider.prototype._stopPollingAccount = function () {
if (!this._pollingAccount) {
return;
}
console.log("stop polling for account changes including to/from null");
clearInterval(this._pollingAccount);
this._pollingAccount = null;
};
MetamaskProvider.prototype.on = function (eventName, listener) {
_super.prototype.on.call(this, eventName, listener);
if (this.listenerCount("account") > 0) {
this._startPollingAccount();
}
return this;
};
MetamaskProvider.prototype.off = function (eventName, listener) {
_super.prototype.off.call(this, eventName, listener);
if (this.listenerCount("account") === 0) {
this._stopPollingAccount();
}
return this;
};
return MetamaskProvider;
}(ethers_1.ethers.providers.Web3Provider));
exports.MetamaskProvider = MetamaskProvider;
2020-07-13 08:03:56 -04:00
//# sourceMappingURL=metamask-provider.js.map