Skip to content

Commit 131208d

Browse files
author
Vladimir Enchev
committed
lazy loading code refactored
1 parent f5ba963 commit 131208d

File tree

1 file changed

+28
-53
lines changed

1 file changed

+28
-53
lines changed

globals/globals.ts

Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,34 @@ global.moduleMerge = function (sourceExports: any, destExports: any) {
55
destExports[key] = sourceExports[key];
66
}
77
}
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+
836
import platform = require("platform");
937
import consoleModule = require("console");
1038

@@ -16,59 +44,6 @@ if (platform.device.os === platform.platformNames.android) {
1644
global.console.dump = function (args) { c.dump(args); };
1745
}
1846

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-
7247
if (typeof global.__decorate !== "function") {
7348
global.__decorate = function (decorators, target, key, desc) {
7449
if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") {

0 commit comments

Comments
 (0)