1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Memoize getEnvironmentType function (#8624)

Co-authored-by: Mark Stacey <markjstacey@gmail.com>
This commit is contained in:
Whymarrh Whitby 2020-05-20 10:36:18 -02:30 committed by GitHub
parent 4ced792aec
commit a03ef1b002
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@ import extension from 'extensionizer'
import ethUtil from 'ethereumjs-util'
import assert from 'assert'
import BN from 'bn.js'
import { memoize } from 'lodash'
import {
ENVIRONMENT_TYPE_POPUP,
@ -16,16 +17,9 @@ import {
} from './enums'
/**
* Used to determine the window type through which the app is being viewed.
* - 'popup' refers to the extension opened through the browser app icon (in top right corner in chrome and firefox)
* - 'fullscreen' refers to the main browser window
* - 'notification' refers to the popup that appears in its own window when taking action outside of metamask
* - 'background' refers to the background page
*
* @returns {string} - A single word label that represents the type of window through which the app is being viewed
*
* @see {@link getEnvironmentType}
*/
const getEnvironmentType = (url = window.location.href) => {
const getEnvironmentTypeMemo = memoize((url) => {
const parsedUrl = new URL(url)
if (parsedUrl.pathname === '/popup.html') {
return ENVIRONMENT_TYPE_POPUP
@ -36,7 +30,22 @@ const getEnvironmentType = (url = window.location.href) => {
} else {
return ENVIRONMENT_TYPE_BACKGROUND
}
}
})
/**
* Returns the window type for the application
*
* - `popup` refers to the extension opened through the browser app icon (in top right corner in chrome and firefox)
* - `fullscreen` refers to the main browser window
* - `notification` refers to the popup that appears in its own window when taking action outside of metamask
* - `background` refers to the background page
*
* NOTE: This should only be called on internal URLs.
*
* @param {string} [url] - the URL of the window
* @returns {string} the environment ENUM
*/
const getEnvironmentType = (url = window.location.href) => getEnvironmentTypeMemo(url)
/**
* Returns the platform (browser) where the extension is running.