Globals in snapshot are overriden by the V8 on start, so they have to be installed later (#4387)

This commit is contained in:
Panayot Cankov
2017-06-15 10:36:50 +03:00
committed by GitHub
parent ab5da0a4a9
commit b94a61f9a5
4 changed files with 61 additions and 40 deletions

4
tns-core-modules/globals/globals.d.ts vendored Normal file
View File

@ -0,0 +1,4 @@
/**
* Register globals such as setTimeout, setInterval, console etc.
*/
export function install();

View File

@ -86,49 +86,62 @@ function registerOnGlobalContext(name: string, module: string): void {
});
}
if ((<any>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");
(<any>global).setTimeout = timer.setTimeout;
(<any>global).clearTimeout = timer.clearTimeout;
(<any>global).setInterval = timer.setInterval;
(<any>global).clearInterval = timer.clearInterval;
let snapshotGlobals;
export function install() {
if ((<any>global).__snapshot) {
if (!snapshotGlobals) {
// require in snapshot mode is cheap
var timer: typeof timerModule = require("timer");
var dialogs: typeof dialogsModule = require("ui/dialogs");
var xhr = require("xhr");
var fetch = require("fetch");
var consoleModule = require("console");
var dialogs: typeof dialogsModule = require("ui/dialogs");
(<any>global).alert = dialogs.alert;
(<any>global).confirm = dialogs.confirm;
(<any>global).prompt = dialogs.prompt;
snapshotGlobals = snapshotGlobals || {
setTimeout: timer.setTimeout,
clearTimeout: timer.clearTimeout,
setInterval: timer.setInterval,
clearInterval: timer.clearInterval,
var xhr = require("xhr");
(<any>global).XMLHttpRequest = xhr.XMLHttpRequest;
(<any>global).FormData = xhr.FormData;
alert: dialogs.alert,
confirm: dialogs.confirm,
prompt: dialogs.prompt,
var fetch = require("fetch");
(<any>global).fetch = fetch.fetch;
(<any>global).Headers = fetch.Headers;
(<any>global).Request = fetch.Request;
(<any>global).Response = fetch.Response;
} 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");
}
// check whether the 'android' namespace is exposed
// if positive - the current device is an Android
// so a custom implementation of the global 'console' object is attached.
// otherwise do nothing on iOS - the NS runtime provides a native 'console' functionality.
if ((<any>global).android || (<any>global).__snapshot) {
const consoleModule = require("console");
(<any>global).console = new consoleModule.Console();
XMLHttpRequest: xhr.XMLHttpRequest,
FormData: xhr.FormData,
fetch: fetch.fetch,
Headers: fetch.Headers,
Request: fetch.Request,
Response: fetch.Response,
console: new consoleModule.Console()
}
}
Object.assign(global, snapshotGlobals);
} 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");
// check whether the 'android' namespace is exposed
// if positive - the current device is an Android
// so a custom implementation of the global 'console' object is attached.
// otherwise do nothing on iOS - the NS runtime provides a native 'console' functionality.
if ((<any>global).android) {
const consoleModule = require("console");
(<any>global).console = new consoleModule.Console();
}
}
}
install();
export function Deprecated(target: Object, key?: string | symbol, descriptor?: any) {
if (descriptor) {

View File

@ -4,4 +4,3 @@
"types": "globals.d.ts",
"nativescript": {}
}

View File

@ -1,6 +1,11 @@
import { setActivityCallbacks, AndroidActivityCallbacks } from "./frame";
import * as globals from "../../globals";
import * as appModule from "../../application";
if ((<any>global).__snapshot) {
globals.install();
}
@JavaProxy("com.tns.NativeScriptActivity")
class NativeScriptActivity extends android.app.Activity {
private _callbacks: AndroidActivityCallbacks;