pulsar/static/index.js

218 lines
6.9 KiB
JavaScript
Raw Normal View History

2019-05-31 19:33:56 +03:00
(function() {
// Define the window start time before the requires so we get a more accurate
// window:start marker.
2019-05-31 19:33:56 +03:00
const startWindowTime = Date.now();
2019-05-31 19:33:56 +03:00
const electron = require('electron');
const path = require('path');
const Module = require('module');
const getWindowLoadSettings = require('../src/get-window-load-settings');
const getReleaseChannel = require('../src/get-release-channel');
2019-05-31 19:33:56 +03:00
const StartupTime = require('../src/startup-time');
const entryPointDirPath = __dirname;
let blobStore = null;
let useSnapshot = false;
2017-02-27 16:47:20 +03:00
2019-05-31 19:33:56 +03:00
const startupMarkers = electron.remote.getCurrentWindow().startupMarkers;
if (startupMarkers) {
2019-05-31 19:33:56 +03:00
StartupTime.importData(startupMarkers);
}
2019-05-31 19:33:56 +03:00
StartupTime.addMarker('window:start', startWindowTime);
2019-05-31 19:33:56 +03:00
window.onload = function() {
2017-02-27 16:47:20 +03:00
try {
2019-05-31 19:33:56 +03:00
StartupTime.addMarker('window:onload:start');
const startTime = Date.now();
2017-02-27 16:47:20 +03:00
2019-05-31 19:33:56 +03:00
process.on('unhandledRejection', function(error, promise) {
console.error(
'Unhandled promise rejection %o with error: %o',
promise,
error
);
});
2017-02-27 16:47:20 +03:00
// Normalize to make sure drive letter case is consistent on Windows
2019-05-31 19:33:56 +03:00
process.resourcesPath = path.normalize(process.resourcesPath);
2017-02-27 16:47:20 +03:00
2019-05-31 19:33:56 +03:00
setupAtomHome();
const devMode =
getWindowLoadSettings().devMode ||
!getWindowLoadSettings().resourcePath.startsWith(
process.resourcesPath + path.sep
);
useSnapshot = !devMode && typeof snapshotResult !== 'undefined';
2017-02-27 16:47:20 +03:00
if (devMode) {
2019-05-31 19:33:56 +03:00
const metadata = require('../package.json');
2017-02-27 16:47:20 +03:00
if (!metadata._deprecatedPackages) {
try {
2019-05-31 19:33:56 +03:00
metadata._deprecatedPackages = require('../script/deprecated-packages.json');
2017-02-27 16:47:20 +03:00
} catch (requireError) {
2019-05-31 19:33:56 +03:00
console.error(
'Failed to setup deprecated packages list',
requireError.stack
);
2017-02-27 16:47:20 +03:00
}
}
2017-02-27 16:47:20 +03:00
} else if (useSnapshot) {
2019-05-31 19:33:56 +03:00
Module.prototype.require = function(module) {
const absoluteFilePath = Module._resolveFilename(module, this, false);
let relativeFilePath = path.relative(
entryPointDirPath,
absoluteFilePath
);
if (process.platform === 'win32') {
2019-05-31 19:33:56 +03:00
relativeFilePath = relativeFilePath.replace(/\\/g, '/');
}
2019-05-31 19:33:56 +03:00
let cachedModule =
snapshotResult.customRequire.cache[relativeFilePath];
2017-02-28 14:15:00 +03:00
if (!cachedModule) {
2019-05-31 19:33:56 +03:00
cachedModule = { exports: Module._load(module, this, false) };
snapshotResult.customRequire.cache[relativeFilePath] = cachedModule;
2017-02-28 14:15:00 +03:00
}
2019-05-31 19:33:56 +03:00
return cachedModule.exports;
};
snapshotResult.setGlobals(
global,
process,
window,
document,
console,
require
);
}
2019-05-31 19:33:56 +03:00
const FileSystemBlobStore = useSnapshot
? snapshotResult.customRequire('../src/file-system-blob-store.js')
: require('../src/file-system-blob-store');
blobStore = FileSystemBlobStore.load(
path.join(process.env.ATOM_HOME, 'blob-store')
);
2017-03-02 11:11:54 +03:00
2019-05-31 19:33:56 +03:00
const NativeCompileCache = useSnapshot
? snapshotResult.customRequire('../src/native-compile-cache.js')
: require('../src/native-compile-cache');
NativeCompileCache.setCacheStore(blobStore);
NativeCompileCache.setV8Version(process.versions.v8);
NativeCompileCache.install();
2017-03-02 11:11:54 +03:00
2017-02-27 16:47:20 +03:00
if (getWindowLoadSettings().profileStartup) {
2019-05-31 19:33:56 +03:00
profileStartup(Date.now() - startTime);
2017-02-27 16:47:20 +03:00
} else {
2019-05-31 19:33:56 +03:00
StartupTime.addMarker('window:setup-window:start');
setupWindow().then(() => {
2019-05-31 19:33:56 +03:00
StartupTime.addMarker('window:setup-window:end');
});
setLoadTime(Date.now() - startTime);
2015-08-22 02:56:32 +03:00
}
2017-02-27 16:47:20 +03:00
} catch (error) {
2019-05-31 19:33:56 +03:00
handleSetupError(error);
2017-02-27 16:47:20 +03:00
}
2019-05-31 19:33:56 +03:00
StartupTime.addMarker('window:onload:end');
};
2017-02-27 16:47:20 +03:00
2019-05-31 19:33:56 +03:00
function setLoadTime(loadTime) {
2017-02-27 16:47:20 +03:00
if (global.atom) {
2019-05-31 19:33:56 +03:00
global.atom.loadTime = loadTime;
2017-02-27 16:47:20 +03:00
}
}
2019-05-31 19:33:56 +03:00
function handleSetupError(error) {
const currentWindow = electron.remote.getCurrentWindow();
currentWindow.setSize(800, 600);
currentWindow.center();
currentWindow.show();
currentWindow.openDevTools();
console.error(error.stack || error);
2017-02-27 16:47:20 +03:00
}
2019-05-31 19:33:56 +03:00
function setupWindow() {
const CompileCache = useSnapshot
? snapshotResult.customRequire('../src/compile-cache.js')
: require('../src/compile-cache');
CompileCache.setAtomHomeDirectory(process.env.ATOM_HOME);
CompileCache.install(process.resourcesPath, require);
const ModuleCache = useSnapshot
? snapshotResult.customRequire('../src/module-cache.js')
: require('../src/module-cache');
ModuleCache.register(getWindowLoadSettings());
const startCrashReporter = useSnapshot
? snapshotResult.customRequire('../src/crash-reporter-start.js')
: require('../src/crash-reporter-start');
const { userSettings, appVersion } = getWindowLoadSettings();
const uploadToServer =
userSettings &&
userSettings.core &&
userSettings.core.telemetryConsent === 'limited';
const releaseChannel = getReleaseChannel(appVersion);
startCrashReporter({
uploadToServer,
releaseChannel
});
2019-05-31 19:33:56 +03:00
const CSON = useSnapshot
? snapshotResult.customRequire('../node_modules/season/lib/cson.js')
: require('season');
CSON.setCacheDir(path.join(CompileCache.getCacheDirectory(), 'cson'));
const initScriptPath = path.relative(
entryPointDirPath,
getWindowLoadSettings().windowInitializationScript
);
const initialize = useSnapshot
? snapshotResult.customRequire(initScriptPath)
: require(initScriptPath);
StartupTime.addMarker('window:initialize:start');
return initialize({ blobStore: blobStore }).then(function() {
StartupTime.addMarker('window:initialize:end');
electron.ipcRenderer.send('window-command', 'window:loaded');
});
2017-02-27 16:47:20 +03:00
}
2019-05-31 19:33:56 +03:00
function profileStartup(initialTime) {
function profile() {
console.profile('startup');
const startTime = Date.now();
setupWindow().then(function() {
setLoadTime(Date.now() - startTime + initialTime);
console.profileEnd('startup');
console.log(
'Switch to the Profiles tab to view the created startup profile'
);
});
2017-02-27 16:47:20 +03:00
}
2015-05-13 19:47:10 +03:00
2019-05-31 19:33:56 +03:00
const webContents = electron.remote.getCurrentWindow().webContents;
2017-02-27 16:47:20 +03:00
if (webContents.devToolsWebContents) {
2019-05-31 19:33:56 +03:00
profile();
2017-02-27 16:47:20 +03:00
} else {
2019-05-31 19:33:56 +03:00
webContents.once('devtools-opened', () => {
setTimeout(profile, 1000);
});
webContents.openDevTools();
2017-02-27 16:47:20 +03:00
}
}
2019-05-31 19:33:56 +03:00
function setupAtomHome() {
2017-02-27 16:47:20 +03:00
if (process.env.ATOM_HOME) {
2019-05-31 19:33:56 +03:00
return;
2017-02-27 16:47:20 +03:00
}
2015-06-04 02:11:16 +03:00
2017-02-27 16:47:20 +03:00
// Ensure ATOM_HOME is always set before anything else is required
// This is because of a difference in Linux not inherited between browser and render processes
// https://github.com/atom/atom/issues/5412
if (getWindowLoadSettings() && getWindowLoadSettings().atomHome) {
2019-05-31 19:33:56 +03:00
process.env.ATOM_HOME = getWindowLoadSettings().atomHome;
}
}
2019-05-31 19:33:56 +03:00
})();