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:
parent
4ced792aec
commit
a03ef1b002
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user