Skip to content

Commit

Permalink
add global initialization event
Browse files Browse the repository at this point in the history
return provider from initProvider
  • Loading branch information
rekmarks committed Apr 18, 2020
1 parent 89e06b8 commit b29b6e9
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
module.exports = require('./src/MetamaskInpageProvider')
const MetamaskInpageProvider = require('./src/MetamaskInpageProvider')
const { initProvider, setGlobalProvider } = require('./src/initProvider')

module.exports = {
MetamaskInpageProvider,
initProvider,
setGlobalProvider,
}
19 changes: 18 additions & 1 deletion src/MetamaskInpageProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,29 @@ const {

module.exports = class MetamaskInpageProvider extends SafeEventEmitter {

constructor (connectionStream, shouldSendMetadata = true) {
/**
* @param {Object} connectionStream - A Node.js stream
* @param {Object} opts - An options bag
* @param {number} opts.maxEventListeners - The maximum number of event listeners
* @param {boolean} opts.shouldSendMetadata - Whether the provider should send page metadata
*/
constructor (
connectionStream,
{ shouldSendMetadata = true, maxEventListeners = 100 },
) {

if (
typeof shouldSendMetadata !== 'boolean' || typeof maxEventListeners !== 'number'
) {
throw new Error('Invalid options.')
}

super()

this.isMetaMask = true

this.setMaxEventListeners(maxEventListeners)

// private state, kept here in part for use in the _metamask proxy
this._state = {
sentWarnings: {
Expand Down
55 changes: 55 additions & 0 deletions src/initProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const MetamaskInpageProvider = require('./MetamaskInpageProvider')

/**
* Initializes a MetamaskInpageProvider and (optionally) sets it on window.ethereum.
*
* @param {Object} opts - An options bag.
* @param {Object} opts.connectionStream - A Node.js stream.
* @param {number} opts.maxEventListeners - The maximum number of event listeners.
* @param {boolean} opts.shouldSendMetadata - Whether the provider should send page metadata.
* @param {Object} opts.proxyHandler - A proxy handler object. The provider is proxied if present.
* @param {boolean} opts.shouldSet - Whether the provider should be set as window.ethereum
* @returns {MetamaskInpageProvider | Proxy} The initialized provider (whether set or not).
*/
function initProvider ({
connectionStream,
shouldSendMetadata = true,
maxEventListeners = 100,
proxyHandler,
shouldSet = true,
}) {

if (!connectionStream) {
throw new Error('Must provide a connection stream.')
}

let provider = new MetamaskInpageProvider(
connectionStream, { shouldSendMetadata, maxEventListeners },
)

if (proxyHandler) {
provider = new Proxy(provider, proxyHandler)
}

if (shouldSet) {
setGlobalProvider(provider)
}

return provider
}

/**
* Sets the given provider instance as window.ethereum and dispatches the
* 'ethereum#initialized' event on window.
*
* @param {MetamaskInpageProvider} providerInstance - The provider instance.
*/
function setGlobalProvider (providerInstance) {
window.ethereum = providerInstance
window.dispatchEvent(new Event('ethereum#initialized'))
}

module.exports = {
initProvider,
setGlobalProvider,
}

0 comments on commit b29b6e9

Please sign in to comment.