All files / src/utils global.ts

87.1% Statements 27/31
59.38% Branches 19/32
100% Functions 5/5
92.31% Lines 24/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77          14x                                 14x   14x     14x           14x 15x         14x 14x   14x   14x 14x 14x 23x 22x     14x 11x               14x 15x 15x     14x       1x   1x 1x   1x    
import { EVENTTYPES, WxEvents } from '../common/constant'
import { TransportData } from '../core/transportData'
import { Breadcrumb } from '../core/breadcrumb'
import { Logger } from './logger'
import { Options } from '../core/options'
import { variableTypeDetection } from './is'
 
// MITO的全局变量
export interface MitoSupport {
  logger: Logger
  breadcrumb: Breadcrumb
  transportData: TransportData
  replaceFlag: { [key in EVENTTYPES]?: boolean }
  record?: any[]
  options?: Options
}
 
interface MITOGlobal {
  console?: Console
  __MITO__?: MitoSupport
}
 
export const isNodeEnv = variableTypeDetection.isProcess(typeof process !== 'undefined' ? process : 0)
 
export const isWxMiniEnv =
  variableTypeDetection.isObject(typeof wx !== 'undefined' ? wx : 0) && variableTypeDetection.isFunction(typeof App !== 'undefined' ? App : 0)
 
export const isBrowserEnv = variableTypeDetection.isWindow(typeof window !== 'undefined' ? window : 0)
/**
 * 获取全局变量
 *
 * ../returns Global scope object
 */
export function getGlobal<T>() {
  Eif (isBrowserEnv) return (window as unknown) as MITOGlobal & T
  if (isWxMiniEnv) return (wx as unknown) as MITOGlobal & T
  if (isNodeEnv) return (process as unknown) as MITOGlobal & T
}
 
const _global = getGlobal<Window & WechatMiniprogram.Wx>()
const _support = getGlobalMitoSupport()
 
export { _global, _support }
 
_support.replaceFlag = _support.replaceFlag || {}
const replaceFlag = _support.replaceFlag
export function setFlag(replaceType: EVENTTYPES | WxEvents, isSet: boolean): void {
  if (replaceFlag[replaceType]) return
  replaceFlag[replaceType] = isSet
}
 
export function getFlag(replaceType: EVENTTYPES | WxEvents): boolean {
  return replaceFlag[replaceType] ? true : false
}
 
/**
 * 获取全部变量__MITO__的引用地址
 *
 * ../returns global variable of MITO
 */
export function getGlobalMitoSupport(): MitoSupport {
  _global.__MITO__ = _global.__MITO__ || ({} as MitoSupport)
  return _global.__MITO__
}
 
export function supportsHistory(): boolean {
  // NOTE: in Chrome App environment, touching history.pushState, *even inside
  //       a try/catch block*, will cause Chrome to output an error to console.error
  // borrowed from: https://github.com/angular/angular.js/pull/13945/files
  const chrome = (_global as any).chrome
  // tslint:disable-next-line:no-unsafe-any
  const isChromePackagedApp = chrome && chrome.app && chrome.app.runtime
  const hasHistoryApi = 'history' in _global && !!_global.history.pushState && !!_global.history.replaceState
 
  return !isChromePackagedApp && hasHistoryApi
}