@@ -5,6 +5,34 @@ global.moduleMerge = function (sourceExports: any, destExports: any) {
5
5
destExports [ key ] = sourceExports [ key ] ;
6
6
}
7
7
}
8
+
9
+ function registerOnGlobalContext ( name , module ) {
10
+ Object . defineProperty ( global , name , {
11
+ get : function ( ) {
12
+ // We do not need to cache require() call since it is already cached in the runtime.
13
+ let m = require ( module ) ;
14
+ global . moduleMerge ( m , global ) ;
15
+
16
+ // Redefine the property to make sure the above code is executed only once.
17
+ Object . defineProperty ( this , name , { value : m [ name ] , configurable : true , writable : true } ) ;
18
+
19
+ return m [ name ] ;
20
+ } ,
21
+ configurable : true
22
+ } ) ;
23
+ }
24
+
25
+ registerOnGlobalContext ( "setTimeout" , "timer" ) ;
26
+ registerOnGlobalContext ( "clearTimeout" , "timer" ) ;
27
+ registerOnGlobalContext ( "setInterval" , "timer" ) ;
28
+ registerOnGlobalContext ( "clearInterval" , "timer" ) ;
29
+ registerOnGlobalContext ( "alert" , "ui/dialogs" ) ;
30
+ registerOnGlobalContext ( "confirm" , "ui/dialogs" ) ;
31
+ registerOnGlobalContext ( "prompt" , "ui/dialogs" ) ;
32
+ registerOnGlobalContext ( "XMLHttpRequest" , "../xhr/xhr" ) ;
33
+ registerOnGlobalContext ( "FormData" , "../xhr/xhr" ) ;
34
+ registerOnGlobalContext ( "fetch" , "fetch" ) ;
35
+
8
36
import platform = require( "platform" ) ;
9
37
import consoleModule = require( "console" ) ;
10
38
@@ -16,59 +44,6 @@ if (platform.device.os === platform.platformNames.android) {
16
44
global . console . dump = function ( args ) { c . dump ( args ) ; } ;
17
45
}
18
46
19
- var tm ;
20
- function getTimer ( ) {
21
- if ( ! tm ) {
22
- tm = require ( "timer" ) ;
23
- }
24
-
25
- return tm ;
26
- }
27
-
28
- global . setTimeout = function ( callback , milliseconds ) {
29
- return getTimer ( ) . setTimeout ( callback , milliseconds ) ;
30
- }
31
-
32
- global . clearTimeout = function ( id ) {
33
- getTimer ( ) . clearTimeout ( id ) ;
34
- }
35
-
36
- global . setInterval = function ( callback , milliseconds ) {
37
- return getTimer ( ) . setInterval ( callback , milliseconds ) ;
38
- }
39
-
40
- global . clearInterval = function ( id ) {
41
- getTimer ( ) . clearInterval ( id ) ;
42
- }
43
-
44
- var dm ;
45
- function getDialogs ( ) {
46
- if ( ! dm ) {
47
- dm = require ( "ui/dialogs" ) ;
48
- }
49
-
50
- return dm ;
51
- }
52
-
53
- global . alert = function ( args ) {
54
- return getDialogs ( ) . alert ( args ) ;
55
- }
56
-
57
- global . confirm = function ( args ) {
58
- return getDialogs ( ) . confirm ( args ) ;
59
- }
60
-
61
- global . prompt = function ( args ) {
62
- return getDialogs ( ) . prompt ( args ) ;
63
- }
64
-
65
- var xhr = require ( "../xhr/xhr" ) ;
66
- global . moduleMerge ( xhr , global ) ;
67
-
68
- // Fetch module should be after XMLHttpRequest/FormData!
69
- var fetchModule = require ( "fetch" ) ;
70
- global . moduleMerge ( fetchModule , global ) ;
71
-
72
47
if ( typeof global . __decorate !== "function" ) {
73
48
global . __decorate = function ( decorators , target , key , desc ) {
74
49
if ( typeof global . Reflect === "object" && typeof global . Reflect . decorate === "function" ) {
0 commit comments