mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 21:01:34 +08:00
Globals in snapshot are overriden by the V8 on start, so they have to be installed later (#4387)
This commit is contained in:
4
tns-core-modules/globals/globals.d.ts
vendored
Normal file
4
tns-core-modules/globals/globals.d.ts
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
/**
|
||||||
|
* Register globals such as setTimeout, setInterval, console etc.
|
||||||
|
*/
|
||||||
|
export function install();
|
@ -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) {
|
||||||
|
@ -4,4 +4,3 @@
|
|||||||
"types": "globals.d.ts",
|
"types": "globals.d.ts",
|
||||||
"nativescript": {}
|
"nativescript": {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
Reference in New Issue
Block a user