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