import * as tslibType from 'tslib' ; const tslib = require ( 'tslib' ) ; import { Observable } from '../data/observable' ; import { trace as profilingTrace , time , uptime , level as profilingLevel } from '../profiling' ; type ModuleLoader = ( name ? : string ) => any ; interface Context { keys ( ) : string [ ] ; ( key ) : any ; } interface ExtensionMap { [ originalFileExtension ] : string ; } function registerOnGlobalContext ( moduleName , exportName ) { Object . defineProperty ( global , exportName , { get : function ( ) { const m = global . loadModule ( moduleName ) ; const resolvedValue = m [ exportName ] ; Object . defineProperty ( global , exportName , { value : resolvedValue , configurable : true , writable : true , } ) ; return resolvedValue ; } , configurable : true , } ) ; } /**
 * Manages internal framework global state
 */ export class NativeScriptGlobalState { events : Observable ; launched = false ; appEventWiring : Array < any > ; private _appInstanceReady = false ; private _setLaunched : ( ) => void ; constructor ( ) { this . events = new Observable ( ) ; this . _setLaunched = this . _setLaunchedFn . bind ( this ) ; this . events . on ( 'launch' , this . _setLaunched ) ; if ( profilingLevel ( ) > 0 ) { this . events . on ( 'displayed' , ( ) => { const duration = uptime ( ) ; const end = time ( ) ; const start = end - duration ; profilingTrace ( `Displayed in ${ duration . toFixed ( 2 ) }ms` , start , end ) ; } ) ; } } get appInstanceReady ( ) { return this . _appInstanceReady ; } set appInstanceReady ( value ) { this . _appInstanceReady = value ; if ( this . appEventWiring && this . appEventWiring . length ) { for ( const callback of this . appEventWiring ) { callback ( ) ; } this . appEventWiring = null ; } } param callback addEventWiring ( callback ) { if ( this . _appInstanceReady ) { callback ( ) ; } else { if ( ! this . appEventWiring ) { this . appEventWiring = [ ] ; } this . appEventWiring . push ( callback ) ; } } private _setLaunchedFn ( ) { this . launched = true ; this . events . off ( 'launch' , this . _setLaunched ) ; this . _setLaunched = null ; } } export function installPolyfills ( moduleName , exportNames ) { if ( global . __snapshot ) { const loadedModule = global . loadModule ( moduleName ) ; exportNames . forEach ( ( exportName ) => ( global [ exportName ] = loadedModule [ exportName ] ) ) ; } else { exportNames . forEach ( ( exportName ) => registerOnGlobalContext ( moduleName , exportName ) ) ; } } export function initGlobal ( ) { if ( ! global . NativeScriptHasInitGlobal ) { global . NativeScriptHasInitGlobal = true ; global . NativeScriptGlobals = new NativeScriptGlobalState ( ) ; if ( ! ( < any > global ) . __extends ) { ( < any > global ) . __extends = function ( d , b ) { for ( const p in b ) { if ( b . hasOwnProperty ( p ) ) { d [ p ] = b [ p ] ; } } function __ ( ) { this . constructor = d ; } d . prototype = b === null ? Object . create ( b ) : ( ( __ . prototype = b . prototype ) , new __ ( ) ) ; } ; } for ( const fnName of Object . keys ( tslib ) ) { if ( typeof tslib [ fnName ] !== 'function' ) { continue ; } if ( fnName in global ) { continue ; } global [ fnName ] = tslib [ fnName ] ; } const modules = new Map < string , { moduleId : string ; loader : ModuleLoader ; } > ( ) ; const modulesLoadedForUI = new Set < string > ( ) ; const defaultExtensionMap = { '.js' : '.js' , '.ts' : '.js' , '.kt' : '.js' , '.css' : '.css' , '.scss' : '.css' , '.less' : '.css' , '.sass' : '.css' , '.xml' : '.xml' , } ; ( < any > global ) . moduleResolvers = [ global . require ] ; global . registerModule = function ( name , loader ) : void { modules . set ( name , { loader , moduleId : name } ) ; } ; global . _unregisterModule = function _unregisterModule ( name ) : void { modules . delete ( name ) ; } ; global . _isModuleLoadedForUI = function _isModuleLoadedForUI ( moduleName ) : boolean { return modulesLoadedForUI . has ( moduleName ) ; } ; global . registerWebpackModules = function registerWebpackModules ( context , extensionMap = { } ) { context . keys ( ) . forEach ( ( moduleId ) => { const extDotIndex = moduleId . lastIndexOf ( '.' ) ; const base = moduleId . substr ( 0 , extDotIndex ) ; const originalExt = moduleId . substr ( extDotIndex ) ; const registerExt = extensionMap [ originalExt ] || defaultExtensionMap [ originalExt ] || originalExt ; const isSourceFile = originalExt !== registerExt ; const registerName = base + registerExt ; const registerWithName = ( nickName ) => { modules . set ( nickName , { moduleId , loader : ( ) => { return context ( moduleId ) ; } , } ) ; } ; if ( registerName . startsWith ( './' ) && registerName . endsWith ( '.js' ) ) { const jsNickNames = [ registerName . substr ( 2 , registerName . length - 5 ) , registerName . substr ( 0 , registerName . length - 3 ) , registerName . substr ( 2 ) , ] ; jsNickNames . forEach ( ( jsNickName ) => { if ( isSourceFile || ! global . moduleExists ( jsNickName ) ) { registerWithName ( jsNickName ) ; } } ) ; } else if ( registerName . startsWith ( './' ) ) { const moduleNickNames = [ registerName . substr ( 2 ) , ] ; moduleNickNames . forEach ( ( moduleNickName ) => { if ( ! global . moduleExists ( moduleNickName ) ) { registerWithName ( moduleNickName ) ; } } ) ; } if ( isSourceFile || ! global . moduleExists ( registerName ) ) { registerWithName ( registerName ) ; } } ) ; } ; global . moduleExists = function moduleExists ( name ) : boolean { return modules . has ( name ) ; } ; global . loadModule = function loadModule ( name , isUIModule = false ) : any { const moduleInfo = modules . get ( name ) ; if ( moduleInfo ) { if ( isUIModule ) { modulesLoadedForUI . add ( moduleInfo . moduleId ) ; } const result = moduleInfo . loader ( name ) ; if ( result . enableAutoAccept ) { result . enableAutoAccept ( ) ; } return result ; } for ( const resolver of ( < any > global ) . moduleResolvers ) { const result = resolver ( name ) ; if ( result ) { modules . set ( name , { moduleId : name , loader : ( ) => result } ) ; return result ; } } } ; global . getRegisteredModules = function getRegisteredModules ( ) : string [ ] { return Array . from ( modules . keys ( ) ) ; } ; /**
         * Polyfills
         */ global . moduleMerge = function ( sourceExports , destExports ) { for ( const key in sourceExports ) { destExports [ key ] = sourceExports [ key ] ; } } ; global . zonedCallback = function ( callback ) : Function { if ( ( < any > global ) . zone ) { return ( < any > global ) . zone . bind ( callback ) ; } if ( ( < any > global ) . Zone ) { return ( < any > global ) . Zone . current . wrap ( callback ) ; } else { return callback ; } } ; ( < any > global ) . System = { import ( path ) { return new Promise ( ( resolve , reject ) => { try { resolve ( global . require ( path ) ) ; } catch ( e ) { reject ( e ) ; } } ) ; } , } ; global . registerModule ( 'timer' , ( ) => require ( '../timer' ) ) ; installPolyfills ( 'timer' , [ 'setTimeout' , 'clearTimeout' , 'setInterval' , 'clearInterval' ] ) ; global . registerModule ( 'animation' , ( ) => require ( '../animation-frame' ) ) ; installPolyfills ( 'animation' , [ 'requestAnimationFrame' , 'cancelAnimationFrame' ] ) ; global . registerModule ( 'ui-dialogs' , ( ) => require ( '../ui/dialogs' ) ) ; installPolyfills ( 'ui-dialogs' , [ 'alert' , 'confirm' , 'prompt' , 'login' , 'action' ] ) ; global . registerModule ( 'text' , ( ) => require ( '../text' ) ) ; installPolyfills ( 'text' , [ 'TextDecoder' , 'TextEncoder' ] ) ; global . registerModule ( 'xhr' , ( ) => require ( '../xhr' ) ) ; installPolyfills ( 'xhr' , [ 'XMLHttpRequest' , 'FormData' , 'Blob' , 'File' , 'FileReader' ] ) ; global . registerModule ( 'fetch' , ( ) => require ( '../fetch' ) ) ; installPolyfills ( 'fetch' , [ 'fetch' , 'Headers' , 'Request' , 'Response' ] ) ; global . Deprecated = function ( target , key ? , descriptor ? ) { if ( descriptor ) { const originalMethod = descriptor . value ; descriptor . value = function ( ... args ) { console . log ( `${ key . toString ( ) } is deprecated` ) ; return originalMethod . apply ( this , args ) ; } ; return descriptor ; } else { console . log ( `${ ( target && ( < any > target ) . name ) || target } is deprecated` ) ; return target ; } } ; global . Experimental = function ( target , key ? , descriptor ? ) { if ( descriptor ) { const originalMethod = descriptor . value ; descriptor . value = function ( ... args ) { console . log ( `${ key . toString ( ) } is experimental` ) ; return originalMethod . apply ( this , args ) ; } ; return descriptor ; } else { console . log ( `${ ( target && ( < any > target ) . name ) || target } is experimental` ) ; return target ; } } ; } } if ( ! global . NativeScriptHasInitGlobal ) { initGlobal ( ) ; }