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) { let snapshotGlobals;
// when we have a snapshot, it is better to pre-populate these on the global context to get them saved within the blob export function install() {
var timer: typeof timerModule = require("timer"); if ((<any>global).__snapshot) {
(<any>global).setTimeout = timer.setTimeout; if (!snapshotGlobals) {
(<any>global).clearTimeout = timer.clearTimeout; // require in snapshot mode is cheap
(<any>global).setInterval = timer.setInterval; var timer: typeof timerModule = require("timer");
(<any>global).clearInterval = timer.clearInterval; 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"); snapshotGlobals = snapshotGlobals || {
(<any>global).alert = dialogs.alert; setTimeout: timer.setTimeout,
(<any>global).confirm = dialogs.confirm; clearTimeout: timer.clearTimeout,
(<any>global).prompt = dialogs.prompt; setInterval: timer.setInterval,
clearInterval: timer.clearInterval,
var xhr = require("xhr"); alert: dialogs.alert,
(<any>global).XMLHttpRequest = xhr.XMLHttpRequest; confirm: dialogs.confirm,
(<any>global).FormData = xhr.FormData; prompt: dialogs.prompt,
var fetch = require("fetch"); XMLHttpRequest: xhr.XMLHttpRequest,
(<any>global).fetch = fetch.fetch; FormData: xhr.FormData,
(<any>global).Headers = fetch.Headers;
(<any>global).Request = fetch.Request; fetch: fetch.fetch,
(<any>global).Response = fetch.Response; Headers: fetch.Headers,
} else { Request: fetch.Request,
registerOnGlobalContext("setTimeout", "timer"); Response: fetch.Response,
registerOnGlobalContext("clearTimeout", "timer");
registerOnGlobalContext("setInterval", "timer"); console: new consoleModule.Console()
registerOnGlobalContext("clearInterval", "timer"); }
registerOnGlobalContext("alert", "ui/dialogs"); }
registerOnGlobalContext("confirm", "ui/dialogs"); Object.assign(global, snapshotGlobals);
registerOnGlobalContext("prompt", "ui/dialogs"); } else {
registerOnGlobalContext("XMLHttpRequest", "xhr"); registerOnGlobalContext("setTimeout", "timer");
registerOnGlobalContext("FormData", "xhr"); registerOnGlobalContext("clearTimeout", "timer");
registerOnGlobalContext("fetch", "fetch"); registerOnGlobalContext("setInterval", "timer");
} registerOnGlobalContext("clearInterval", "timer");
registerOnGlobalContext("alert", "ui/dialogs");
// check whether the 'android' namespace is exposed registerOnGlobalContext("confirm", "ui/dialogs");
// if positive - the current device is an Android registerOnGlobalContext("prompt", "ui/dialogs");
// so a custom implementation of the global 'console' object is attached. registerOnGlobalContext("XMLHttpRequest", "xhr");
// otherwise do nothing on iOS - the NS runtime provides a native 'console' functionality. registerOnGlobalContext("FormData", "xhr");
if ((<any>global).android || (<any>global).__snapshot) { registerOnGlobalContext("fetch", "fetch");
const consoleModule = require("console");
(<any>global).console = new consoleModule.Console(); // 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) { export function Deprecated(target: Object, key?: string | symbol, descriptor?: any) {
if (descriptor) { if (descriptor) {

View File

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

View File

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