forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobals.ts
More file actions
154 lines (124 loc) · 4.96 KB
/
globals.ts
File metadata and controls
154 lines (124 loc) · 4.96 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// This method iterates all the keys in the source exports object and copies them to the destination exports one.
// Note: the method will not check for naming collisions and will override any already existing entries in the destination exports.
global.moduleMerge = function (sourceExports: any, destExports: any) {
for (var key in sourceExports) {
destExports[key] = sourceExports[key];
}
}
import * as timerModule from "timer";
import * as dialogsModule from "ui/dialogs";
type ModuleLoader = () => any;
const modules: Map<string, ModuleLoader> = new Map<string, ModuleLoader>();
global.registerModule = function(name: string, loader: ModuleLoader): void {
modules.set(name, loader);
}
global.moduleExists = function(name: string): boolean {
return modules.has(name);
}
global.loadModule = function(name: string): any {
const loader = modules.get(name);
if (loader) {
return loader();
} else {
return require(name);
}
}
global.zonedCallback = function (callback: Function): Function {
if (global.zone) {
// Zone v0.5.* style callback wrapping
return global.zone.bind(callback);
}
if (global.Zone) {
// Zone v0.6.* style callback wrapping
return global.Zone.current.wrap(callback);
} else {
return callback;
}
}
global.registerModule("timer", () => require("timer"));
global.registerModule("ui/dialogs", () => require("ui/dialogs"));
global.registerModule("xhr", () => require("xhr"));
global.registerModule("fetch", () => require("fetch"));
const __tnsGlobalMergedModules = new Map<string, boolean>();
function registerOnGlobalContext(name: string, module: string): void {
Object.defineProperty(global, name, {
get: function () {
// We do not need to cache require() call since it is already cached in the runtime.
let m = global.loadModule(module);
if (!__tnsGlobalMergedModules.has(module)) {
__tnsGlobalMergedModules.set(module, true);
global.moduleMerge(m, global);
}
// Redefine the property to make sure the above code is executed only once.
let resolvedValue = m[name];
Object.defineProperty(this, name, { value: resolvedValue, configurable: true, writable: true });
return resolvedValue;
},
configurable: true
});
}
if (global.__snapshot) {
// when we have a snapshot, it is better to pre-populate these on the global context to get them saved within the blob
var timer: typeof timerModule = require("timer");
global.setTimeout = timer.setTimeout;
global.clearTimeout = timer.clearTimeout;
global.setInterval = timer.setInterval;
global.clearInterval = timer.clearInterval;
var dialogs: typeof dialogsModule = require("ui/dialogs");
global.alert = dialogs.alert;
global.confirm = dialogs.confirm;
global.prompt = dialogs.prompt;
var xhr = require("xhr");
global.XMLHttpRequest = xhr.XMLHttpRequest;
global.FormData = xhr.FormData;
var fetch = require("fetch");
global.fetch = fetch.fetch;
} else {
registerOnGlobalContext("setTimeout", "timer");
registerOnGlobalContext("clearTimeout", "timer");
registerOnGlobalContext("setInterval", "timer");
registerOnGlobalContext("clearInterval", "timer");
registerOnGlobalContext("alert", "ui/dialogs");
registerOnGlobalContext("confirm", "ui/dialogs");
registerOnGlobalContext("prompt", "ui/dialogs");
registerOnGlobalContext("XMLHttpRequest", "xhr");
registerOnGlobalContext("FormData", "xhr");
registerOnGlobalContext("fetch", "fetch");
}
import platform = require("platform");
import consoleModule = require("console");
var c = new consoleModule.Console();
if (platform.device.os === platform.platformNames.android) {
global.console = c;
} else if (platform.device.os === platform.platformNames.ios) {
global.console.dump = function (args) { c.dump(args); };
}
require("./decorators");
export function Deprecated(target: Object, key?: string | symbol, descriptor?: any) {
if (descriptor) {
var originalMethod = descriptor.value;
descriptor.value = function (...args: any[]) {
console.log(`${key} is deprecated`);
return originalMethod.apply(this, args);
}
return descriptor;
} else {
console.log(`${(target && (<any>target).name || target)} is deprecated`);
return target;
}
}
global.Deprecated = Deprecated;
export function Experimental(target: Object, key?: string | symbol, descriptor?: any) {
if (descriptor) {
var originalMethod = descriptor.value;
descriptor.value = function (...args: any[]) {
console.log(`${key} is experimental`);
return originalMethod.apply(this, args);
}
return descriptor;
} else {
console.log(`${(target && (<any>target).name || target)} is experimental`);
return target;
}
}
global.Experimental = Experimental;